文字

Basic usage

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' );

// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$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
$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 ;
?>

以上例程的输出类似于:

Output of example : Creating a reflection of an image

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 ;
?>

以上例程的输出类似于:

Output of example : Filling text with gradient

Working with animated GIF images

Example #5 Read in GIF image and resize all frames

<?php


$im  = new  Imagick ( "example.gif" );


foreach ( $im  as  $frame ) {
    

    
$frame -> thumbnailImage ( 50 50 );

    

    
$frame -> setImagePage ( 50 50 0 0 );
}


$im -> writeImages ( "example_small.gif" true );
?>

Working with ellipse primitive and custom fonts

Example #6 Create a PHP logo

<?php

$width  400 ;
$height  210 ;


$img  = new  Imagick ();
$img -> newImage ( $width $height , new  ImagickPixel ( 'transparent' ));


$draw  = new  ImagickDraw ();

$draw -> setFillColor ( '#777bb4' );

$draw -> ellipse ( $width  2 $height  2 $width  2 $height  2 0 360 );

$img -> drawImage ( $draw );


$draw -> setFillColor ( 'black' );

$draw -> setStrokeColor ( 'white' );

$draw -> setStrokeWidth ( 2 );

$draw -> setTextKerning (- 8 );

$draw -> setFont ( 'Handel Gothic.ttf' );
$draw -> setFontSize ( 150 );

$draw -> setGravity ( Imagick :: GRAVITY_CENTER );


$img -> annotateImage ( $draw 0 , - 10 0 'php' );
$img -> setImageFormat ( 'png' );


header ( 'Content-Type: image/png' );
echo 
$img ;
?>

以上例程的输出类似于:

Output of example : Creating PHP logo with Imagick

用户评论:

[#1] vokseli [2014-08-06 07:24:49]

Be careful when loading multiple images by passing an array to a new Imagick object. This is from Example #2:

<?php

$images 
= new Imagick(glob('images
$reflection = $im->clone();
$reflection->flipImage();
----------------------------------------------------
it was using the Imagick::clone function

This function has been DEPRECATED as of imagick 3.1.0 in favour of using the clone keyword.

use below code instead:
----------------------------------------------------

$reflection = clone $im;
$reflection->flipImage();
----------------------------------------------------

http://php.net/manual/en/imagick.clone.php

上一篇: 下一篇: