Hi there,
Long ago I created a small library for resizing images using imagemagick through system(...)
because I did not feel that the build-in imagemagick functions for PHP were sufficient.
However, recently I had to port this to a symfony project and I chose to use sfThumbnailPlugin (if I remember correctly). This unfortunately didn't include the crop functionality - i.e. to specify a desired size, e.g. 300x300 px and have the thumbnail cropped so that it fit. I chose to implement this functionality myself, but there seems to be something wrong.
Whenever I resize an image to a desired size there whe width is greater than the height, the proportions get screwed. Take a look at this example: http://i37.tinypic.com/9hkqrl.png - In this example the top row is the correct proportions and the bottom row is the problem.
In the example, the top and bottom should have been cropped.
Here is the code for the part where the crop is done (the variable names should be self-explanatory):
<?php
if ($width/$height > $this->maxWidth/$this->maxHeight) {
// then resize to the new height...
$command .= ' -resize "x'.$this->maxWidth.'"';
// ... and get the middle part of the new image
// what is the resized width?
$resized_w = ($this->maxWidth/$height) * $width;
// crop
$command .= ' -crop "'.$this->maxHeight.'x'.$this->maxWidth.'+'.round(($resized_w - $this->maxWidth)/2).'+0"';
} else {
// or else resize to the new width
$command .= ' -resize "'.$this->maxHeight.'x"';
// ... and get the middle part of the new image
// what is the resized height?
$resized_h = ($this->maxHeight/$width) * $height;
// crop
$command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
'+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
}
Is is the second part of the if statement that produces the wrong code.
Can anyone correct this for me? Obviously the calculations are wrong.