©
本文檔使用 php中文網手册 發布
(PECL imagick 2.0.1)
Imagick::distortImage — Distorts an image using various distortion methods
$method
, array $arguments
, bool $bestfit
)
Distorts an image using various distortion methods, by mapping color
lookups of the source image to a new destination image usually of the same
size as the source image, unless 'bestfit' is set to TRUE
.
If 'bestfit' is enabled, and distortion allows it, the destination image is adjusted to ensure the whole source 'image' will just fit within the final destination image, which will be sized and offset accordingly. Also in many cases the virtual offset of the source image will be taken into account in the mapping.
此方法在Imagick基于ImageMagick 6.3.6以上版本编译时可用。
method
The method of image distortion. See distortion constants
arguments
The arguments for this distortion method
bestfit
Attempt to resize destination to fit distorted source
成功时返回 TRUE
。
错误时抛出 ImagickException。
Example #1 Using Imagick::distortImage() :
Distort an image and display to the browser.
<?php
$im = new Imagick ();
$im -> newPseudoImage ( 100 , 100 , "pattern:checkerboard" );
$im -> setImageFormat ( 'png' );
$im -> setImageVirtualPixelMethod ( Imagick :: VIRTUALPIXELMETHOD_TRANSPARENT );
$im -> setImageMatte ( true );
$controlPoints = array( 10 , 10 ,
10 , 5 ,
10 , $im -> getImageHeight () - 20 ,
10 , $im -> getImageHeight () - 5 ,
$im -> getImageWidth () - 10 , 10 ,
$im -> getImageWidth () - 10 , 20 ,
$im -> getImageWidth () - 10 , $im -> getImageHeight () - 10 ,
$im -> getImageWidth () - 10 , $im -> getImageHeight () - 30 );
$im -> distortImage ( Imagick :: DISTORTION_PERSPECTIVE , $controlPoints , true );
header ( "Content-Type: image/png" );
echo $im ;
?>
以上例程的输出类似于:
[#1] peter dot e dot lind at gmail dot com [2014-12-30 08:55:08]
It is worth nothing that details on distorts as well as arguments can be found at http://www.imagemagick.org/Usage/distorts/ - the PHP manual doesn't give much, if any, detail on arguments.
I found this very useful when I had to do a translated rotation on an image - i.e. rotating around a point in the image that was not square in the center. This can be achieved by using distortImage with Imagick::DISTORTION_SCALEROTATETRANSLATE like this:
<?php
$imagick = new Imagick('/path/to/image');
$args = array(
20, # x point to rotate around
20, # y point to rotate around
1, # scaling factor - 1 means no scaling
90, # angle to rotate
);
$imagick->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$imagick->distortImage(Imagick::DISTORTION_SCALEROTATETRANSLATE, $args, true);
?>
Note that this type of distort takes from 1 to 7 arguments, depending on what you want to do exactly - there is a lot of flexibility here.
[#2] DJ Mike [2011-04-11 23:11:07]
Affine
<?php
$image = new imagick( "opossum.jpg" );
$points = array(
0,0, 25,25,
100,0, 100,50
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_AFFINE, $points, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;
?>
Affine Projection
<?php
$image = new imagick( "opossum.jpg" );
$points = array( 0.9,0.3,
-0.2,0.7,
20,15 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_AFFINEPROJECTION, $points, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;
?>
Arc
<?php
$image = new imagick( "opossum.jpg" );
$draw = new imagickdraw();
$degrees = array( 180 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_ARC, $degrees, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;
?>
Rotated Arc
<?php
$image = new imagick( "opossum.jpg" );
$draw = new imagickdraw();
$degrees = array( 180, 45, 100, 20 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_ARC, $degrees, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;
?>
Bilinear
<?php
$image = new imagick( "opossum.jpg" );
$points = array(
0,0, 25,25, # top left
176,0, 126,0, # top right
0,135, 0,105, # bottom right
176,135, 176,135 # bottum left
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_BILINEAR, $points, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;
?>
Perspective
<?php
$image = new imagick( "opossum.jpg" );
$points = array(
0,0, 25,25, # top left
176,0, 126,0, # top right
0,135, 0,105, # bottom right
176,135, 176,135 # bottum left
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_PERSPECTIVE, $points, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;
?>
Scale Rotate Translate
<?php
$image = new imagick( "opossum.jpg" );
$points = array(
1.5, # scale 150%
150 # rotate
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( imagick::DISTORTION_SCALEROTATETRANSLATE, $points, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;
?>
[#3] ashokmca.g at gmail dot com [2011-02-04 02:13:33]
Slide image with shadow using distortImage
<?php
$slideValue = 150;
// Create new object
$im = new Imagick("grnhrs.jpg");
// Resize
$im->thumbnailImage(500,400);
// Set the image format to png
$im->setImageFormat('png');
//Clone the current object
$shadow = $im->clone();
//Set image background color to black (this is the color of the shadow)
$shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
//Create the shadow
$shadow->shadowImage( 80, 10, 5, 5 );
// Fill background area with transparent for image
//VIRTUALPIXELMETHOD_TRANSPARENT
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_ TRANSPARENT);
// Activate matte
$im->setImageMatte(true);
//Control points for the distortion
$controlPoints = array( 0, 0,
$slideValue, 0,
0, $im->getImageHeight(),
0, $im->getImageHeight(),
$im->getImageWidth(), 0,
$im->getImageWidth(), 0,
$im->getImageWidth(), $im->getImageHeight(),
$im->getImageWidth()-$slideValue, $im->getImageHeight());
// Perform the distortion
$im->distortImage(Imagick::DISTORTION_PERSPECTIVEPROJECTION, $controlPoints, true);
// Perform the distortion in shadow image
$shadow->distortImage(Imagick::DISTORTION_PERSPECTIVEPROJECTION, $controlPoints, true);
// Imagick::shadowImage only creates the shadow.
// That is why the original image is composited over it
$shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );
header("Content-Type: image/png");
echo $shadow;
?>