DJ Mike's Tutorials: PHP

ImageMagick Functions

^

Imagick::distortImage

Description:
bool Imagick::distortImage ( int $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.

This method is available if Imagick has been compiled against ImageMagick version 6.3.6 or newer.

Parameters:
method
The method of image distortion.
  • imagick::DISTORTION_UNDEFINED
  • imagick::DISTORTION_AFFINE
  • imagick::DISTORTION_AFFINEPROJECTION
  • imagick::DISTORTION_ARC
  • imagick::DISTORTION_BILINEAR
  • imagick::DISTORTION_PERSPECTIVE
  • imagick::DISTORTION_PERSPECTIVEPROJECTION
  • imagick::DISTORTION_SCALEROTATETRANSLATE
arguments
An array of arguments for this distortion method. The number of members in the array depend on the distortion method.
Affine
As near as I can figure, the number of members in the array has 4, 8, 12, 16, 20, 36... members. A four member array will do nothing. An eight member will shrink the image. Affine transformation
Affine Projection
Takes six arguments but I don't Know what they do.
Arc
Up to four members: arc_angle rotate_angle top_radius bottom_radius
Bilinear
Eight members which represent the starting corners and ending corners when the image is distorted. The image is distorted similarly to perspective but the sides are curver instead or straight lines
Perspective
Eight members which represent the starting corners and ending corners when the image is distorted into a quadrilateral.
Scale Rotate Translate
Supposedly this will accept up to seven arguments which will allow you to rotate the image around any given point, scale the image and then move it. For me it always rotated around the center and didn't move. The examples at ImageMagick use command line syntax
Perspective Projection
A mystery function. Accepts eight arguments. No telling what they do. The Imagick web site is no help at all.
bestfit
Attempt to resize destination to fit distorted source
Return Values
Returns TRUE on success.
Errors/Exceptions
Throws ImagickException on error.

Example



Affine


<?php 
$image 
= new imagick"opossum.jpg" ); 
$points = array( 
                
0,025,25,   
               
100,0100,50 
               
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethodimagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage(  Imagick::DISTORTION_AFFINE$pointsTRUE ); 
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->setImageVirtualPixelMethodimagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImageImagick::DISTORTION_AFFINEPROJECTION$pointsTRUE ); 
header"Content-Type: image/jpeg" ); 
echo 
$image;
?>

Arc


<?php 
$image 
= new imagick"opossum.jpg" ); 
$draw = new imagickdraw(); 
$degrees = array( 180 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethodimagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImageImagick::DISTORTION_ARC$degreesTRUE ); 
header"Content-Type: image/jpeg" ); 
echo 
$image;
?>

<?php 
$image 
= new imagick"opossum.jpg" ); 
$draw = new imagickdraw(); 
$degrees = array( 1804510020 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethodimagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImageImagick::DISTORTION_ARC$degreesTRUE ); 
header"Content-Type: image/jpeg" ); 
echo 
$image;
?>

Bilinear


<?php 
$image 
= new imagick"opossum.jpg" ); 
$points = array( 
                
0,025,25# top left  
                
176,0126,0# top right
                
0,1350,105# bottom right 
                
176,135176,135 # bottum left
                
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethodimagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImageImagick::DISTORTION_BILINEAR$pointsTRUE ); 
header"Content-Type: image/jpeg" ); 
echo 
$image;
?>

Perspective


<?php 
$image 
= new imagick"opossum.jpg" ); 
$points = array( 
                
0,025,25# top left  
                
176,0126,0# top right
                
0,1350,105# bottom right 
                
176,135176,135 # bottum left
                
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethodimagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImageImagick::DISTORTION_PERSPECTIVE$pointsTRUE ); 
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->setImageVirtualPixelMethodimagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImageimagick::DISTORTION_SCALEROTATETRANSLATE$pointsTRUE ); 
header"Content-Type: image/jpeg" ); 
echo 
$image;
?>

Scale Rotate Translate


<?php 
$image 
= new imagick"opossum.jpg" ); 
$points = array( 
               
100,
               
10# rotation?
               
210# width?
               
501# ?  
               
100#
               
200# height
               
50,
               
1100
               
);
$image->distortImage(
                                    
Imagick::DISTORTION_PERSPECTIVEPROJECTION,
                                    
$pointsTRUE ); 
header"Content-Type: image/jpeg" ); 
echo 
$image;
?>


^


Created by DJ Mike from Santa Barbara

DJ Mike


Dance Away Santa Barbara's Home Page
<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>