Imagemagick miniaturas geração com php - usando -crop
-
16-09-2019 - |
Pergunta
Há muito tempo eu criei uma pequena biblioteca para redimensionar imagens usando imagemagick através system(...)
porque eu não sinto que o build-in funções imagemagick para PHP eram suficientes.
No entanto, recentemente eu tive que porta a um projeto symfony e eu escolhi usar sfThumbnailPlugin (se bem me lembro). Este infelizmente não incluem a funcionalidade de colheita - isto é, para especificar um tamanho desejado, por exemplo 300x300 px e ter a miniatura cortada para que ele se encaixa. Eu escolhi para implementar essa funcionalidade mim mesmo, mas parece haver algo errado.
Sempre que redimensionar uma imagem para um tamanho desejado há whe largura é maior que a altura, as proporções se ferrar. Dê uma olhada neste exemplo: http://i37.tinypic.com/9hkqrl.png -. neste exemplo, a linha superior é as proporções corretas e a linha de fundo é o problema
No exemplo, a parte superior e inferior deveriam ter sido cortadas.
Aqui está o código para a parte onde o cultivo é feito (os nomes das variáveis ??devem ser auto-explicativo):
<?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á é a segunda parte da instrução if que produz o código errado.
Alguém pode corrigir isso para mim? Obviamente, os cálculos estão errados.
Solução
A solução foi o seguinte:
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';
}
Outras dicas
Eu digo uma sugestão que você pode experimentá-lo,
Eu vi o link da imagem Eu acho que as condições não são verificados corretamente.
condiçãode ter verificado [(largura / altura)> (maxWidth / maxHeight)] Em vez de verificação
if (largura == altura) {}
elseif (largura> altura) {}
mais (largura
Verifique esta condição e de acordo com esta condição cortar a imagem e redimensionamento.
Obrigado