Persist Image Modifications Post-JavaScript

Drupal 7 module uploads and previews images with borders updated via JavaScript. How can PHP save the image with its modified style?

<img id="imgPreview" src="initial.png" alt="preview image">
<script>
function adjustBorder() {
  document.getElementById("imgPreview").style.border = "2px dotted blue";
}
</script>

hey, so js only flexes the look on client side. you gotta use php libs (like gd) to redraw your image with border and then save it. otherwise, your image stays unchanged on the server side.

One approach that has worked well in my past projects is to send the style modifications from the client side as parameters to the server using an AJAX call. Instead of capturing the visual changes through means like canvas, you can simply pass the border details and any other adjustments to PHP. On the server, using a library like Imagick, you can load the original image and apply the modifications exactly as specified. This method not only maintains flexibility regarding what modifications you can apply but also keeps the image processing workflow organized and controlled on the server.

A viable alternative to regenerating your image using PHP libraries is to shift the process to the client side. In my experience, converting the modified image into a canvas element allows you to capture all visual modifications applied through JavaScript. Once rendered on the canvas, you can convert it to a data URL and send it to the server for saving. This technique has worked well in past projects and simplifies working with dynamically modified images while maintaining server-side efficiency.

hey, yo can draw the img on a canvas with the modded style then get the base64 output to send to php. its a neat trick to keep your js changes permanent by recreating the image on the canvas.