Once upon a time, I created a small library for resizing images using imagemagick through system(...), because I did not feel that the built-in imagemagick functions for PHP were sufficient.
However, I recently had to migrate this to a symfony project, and I decided to use sfThumbnailPlugin (if I remember correctly). This, unfortunately, did not include the functionality of the crop - that is, to indicate the desired size, for example. 300x300 px and reduce the sketch to fit. I decided to implement this function myself, but there seems to be something wrong.
Whenever I resize the image to the desired size, the width increases more than the height, the proportions are screwed. Take a look at this example: http://i37.tinypic.com/9hkqrl.png - In this example, the top line is the correct proportions, and the bottom line is the problem.
In this example, the top and bottom should be trimmed.
Here is the code for the part in which the trimming is done (variable names should be clear):
<?php
if ($width/$height > $this->maxWidth/$this->maxHeight) {
$command .= ' -resize "x'.$this->maxWidth.'"';
$resized_w = ($this->maxWidth/$height) * $width;
$command .= ' -crop "'.$this->maxHeight.'x'.$this->maxWidth.'+'.round(($resized_w - $this->maxWidth)/2).'+0"';
} else {
$command .= ' -resize "'.$this->maxHeight.'x"';
$resized_h = ($this->maxHeight/$width) * $height;
$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 fix this for me? Obviously, the calculations are wrong.
source
share