2024 日本 PHP 研討會

基本用法

Imagick 透過物件導向介面讓 PHP 中的影像操作變得非常簡單。以下是如何製作縮圖的快速範例

範例 #1 使用 Imagick 建立縮圖

<?php

header
('Content-type: image/jpeg');

$image = new Imagick('image.jpg');

// 若寬度或高度參數設為 0,
// 則會維持圖片的長寬比
$image->thumbnailImage(100, 0);

echo
$image;

?>

使用 SPL 和 Imagick 支援的其他物件導向功能,可以輕鬆調整目錄中所有檔案的大小(適用於批次調整大型數位相機影像大小以供網頁檢視)。在這裡我們使用 resize,因為我們可能想要保留某些中繼資料。

範例 #2 建立目錄中所有 JPG 檔案的縮圖

<?php

$images
= new Imagick(glob('images/*.JPG'));

foreach(
$images as $image) {

// 將參數設為 0 會強制 thumbnailImage 保持長寬比
$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;
?>

上述範例將輸出類似以下的內容

Output of example : Creating a reflection of an image

此範例說明如何在繪圖期間使用填色圖樣。

範例 #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;
?>

上述範例將輸出類似以下的內容

Output of example : Filling text with gradient

處理動畫 GIF 影像

範例 #5 讀取 GIF 影像並調整所有影格的大小

<?php

/* 建立一個新的 Imagick 物件並讀取 GIF 檔 */
$im = new Imagick("example.gif");

/* 調整所有影格的大小 */
foreach ($im as $frame) {
/* 50x50 的影格 */
$frame->thumbnailImage(50, 50);

/* 設定虛擬畫布為正確大小 */
$frame->setImagePage(50, 50, 0, 0);
}

/* 注意是 writeImages 而不是 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;
?>

上述範例將輸出類似以下的內容

Output of example : Creating PHP logo with Imagick

新增註解

使用者貢獻的註解 2 則註解

vokseli
10 年前
透過傳遞陣列到新的 Imagick 物件來載入多個影像時要小心。這段程式碼來自範例 #2

<?php

$images
= new Imagick(glob('images/*.JPG'));

?>

如果 images 資料夾中有很多影像,PHP 會消耗大量記憶體,因此不建議這樣做。我個人認為,分別迴圈處理每個影像會更好

<?php

$image_files
= glob('images/*.JPG');

foreach (
$image_files as $image_file) {
$image = new Imagick($image_file);
// 處理影像等等...
}

?>

這樣一次只會將單個影像載入記憶體中。
inoshadi at gmail dot com
10 年前
關於範例 #3 建立影像的倒影
----------------------------------------------------
/* 複製影像並翻轉它 */
$reflection = $im->clone();
$reflection->flipImage();
----------------------------------------------------
它先前使用的是 Imagick::clone 函式

自 imagick 3.1.0 版本起,此函式已被棄用,建議改用 clone 關鍵字。

請改用以下程式碼
----------------------------------------------------
/* 複製影像並翻轉它 */
$reflection = clone $im;
$reflection->flipImage();
----------------------------------------------------

https://php.dev.org.tw/manual/en/imagick.clone.php
To Top