ImageMagick 기본 사용법
Imagick은 OO 인터페이스를 통해 PHP에서 이미지 조작을 매우 쉽게 만듭니다. 다음은 미리보기 이미지를 만드는 방법에 대한 간단한 예입니다.
예제 #1 Imagick에서 썸네일 만들기
<?php
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>
Imagick에서 지원되는 SPL 및 기타 OO 기능을 사용하여 디렉토리의 모든 파일 크기를 간단하게 조정할 수 있습니다(웹에서 볼 수 있도록 대용량 디지털 카메라 이미지의 크기를 일괄 조정하는 데 유용). 여기에서는 특정 메타 데이터를 유지하기를 원할 수 있으므로 크기 조정을 사용합니다.
예제 #2 디렉토리에 있는 모든 JPG 파일의 썸네일 만들기
<?php
$images = new Imagick(glob('images/*.JPG'));
foreach($images as $image) {
// Providing 0 forces thumbnailImage to maintain aspect ratio
$image->thumbnailImage(1024,0);
}
$images->writeImages();
?>
이것은 이미지의 반사를 생성하는 예입니다. 반사는 이미지를 뒤집고 그 위에 그라디언트를 겹쳐서 만듭니다. 그런 다음 원본 이미지와 반사가 모두 캔버스에 오버레이됩니다.
예제 #3 이미지의 반사 만들기
<?php
/* Read the image */
$im = new Imagick("test.png");
/* Thumbnail the image */
$im->thumbnailImage(200, null);
/* Create a border for the image */
$im->borderImage(new ImagickPixel("white"), 5, 5);
/* Clone the image and flip it */
$reflection = $im->clone();
$reflection->flipImage();
/* Create gradient. It will be overlayed on the reflection */
$gradient = new Imagick();
/* Gradient needs to be large enough for the image and the borders */
$gradient->newPseudoImage($reflection->getImageWidth() + 10, $reflection->getImageHeight() + 10, "gradient:transparent-black");
/* Composite the gradient on the reflection */
$reflection->compositeImage($gradient, imagick::COMPOSITE_OVER, 0, 0);
/* Add some opacity. Requires ImageMagick 6.2.9 or later */
$reflection->setImageOpacity( 0.3 );
/* Create an empty canvas */
$canvas = new Imagick();
/* Canvas needs to be large enough to hold the both images */
$width = $im->getImageWidth() + 40;
$height = ($im->getImageHeight() * 2) + 30;
$canvas->newImage($width, $height, new ImagickPixel("black"));
$canvas->setImageFormat("png");
/* Composite the original image and the reflection on the canvas */
$canvas->compositeImage($im, imagick::COMPOSITE_OVER, 20, 10);
$canvas->compositeImage($reflection, imagick::COMPOSITE_OVER, 20, $im->getImageHeight() + 10);
/* Output the image*/
header("Content-Type: image/png");
echo $canvas;
?>
위의 예는 다음과 유사한 결과를 출력합니다.
이 예는 그리는 동안 채우기 패턴을 사용하는 방법을 보여줍니다.
예제 #4 그라디언트로 텍스트 채우기
<?php
/* Create a new imagick object */
$im = new Imagick();
/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");
/* Create imagickdraw object */
$draw = new ImagickDraw();
/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);
/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);
/* Close the pattern */
$draw->popPattern();
/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');
/* Set font size to 52 */
$draw->setFontSize(52);
/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");
/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");
/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);
/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);
/* Set the format to PNG */
$canvas->setImageFormat('png');
/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>
위의 예는 다음과 유사한 결과를 출력합니다.
애니메이션 GIF 이미지 작업
예제 #5 GIF 이미지 읽기 및 모든 프레임 크기 조정
<?php
/* Create a new imagick object and read in GIF */
$im = new Imagick("example.gif");
/* Resize all frames */
foreach ($im as $frame) {
/* 50x50 frames */
$frame->thumbnailImage(50, 50);
/* Set the virtual canvas to correct size */
$frame->setImagePage(50, 50, 0, 0);
}
/* Notice writeImages instead of writeImage */
$im->writeImages("example_small.gif", true);
?>
타원 기본 및 사용자 정의 글꼴 작업
예제 #6 PHP 로고 만들기
<?php
/* Set width and height in proportion of genuine PHP logo */
$width = 400;
$height = 210;
/* Create an Imagick object with transparent canvas */
$img = new Imagick();
$img->newImage($width, $height, new ImagickPixel('transparent'));
/* New ImagickDraw instance for ellipse draw */
$draw = new ImagickDraw();
/* Set purple fill color for ellipse */
$draw->setFillColor('#777bb4');
/* Set ellipse dimensions */
$draw->ellipse($width / 2, $height / 2, $width / 2, $height / 2, 0, 360);
/* Draw ellipse onto the canvas */
$img->drawImage($draw);
/* Reset fill color from purple to black for text (note: we are reusing ImagickDraw object) */
$draw->setFillColor('black');
/* Set stroke border to white color */
$draw->setStrokeColor('white');
/* Set stroke border thickness */
$draw->setStrokeWidth(2);
/* Set font kerning (negative value means that letters are closer to each other) */
$draw->setTextKerning(-8);
/* Set font and font size used in PHP logo */
$draw->setFont('Handel Gothic.ttf');
$draw->setFontSize(150);
/* Center text horizontally and vertically */
$draw->setGravity(Imagick::GRAVITY_CENTER);
/* Add center "php" with Y offset of -10 to canvas (inside ellipse) */
$img->annotateImage($draw, 0, -10, 0, 'php');
$img->setImageFormat('png');
/* Set appropriate header for PNG and output the image */
header('Content-Type: image/png');
echo $img;
?>
위의 예는 다음과 유사한 결과를 출력합니다.