Note that there are some explanatory texts on larger screens.

plurals
  1. POimage::Magick perl module - Multiple crop/resize manipulations from one Read and one Write?
    text
    copied!<p>I have a Perl script that uses the Image::Magick perl module to crop an existing image from a web form (that uses JCrop to feed me width, height, x, y). Because the cropped image will be used in a responsive design environment, I'm creating multiple sizes of the image for use on the front end of the web site. The sizes are built in an array (as seen in the code below) and are handled one by one to create each image size.</p> <p>As you'll see from the code below, I ultimately have to open the image 3 times and write it 3 times...which seems rather excessive...so I'm hoping you guys know a better way to do this.</p> <p>I originally tried to use Image::Magick to simply open the file one time, run the "crop, resize, crop" process and then write the image once, but the results were horrendous. None of the coordinates translated properly and as a result the image was not even close to the size of what the user requested in the web form...despite the values being read perfectly.</p> <p>So my question to all of you is whether anyone has been able to pull off a single "open", (with multiple manipulations of the opened image), and then perform a single "write" using the Image::Magick perl module? And if so, could you please provide a sample that would be a long the lines of the code I've posted below? I greatly appreciate any help that can be given. A snippet of my code is below. Sorry for the excessive commenting, I wanted to make it as easy as possible to follow along with :)</p> <pre><code>#!/usr/bin/perl use Image::Magick; use CGI qw(:cgi-lib); &amp;ReadParse(*input); ############################## # Build array of sizes needed ############################## my @sizes = ("1280","960","640","480","320","160"); foreach $size (@sizes) { $resized = "/path/to/size/folders/$size\/$input{image_ID}\.png"; $image = Image::Magick-&gt;new; $x = $image-&gt;Read("/path/to/fullsize/image"); ######################### # Run the requested crop ######################### $x = $image-&gt;Crop(width=&gt;$input{w},height=&gt;$input{h},x=&gt;$input{x},y=&gt;$input{y}); ######################## # Write cropped version ######################## $x = $image-&gt;Write("$resized"); ########################### # Open the cropped version ########################### $image = Image::Magick-&gt;new; $x = $image-&gt;Read("$resized"); ############################################### # Size the image down to +2 pixels all around # to handle border opacity when pixel rounding ############################################### $temp_width = $size + 2; $temp_height = ($temp_width * $input{h}) / $input{w}; ########################### # Resize the cropped image ########################### $x = $image-&gt;Resize(width=&gt;$temp_width, height=&gt;$temp_height); ################################ # Write the newly resized image ################################ $x = $image-&gt;Write("$resized"); ######################################## # Calculate final dimensions and coords ######################################## $final_height = ($size * $temp_height) / $temp_width; $final_x = 1; $final_y = 1; ############################### # Open the newly resized image ############################### $image = Image::Magick-&gt;new; $x = $image-&gt;Read("$resized"); ####################################### # Final crop the image for clean edges ####################################### $x = $image-&gt;Crop(width=&gt;$size,height=&gt;$final_height,x=&gt;$final_x,y=&gt;$final_y); ######################################## # Write the final cropped image for use ######################################## $x = $image-&gt;Write("$resized"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload