ここでは、GDのlibを使用している間に私が書いたイメージクラスを紹介します。
https://github.com/delboy1978uk/image/blob/master/src/Image.php
私は焦点に基づいてリサイズやトリミングのためのコードを書いていないが、私は焦点が中央にあることを前提に動作しますresizeAndCrop()
方法を持っています。
ここ
public function resizeAndCrop($width,$height)
{
$target_ratio = $width/$height;
$actual_ratio = $this->getWidth()/$this->getHeight();
if($target_ratio == $actual_ratio){
// Scale to size
$this->resize($width,$height);
} elseif($target_ratio > $actual_ratio) {
// Resize to width, crop extra height
$this->resizeToWidth($width);
$this->crop($width,$height,true);
} else {
// Resize to height, crop additional width
$this->resizeToHeight($height);
$this->crop($width,$height,true);
}
}
はあなたに焦点を設定することができcrop()
方法、の左、中央、右:
/**
* @param $width
* @param $height
* @param string $trim
*/
public function crop($width,$height, $trim = 'center')
{
$offset_x = 0;
$offset_y = 0;
$current_width = $this->getWidth();
$current_height = $this->getHeight();
if($trim != 'left')
{
if($current_width > $width) {
$diff = $current_width - $width;
$offset_x = ($trim == 'center') ? $diff/2 : $diff; //full diff for trim right
}
if($current_height > $height) {
$diff = $current_height - $height;
$offset_y = ($trim = 'center') ? $diff/2 : $diff;
}
}
$new_image = imagecreatetruecolor($width,$height);
imagecopyresampled($new_image,$this->_image,0,0,$offset_x,$offset_y,$width,$height,$width,$height);
$this->_image = $new_image;
}
は、私はあなただけトリミングの背後にある理論を探していたので、imagecopyresampled()
を説明する気にしないだろうが、ドキュメントはPHPのGDライブラリを使用して画像をリサイズするの大きさに応じて、メモリ集約的であり、心の中でここhttp://php.net/manual/en/function.imagecopyresampled.php
ベアです画像。私はimagemagick
を使用したいと思います。PHPにはImagick
と呼ばれるラッパークラスがあり、問題が発生した場合はそれを調べる価値があります。
私はこれがあなたに役立つことを願っています。