다음은 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
곰이다 영상. 저는 을 사용하고 있습니다. PHP는 Imagick
이라는 래퍼 클래스를 가지고 있는데, 문제가 생길 때 찾아 볼만한 가치가 있습니다.
나는 이것이 당신에게 도움이되기를 바랍니다, 행운을 비네!