Examples
Examples
Imagick makes image manipulation in PHP extremely easy through an OO
interface. Here is a quick example on how to make a thumbnail:
Example #1 Creating a thumbnail in Imagick
<?php
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
$image->thumbnailImage(100, 0);
echo $image;
?>
Using SPL and other OO features supported in Imagick, it can be simple
to resize all files in a directory (useful for batch resizing large
digital camera images to be web viewable). Here we use resize, as we might
want to retain certain meta-data:
Example #2 Make a thumbnail of all JPG files in a directory
<?php
$images = new Imagick(glob('images/*.JPG'));
foreach($images as $image) {
$image->thumbnailImage(1024,0);
}
$images->writeImages();
?>
This is an example of creating a reflection of an image.
The reflection is created by flipping the image and overlaying a gradient on it.
Then both, the original image and the reflection is overlayed on a canvas.
Example #3 Creating a reflection of an image
<?php
$im = new Imagick("test.png");
$im->thumbnailImage(200, null);
$im->borderImage(new ImagickPixel("white"), 5, 5);
$reflection = $im->clone();
$reflection->flipImage();
$gradient = new Imagick();
$gradient->newPseudoImage($reflection->getImageWidth() + 10, $reflection->getImageHeight() + 10, "gradient:transparent-black");
$reflection->compositeImage($gradient, imagick::COMPOSITE_OVER, 0, 0);
$reflection->setImageOpacity( 0.3 );
$canvas = new Imagick();
$width = $im->getImageWidth() + 40;
$height = ($im->getImageHeight() * 2) + 30;
$canvas->newImage($width, $height, new ImagickPixel("black"));
$canvas->setImageFormat("png");
$canvas->compositeImage($im, imagick::COMPOSITE_OVER, 20, 10);
$canvas->compositeImage($reflection, imagick::COMPOSITE_OVER, 20, $im->getImageHeight() + 10);
header("Content-Type: image/png");
echo $canvas;
?>
This example illustrates how to use fill patterns during drawing.
Example #4 Filling text with gradient
<?php
$im = new Imagick();
$im->newPseudoImage(50, 50, "gradient:red-black");
$draw = new ImagickDraw();
$draw->pushPattern('gradient', 0, 0, 50, 50);
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);
$draw->popPattern();
$draw->setFillPatternURL('#gradient');
$draw->setFontSize(52);
$draw->annotation(20, 50, "Hello World!");
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");
$canvas->drawImage($draw);
$canvas->borderImage('black', 1, 1);
$canvas->setImageFormat('png');
header("Content-Type: image/png");
echo $canvas;
?>
There are no user contributed notes for this page.
|
|