PHP 는 imagick 을 이용 하여 조합 미리 보기 그림 을 생 성 합 니 다.

먼저 효과 도 를 보 여 드 리 겠 습 니 다.여러분 이 만족 하 시 면 계속 읽 어 주세요.

여기 서 말 하 는 imagick 은 ImageMagick 이 PHP 에서 의 확장 입 니 다.pecl 을 사용 하여 설치 하면 쉬 운 명령 이 라 고 합 니 다.

sudo pecl install imagick
(확장 설치 후 php.ini 에 extension=imagick.so 를 추가 하고 apache 나 phop-fpm 서 비 스 를 다시 시작 하 는 것 을 기억 하 세 요.)
최근 에는 여러 장의 그림 을 조합 하여 미리 보기 그림 을 만 드 는 것 이 필요 합 니 다.이 강력 한 imagick 으로 확장 할 수 있 습 니 다.
이 요 구 는 미리 보기 그림 을 이렇게 생 성 하 는 것 입 니 다.
1.그림 이 한 장 있 으 면 이 그림 의 미리 보기 그림 을 직접 생 성 합 니 다.
2.그림 이 두 장 있 으 면 한 장 은 왼쪽 에 있 고 한 장 은 오른쪽 에 있 으 며 각각 반 씩 있다.
3.그림 이 3 장 있 으 면 왼쪽 두 장 을 평균 적 으로 분배 하고 한 장 은 오른쪽 을 독점 합 니 다.
4.그림 4 장 이 있 으 면 밭 칸 처럼 공간 을 균등 하 게 분배 한다.
5.더 많은 그림 은 앞의 4 장 만 가 져 오고,필드 방식 으로 미리 보기 그림 을 생 성 합 니 다.
이 규칙 은 정말 적지 않 지만 너무 복잡 한 편 은 아니 어서 곧 만들어 냈 다.

namespace \clarence\thumbnail;
class Thumbnail extends \Imagick
{
/**
* @param array $images
* @param int $width
* @param int $height
* @return static
* @throws ThumbnailException
*/
public static function createFromImages($images, $width, $height){
if (empty($images)){
throw new ThumbnailException("No images!");
}
$thumbnail = new static();
$thumbnail->newImage($width, $height, 'white', 'jpg');
$thumbnail->compositeImages($images);
return $thumbnail;
}
public function compositeImages($images){
$imagesKeys = array_keys($images);
$compositeConfig = $this->calcCompositeImagesPosAndSize($images);
foreach ($compositeConfig as $index => $cfg){
$imgKey = $imagesKeys[$index];
$img = new \Imagick($images[$imgKey]);
$img = $this->makeCompositeThumbnail($img, $cfg);
$this->compositeImage($img, self::COMPOSITE_OVER, $cfg['to']['x'], $cfg['to']['y']);
}
}
protected function makeCompositeThumbnail(\Imagick $img, $cfg){
$img->cropThumbnailImage($cfg['size']['width'], $cfg['size']['height']);
return $img;
}
protected function calcCompositeImagesPosAndSize($images){
$width = $this->getImageWidth();
$height = $this->getImageHeight();
switch(count($images)){
case 0:
throw new ThumbnailException("No images!");
case 1:
// | 0 |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width,
'height' => $height,
]
]
];
case 2:
// | 0 | 1 |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width / 2,
'height' => $height,
]
],
1 => [
'to' => [ 'x' => $width / 2, 'y' => 0],
'size' => [
'width' => $width / 2,
'height' => $height,
]
]
];
case 3:
// | 0 | 1 |
// | 2 | |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
1 => [
'to' => [ 'x' => $width / 2, 'y' => 0],
'size' => [
'width' => $width / 2,
'height' => $height,
]
],
2 => [
'to' => [ 'x' => 0, 'y' => $height / 2 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
];
default:
// >= 4:
// | 0 | 1 |
// | 2 | 3 |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
1 => [
'to' => [ 'x' => $width / 2, 'y' => 0],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
2 => [
'to' => [ 'x' => 0, 'y' => $height / 2 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
3 => [
'to' => [ 'x' => $width / 2, 'y' => $height / 2],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
];
}
}
}
사용 해 보기:

$thumbnail = \clarence\thumbnail\Thumbnail::createFromImages($srcImages, 240, 320);
$thumbnail->writeImage($outputDir."/example.jpg");
이상 의 내용 은 PHP 가 imagick 을 이용 하여 조합 미리 보기 그림 을 만 드 는 것 에 관 한 지식 을 소개 하 였 으 니 여러분 에 게 도움 이 되 기 를 바 랍 니 다!

좋은 웹페이지 즐겨찾기