Using Imagick in PHP to achieve various picture effect examples,
imagick is a powerful image processing library.
It is said to be a translation, but it is actually a brief introduction to the main functions of imagick, or an introduction to functions that I think are more practical, as well as examples of their use.
Because my English level is limited, I use a more popular or more practical language to describe it.
First enjoy a set of dazzling effects:
Offset image:
Example:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->rollImage(20,39);
echo $image;
?>
thumbnailImage($width,$height) changes the image size
Example:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->thumbnailImage(100,0);
echo $image;
?>
addNoiseImage(int $noise_type [, int $channel= Imagick::CHANNEL_ALL ]);
Function:
Adds random noise to the image
Add interferon
Copy code The code is as follows:
Noise constants ($noise_type type)
imagick::NOISE_UNIFORM (integer)
imagick::NOISE_GAUSSIAN (integer)
imagick::NOISE_MULTIPLICATIVEGAUSSIAN (integer)
imagick::NOISE_IMPULSE (integer)
imagick::NOISE_LAPLACIAN (integer)
imagick::NOISE_POISSON (integer)
Channel constants ($channel type)
imagick::CHANNEL_UNDEFINED (integer)
imagick::CHANNEL_RED (integer)
imagick::CHANNEL_GRAY (integer)
imagick::CHANNEL_CYAN (integer)
imagick::CHANNEL_GREEN (integer)
imagick::CHANNEL_MAGENTA (integer)
imagick::CHANNEL_BLUE (integer)
imagick::CHANNEL_YELLOW (integer)
imagick::CHANNEL_ALPHA (integer)
imagick::CHANNEL_OPACITY (integer)
imagick::CHANNEL_MATTE (integer)
imagick::CHANNEL_BLACK (integer)
imagick::CHANNEL_INDEX (integer)
imagick::CHANNEL_ALL (integer)
Example:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->thumbnailImage(100,0);
$image->addNoiseImage(imagick::NOISE_POISSON,imagick::CHANNEL_OPACITY);
echo $image;
?>
annotateImage creates a text image
Example:
Copy code The code is as follows:
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );
$image->newImage(800, 75, $pixel);
$pixel->setColor('black');
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 30 );
$image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog');
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
?>
blurImage(float $radius , float $sigma [, int $channel ])
Adds blur filter to image Image blur processing
Parameters:
Copy code The code is as follows:
int $channel :
imagick::CHANNEL_UNDEFINED (integer)
imagick::CHANNEL_RED (integer)
imagick::CHANNEL_GRAY (integer)
imagick::CHANNEL_CYAN (integer)
imagick::CHANNEL_GREEN (integer)
imagick::CHANNEL_MAGENTA (integer)
imagick::CHANNEL_BLUE (integer)
imagick::CHANNEL_YELLOW (integer)
imagick::CHANNEL_ALPHA (integer)
imagick::CHANNEL_OPACITY (integer)
imagick::CHANNEL_MATTE (integer)
imagick::CHANNEL_BLACK (integer)
imagick::CHANNEL_INDEX (integer)
imagick::CHANNEL_ALL (integer)
复制代码 代码如下:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->blurImage(5,3);
echo $image;
?>
borderImage ( mixed $bordercolor , int $width , int $height ) 图片边框处理
例子:
复制代码 代码如下:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->borderImage($color,5,4);
$image->blurImage(5,5,imagick::CHANNEL_GREEN);
echo $image;
?>
charcoalImage ( float $radius , float $sigma ) 图像素描处理
参数说明:
$radius :越小越薄。
$sigma: 越大 墨越深 反之。
例子:
复制代码 代码如下:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->borderImage($color,5,4);
$image->charcoalImage(0.0001,0.001);
//$image->blurImage(5,5,imagick::CHANNEL_GREEN);
echo $image;
?>
chopImage ( int $width , int $height , int $x , int $y )
参数说明:删除一定范围的图像区域
就不做参数说明,一看便知.
colorizeImage( mixed $colorize , mixed $opacity )混合填充颜色
$colorize 颜色
$opacit 透明度
例子:
复制代码 代码如下:
/*
胶卷底片效果
*/
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->negateImage(false);
$image->colorizeImage('#000',1.0);
echo $image;
?>
embossImage ( float $radius , float $sigma )
功能: 返回一个灰度级3D图像 不太好。
例子:
复制代码 代码如下:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->embossImage(1,1);
echo $image;
?>
(两张效果图)
flipImage(void)
功能: 创建图像倒影(垂直翻转)
例子:
复制代码 代码如下:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->flipImage();
echo $image;
?>
flopImage ( void )
功能: 图像水平横向翻转
复制代码 代码如下:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->flopImage();
echo $image;
?>
frameImage(mixed $matte_color,int $width, int $height,int $inner_bevel, int $outer_bevel)
Function: Create 3D image borders
Parameter description:
$matte_color: color
$inner_bevel: inner slope of the border
$outer_bevel: outer border slope
Example:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->frameImage($color,11,11,1,10);
echo $image;
?>
Note:
$width (width) cannot be less than $inner_bevel (internal slope of the border)
Imagick::gammaImage (float $gamma [,int $channel= Imagick::CHANNEL_ALL])
Function: Adjust image gamma
Parameter description:
float $gamma: gamma coefficient value
$channel defaults to Imagick::CHANNEL_ALL
Imagick::CHANNEL_ALL
Example 1:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->gammaImage(30);
echo $image;
?>
Example 2:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->gammaImage(30);
echo $image;
?>
gaussianBlurImage ( float $radius , float $sigma [, int $channel= Imagick::CHANNEL_ALL ] )
Function: Gaussian blur processing similar to photo’s Gaussian blur
Parameter description:
float $radius: Radius of Gaussian blur, in pixels, excluding the center pixel.
float $sigma: Standard deviation of the Gaussian, in pixels. I think this parameter is the most important.
int $channel: image color mode.
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->gaussianBlurImage(30,3);
echo $image;
?>
levelImage ( float $blackPoint , float $gamma , float $whitePoint [, int $channel= Imagick::CHANNEL_ALL ] )
Function: Adjusts the levels of an image
Parameter Description
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->levelImage(4,4,4);
echo $image;
?>
Example 2:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->levelImage(200,200,200,imagick::CHANNEL_GREEN);
echo $image;
?>
magnifyImage( void )
Function description: Is a convenience method that scales an image proportionally to twice its original size. (Is a convenience method that scales an image proportionally to twice its original size. )
Example:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->magnifyImage ();
echo $image;
?>
medianFilterImage ( float $radius )
Function: Special filter, a bit like photoshop palette knife filter
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->medianFilterImage(5);
echo $image;
?>
minifyImage(void)
Function: Scale an image proportionally to half its size (Scales an image proportionally to half its size)
Example:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->minifyImage();
echo $image;
?>
modulateImage ( float $brightness , float $saturation , float $hue )
Function: Control and adjust the brightness, saturation, and hue of the image.
Parameter description:
float $brightness: brightness
float $saturation: saturation
float $hue hue
Example 1:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->modulateImage(100,1,100);
echo $image;
?>
Example 2:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$image->modulateImage(250,1,250);
echo $image;
?>
motionBlurImagemotionBlurImage ( float $radius , float $sigma , float $angle [, int $channel= Imagick::CHANNEL_DEFAULT ] )
Function: Simulates motion blur, a motion blur filter function similar to photoshop
Parameter description:
float $radius: Gaussian radius, not including the center pixel.
float $sigma: Standard deviation of Gaussian, in pixels. 【Important parameters】
float $angle: blur angle.
int $channel: image color mode. Default is Imagick::CHANNEL_DEFAULT
Example 1:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->motionBlurImage (61,10,10);
echo $image;
?>
Example 2:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->motionBlurImage (201,10,100);
echo $image;
?>
oilPaintImage (float $radius):
Function description: Simulates an oil painting filter (Simulates an oil painting)
Example:
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->oilPaintImage(1);
echo $image;
?>
radialBlurImage ( float $angle [, int $channel= Imagick::CHANNEL_ALL ] )
Function: Radial blurs (Radial blurs an image)
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->radialBlurImage(30);
echo $image;
?>
raiseImage ( int $width , int $height , int $x , int $y , bool $raise )
Function description: Creates a simulated 3d button-like effect (Creates a simulated 3d button-like effect)
Copy code The code is as follows:
ini_set('display_errors',1);
header('Content-type: image/jpeg');
$image = new Imagick('1.jpg');
$color=new ImagickPixel();
$color->setColor("rgb(220,220,220)");
$image->raiseImage(10,10,3,5,6);
echo $image;
?>
Attachment 1: It is required to reduce the image size, add a translucent border, read the exif information, and display it on the image according to the specified requirements
Copy code The code is as follows:
$towidth = '500';
$toheight = '700'; //Set the maximum width and height allowed when resizing the image
$sourcefile = './b.jpg'; //Define an image file path
//$image->writeImage('./b.jpg.bak'); //You can back up this image
$myimage = new Imagick( $sourcefile ); //Read the image file
$exifobject = my_exif( $myimage ); //Self-written function, read the exif information (shooting data), arrange the exif information according to your own requirements, and return the object
//$myimage->setImageFormat('jpeg'); //Convert the image to jpg format
$myimage->setCompressionQuality( 100 ); //Set jpg compression quality, 1 - 100
$myimage->enhanceImage(); //Remove noise
$sourcewidth = $myimage->getImageWidth(); //Get the original size of the read image
if ( $sourcewidth > $towidth )
{
$myimage->scaleImage( $towidth, $toheight, true ); //Adjust image size
}
$myimage->raiseImage(8, 8, 0, 0, 1); //Add translucent border
$resizewidth = $myimage->getImageWidth(); //Read the adjusted image size
$resizeheight = $myimage->getImageHeight();
$drawback = new ImagickDraw(); //Instantiate a drawing object and draw a translucent black background for exif information
$drawback->setFillColor( new ImagickPixel('#000000') ); //Set the fill color to black
$drawback->setFillOpacity(0.6); //Fill transparency is 0.6, parameter 0.1-1, 1 is opaque
$drawback->rectangle( $resizewidth / 2 - 190, $resizeheight - 50, $resizewidth / 2 + 190, $resizeheight - 12 ); //Draw rectangle parameters, respectively upper left corner x, y, lower right corner x, y
$myimage->drawImage( $drawback ); //Confirm to draw the rectangular frame in the image
$draw = new ImagickDraw(); //Instantiate a drawing object, draw exif text information and embed it into the picture
$draw->setFont( './xianhei.ttf' ); //Set text font, requiring ttf or ttc font, absolute or relative path
$draw->setFontSize( 11 ); //Set font size
$draw->setTextAlignment(2); //Text alignment, 2 is centered
$draw->setFillColor( '#FFFFFF' ); //Text fill color
$myimage->annotateImage( $draw, $resizewidth / 2, $resizeheight - 39, 0, $exifobject->row1 ); //Draw the first line of text, centered
$myimage->annotateImage( $draw, $resizewidth / 2, $resizeheight - 27, 0, $exifobject->row2 ); //Draw the second line of text, centered
$myimage->annotateImage( $draw, $resizewidth / 2, $resizeheight - 15, 0, $exifobject->row3 ); //Draw the third line of text, centered
/* Output the image with headers */
header( 'Content-type: image/jpeg' ); //php file output mime type is jpeg image
echo $myimage; //Output the image on the current php page
//$image->writeImage('./b.new.jpg'); //If the image does not need to be output in the current php program, use the write image to disk function, and the above setting header can also be removed
$myimage->clear();
$myimage->destroy(); //Release resources
//Self-written function, read exif information and return object
function my_exif( $myimage )
{
$exifArray = array( 'exif:Model' => 'Unknown', 'exif:DateTimeOriginal' => 'Unknown', 'exif:ExposureProgram' => 'Unknown', 'exif:FNumber' => ' 0/10', 'exif:ExposureTime' => '0/10', 'exif:ISOSpeedRatings' => 'Unknown',
'exif:MeteringMode' => 'Unknown', 'exif:Flash' => 'Turn off flash', 'exif:FocalLength' => 'Unknown', 'exif:ExifImageWidth' => 'Unknown', ' exif:ExifImageLength' => 'Unknown' ); //Initialize some information to prevent operation errors when the photo exif information cannot be read
$exifArray = $myimage->getImageProperties( "exif:*" ); //Read the exif information of the image and store it in the $exifArray array
//If you need to display the original array, you can use print_r($exifArray);
$r->row1 = 'Camera:' . $exifArray['exif:Model'];
$r->row1 = $r->row1 . 'Shooting time:' . $exifArray['exif:DateTimeOriginal'];
switch ( $exifArray['exif:ExposureProgram'] )
{
case 1:
$exifArray['exif:ExposureProgram'] = "Manual(M)";
Break; //Manual Control
case 2:
$exifArray['exif:ExposureProgram'] = "Program automatic (P)";
Break; //Program Normal
case 3:
$exifArray['exif:ExposureProgram'] = "Aperture Priority (A,Av)";
Break; //Aperture Priority
case 4:
$exifArray['exif:ExposureProgram'] = "Shutter Priority (S,Tv)";
Break; //Shutter Priority
case 5:
$exifArray['exif:ExposureProgram'] = "Slow Shutter";
Break; //Program Creative (Slow Program)
case 6:
$exifArray['exif:ExposureProgram'] = "Exercise Mode";
Break; //Program Action(High-Speed Program)
case 7:
$exifArray['exif:ExposureProgram'] = "Portrait";
Break; //Portrait
case 8:
$exifArray['exif:ExposureProgram'] = "Landscape";
Break; //Landscape
default:
$exifArray['exif:ExposureProgram'] = "Other";
}
$r->row1 = $r->row1 . 'Mode:' . $exifArray['exif:ExposureProgram'];
$exifArray['exif:FNumber'] = explode( '/', $exifArray['exif:FNumber'] );
$exifArray['exif:FNumber'] = $exifArray['exif:FNumber'][0] / $exifArray['exif:FNumber'][1];
$r->row2 = 'Aperture:F/' . $exifArray['exif:FNumber'];
$exifArray['exif:ExposureTime'] = explode( '/', $exifArray['exif:ExposureTime'] );
if ( ($exifArray['exif:ExposureTime'][0] / $exifArray['exif:ExposureTime'][1]) >= 1 )
{
$exifArray['exif:ExposureTime'] = sprintf( "%.1fs", (float)$exifArray['exif:ExposureTime'][0] / $exifArray['exif:ExposureTime'][1] );
} else
{
$exifArray['exif:ExposureTime'] = sprintf( "1/%ds", $exifArray['exif:ExposureTime'][1] / $exifArray['exif:ExposureTime'][0] );
}
$r->row2 = $r->row2 . 'Shutter:' . $exifArray['exif:ExposureTime'];
$r->row2 = $r->row2 . ' ISO:' . $exifArray['exif:ISOSpeedRatings'];
$exifArray['exif:ExposureBiasValue'] = explode( "/", $exifArray['exif:ExposureBiasValue'] );
$exifArray['exif:ExposureBiasValue'] = sprintf( "%1.1feV", ((float)$exifArray['exif:ExposureBiasValue'][0] / $exifArray['exif:ExposureBiasValue'][1] * 100) / 100 );
if ( (float)$exifArray['exif:ExposureBiasValue'] > 0 )
{
$exifArray['exif:ExposureBiasValue'] = "+" . $exifArray['exif:ExposureBiasValue'];
}
$r->row2 = $r->row2 . 'Compensation:' . $exifArray['exif:ExposureBiasValue'];
switch ( $exifArray['exif:MeteringMode'] )
{
case 0:
$exifArray['exif:MeteringMode'] = "Unknown";
Break;
case 1:
$exifArray['exif:MeteringMode'] = "Matrix";
Break;
case 2:
$exifArray['exif:MeteringMode'] = "Center-weighted averaging";
Break;
case 3:
$exifArray['exif:MeteringMode'] = "Spot Metering";
Break;
case 4:
$exifArray['exif:MeteringMode'] = "Multi-point metering";
Break;
default:
$exifArray['exif:MeteringMode'] = "Other";
}
$r->row2 = $r->row2 . 'Metering:' . $exifArray['exif:MeteringMode'];
switch ( $exifArray['exif:Flash'] )
{
case 1:
$exifArray['exif:Flash'] = "Turn on flash";
Break;
}
$r->row2 = $r->row2 . ' ' . $exifArray['exif:Flash'];
if ( $exifArray['exif:FocalLengthIn35mmFilm'] )
{
$r->row3 = 'Equivalent focal length:' . $exifArray['exif:FocalLengthIn35mmFilm'] . "mm";
} else
{
$exifArray['exif:FocalLength'] = explode( "/", $exifArray['exif:FocalLength'] );
$exifArray['exif:FocalLength'] = sprintf( "%4.1fmm", (double)$exifArray['exif:FocalLength'][0] / $exifArray['exif:FocalLength'][1] );
$r->row3 = 'Focal length:' . $exifArray['exif:FocalLength'];
}
$r->row3 = $r->row3 . 'Original pixel:' . $exifArray['exif:ExifImageWidth'] . 'x' . $exifArray['exif:ExifImageLength'] . 'px';
if ( $exifArray['exif:Software'] )
{
$r->row3 = $r->row3 . ' Later:' . $exifArray['exif:Software'];
}
Return $r;
}
Appendix 2: Processing image watermarks
Copy code The code is as follows:
//Get watermark image
$logo = new Imagick("logo.png");
$logo->setImageResolution(0.01,0.03);
//Create an Imagick object and obtain the source image to be processed
$im = new Imagick( "old_large_img_2.jpg" );
//Get the width and height of the source image
$srcWH = $im->getImageGeometry();
//Scale the image proportionally to the width and height settings
if($srcWH['width']>710){
$srcW['width'] = 710;
$srcH['height'] = $srcW['width']/$srcWH['width']*$srcWH['height'];
}else{
$srcW['width'] = $srcWH['width'];
$srcH['height'] = $srcWH['height'];
}
//Scale according to the proportion
$im->thumbnailImage( $srcW['width'], $srcH['height'], true );
//Create a colored image according to the thumbnail size
$canvas = new Imagick();
$canvas->newImage( $srcW['width'], $srcH['height'], 'black', 'jpg' ); //pink,black
//Add watermark
$im->compositeImage($logo,Imagick::COMPOSITE_OVER,$srcW['width']-280,$srcH['height']-77);
$canvas->setcompressionquality(91);
//Merge images
$canvas->compositeImage( $im, imagick::COMPOSITE_OVER, 0, 0);
//Output picture
header( "Content-Type: image/jpg" );
echo $canvas;
//Generate pictures
$canvas->writeImage( "test_img/old_large_img_2_96.jpg" );
?>
http://www.bkjia.com/PHPjc/945708.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/945708.htmlTechArticleImagick is used to achieve various image effect examples in PHP. imagick is a powerful image processing library. It's a translation, but it's actually a brief introduction to the main functions of imagick, or I think...