views:

679

answers:

2

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.

+1  A: 

Hi phidah, I tell one suggestion you can try it,

I saw the Image link I think the conditions are not checked correctly.

you have checked condition [(width/height) > (maxWidth/maxHeight)] Instead of check

if(width == height) { } elseif(width > height) { } else(width < height){ }

Check this condition and according to this condition crop the image and resize.

Thank you

RSK
+1  A: 

The solution was the following:

    if ($width/$height > $this->maxWidth/$this->maxHeight) {
      // then resize to the new height...
    $command .= ' -resize "x'.$this->maxHeight.'"';

    // ... and get the middle part of the new image
    // what is the resized width?
    $resized_w = ($this->maxHeight/$height) * $width;

    // crop
    $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.'+'.round(($resized_w - $this->maxWidth)/2).'+0" +repage';
   } else {
     $command .= ' -resize "' . $this->maxWidth . 'x"';
     $resized_h = ($this->maxWidth/$width) * $height;

    // crop
    $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
                 '+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
   }
phidah
mark this question as answered
Justin Johnson
can't until tomorrow.
phidah