Pregunta

Hace

Long creé una pequeña biblioteca para cambiar el tamaño de las imágenes usando ImageMagick través system(...) porque no me siento que la acumulación de funciones de ImageMagick para PHP fueron suficientes.

Sin embargo, recientemente he tenido al puerto a un proyecto Symfony y opté por usar sfThumbnailPlugin (si no recuerdo mal). Esto, desafortunadamente, no incluye la funcionalidad de cultivos - es decir, para especificar un tamaño deseado, por ejemplo, 300x300 píxeles y tienen la miniatura recortadas de manera que se ajuste. Elegí para implementar esta funcionalidad a mí mismo, pero parece que hay algo mal.

Siempre que cambiar el tamaño de una imagen a un tamaño deseado no whe anchura es mayor que la altura, las proporciones consiguen atornilladas. Echar un vistazo a este ejemplo: http://i37.tinypic.com/9hkqrl.png -. En este ejemplo, la fila superior es las proporciones correctas y la fila inferior es el problema

En el ejemplo, la parte superior e inferior deberían haber sido recortada.

Este es el código para la parte donde se realiza el cultivo (los nombres de las variables deben explicarse por sí mismo):

<?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';
            }

Está es la segunda parte de la sentencia if que se produce un código incorrecto.

¿Alguien puede corregir esto para mí? Es evidente que los cálculos están equivocados.

¿Fue útil?

Solución

La solución fue el siguiente:

    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';
            }

Otros consejos

Yo digo una sugerencia que puede probarlo,

vi el enlace Imagen Creo que las condiciones no son controlados correctamente.

condición

que haya comprobado [(anchura / altura)> (anchoMax / maxHeight)] En lugar de verificación

si (ancho == altura) {} elseif (anchura> altura) {} más (anchura

Comprobar esta condición y de acuerdo con esta condición recortar la imagen y cambiar el tamaño.

Gracias

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top