Imagemagick thumbnail generation with php - using -crop

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) {
      // 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 fix this for me? Obviously, the calculations are wrong.

0
source share
2 answers

The solution was as follows:

    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';
            }
+2
source

, ,

"", , .

[(/) > (maxWidth/maxHeight)]

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

.

+1

Source: https://habr.com/ru/post/1755382/


All Articles