PHP uses the Imagick module to implement image scaling, cropping, and compression examples _php tutorial

Source: Internet
Author: User
Tags imagemagick transparent color
PHP uses the Imagick module to scale, crop, compress pictures including GIF images

Zoom crop

Copy CodeThe code is as follows:
/**
* Picture Clipping
* Cropping rules:
* 1. Height is empty or zero by width scaling height adaptive
* 2. Width is empty or zero is adaptive by height scaling relaxation
* 3. width, height to not null, or zero by width-to-height scaling crop by default center-trimmed from head
* @param number $width
* @param number $height
*/
Public Function Resize ($width =0, $height =0) {
if ($width ==0 && $height ==0) {
Return
}

$color = ';//' Rgba (255,255,255,1) ';
$size = $this->image->getimagepage ();
Original width height
$SRC _width = $size [' width '];
$SRC _height = $size [' height '];

Scale height adaptive by width
if ($width!=0 && $height ==0) {
if ($src _width> $width) {
$height = Intval ($width * $src _height/$src _width);

if ($this->type = = = ' gif ') {
$this->_resizegif ($width, $height);
}else{
$this->image->thumbnailimage ($width, $height, true);
}
}
Return
}
Adaptive to height-scaling relaxation
if ($width ==0 && $height!=0) {
if ($src _height> $height) {
$width = intval ($src _width* $height/$src _height);

if ($this->type = = = ' gif ') {
$this->_resizegif ($width, $height);
}else{
$this->image->thumbnailimage ($width, $height, true);
}
}
Return
}

Size after scaling
$crop _w = $width;
$crop _h = $height;

Where to crop after zooming
$crop _x = 0;
$crop _y = 0;

if (($src _width/$src _height) < ($width/$height)) {
The ratio of the width to height is smaller than the target width and height ratio, and the magnification is intercepted from the head by target
$crop _h = intval ($src _height* $width/$src _width);
Crop from top without calculation $crop _y
}else{
The ratio of wide to high is greater than the target width and height proportional to the center clipping by the target width
$crop _w = intval ($src _width* $height/$src _height);
$crop _x = Intval (($crop _w-$width)/2);
}

if ($this->type = = = ' gif ') {
$this->_resizegif ($crop _w, $crop _h, True, $width, $height, $crop _x, $crop _y);
} else {
$this->image->thumbnailimage ($crop _w, $crop _h, True);
$this->image->cropimage ($width, $height, $crop _x, $crop _y);
}
}

How to handle GIF pictures

Copy CodeThe code is as follows:
/**
* Processing GIF images requires a picture processing for each frame
* @param unknown $t _w Relaxation
* @param unknown $t _h Zoom High
* @param string $isCrop whether to crop
* @param number $c _w clipping width
* @param number $c _h cut high
* @param number $c _x clipping coordinates x
* @param number $c _y crop coordinates y
*/
Private Function _resizegif ($t _w, $t _h, $isCrop =false, $c _w=0, $c _h=0, $c _x=0, $c _y=0) {
$dest = new Imagick ();
$color _transparent = new Imagickpixel ("Transparent"); Transparent Color
foreach ($this->image as $img) {
$page = $img->getimagepage ();
$tmp = new Imagick ();
$tmp->newimage ($page [' width '], $page [' height '], $color _transparent, ' gif ');
$tmp->compositeimage ($img, Imagick::composite_over, $page [' X '], $page [' Y ']);

$tmp->thumbnailimage ($t _w, $t _h, True);
if ($isCrop) {
$tmp->cropimage ($c _w, $c _h, $c _x, $c _y);
}

$dest->addimage ($tmp);
$dest->setimagepage ($tmp->getimagewidth (), $tmp->getimageheight (), 0, 0);
$dest->setimagedelay ($img->getimagedelay ());
$dest->setimagedispose ($img->getimagedispose ());

}
$this->image->destroy ();
$this->image = $dest;
}

Save-time Compression processing

Copy the Code code as follows:
Save to specified path
Public Function save_to ($path) {
Compress picture quality
$this->image->setimageformat (' JPEG ');
$this->image->setimagecompression (imagick::compression_jpeg);
$a = $this->image->getimagecompressionquality () * 0.60;
if ($a = = 0) {
$a = 60;
}
$this->image->setimagecompressionquality ($a);
$this->image->stripimage ();

if ($this->type = = = ' gif ') {
$this->image->writeimages ($path, true);
} else {
$this->image->writeimage ($path);
}
}

imagickservice.php

Copy the Code code as follows:

/**
* Image Processing Service class
* Implemented with PHP extension Service Imagick
* ImageMagick website address [Url]http:www.imagemagick.org/script/index.php[/url]
*
* @author Weiguang3
* @since 20140403
*/
Class Imagickservice {
Private $image = null;
Private $type = null;

constructor function
Public Function __construct () {
}

Destructors
Public Function __destruct () {
if ($this->image!== null)
$this->image->destroy ();
}

Public Function init () {

}

Loading images
Public function Open ($path) {
$this->image = new Imagick ($path);
if ($this->image) {
$this->type = Strtolower ($this->image->getimageformat ());
}
return $this->image;
}

/**
* Picture Clipping
* Cropping rules:
* 1. Height is empty or zero by width scaling height adaptive
* 2. Width is empty or zero is adaptive by height scaling relaxation
* 3. width, height to not null, or zero by width-to-height scaling crop by default center-trimmed from head
* @param number $width
* @param number $height
*/
Public Function Resize ($width =0, $height =0) {
if ($width ==0 && $height ==0) {
Return
}

$color = ';//' Rgba (255,255,255,1) ';
$size = $this->image->getimagepage ();
Original width height
$SRC _width = $size [' width '];
$SRC _height = $size [' height '];

Scale height adaptive by width
if ($width!=0 && $height ==0) {
if ($src _width> $width) {
$height = Intval ($width * $src _height/$src _width);

if ($this->type = = = ' gif ') {
$this->_resizegif ($width, $height);
}else{
$this->image->thumbnailimage ($width, $height, true);
}
}
Return
}
Adaptive to height-scaling relaxation
if ($width ==0 && $height!=0) {
if ($src _height> $height) {
$width = intval ($src _width* $height/$src _height);

if ($this->type = = = ' gif ') {
$this->_resizegif ($width, $height);
}else{
$this->image->thumbnailimage ($width, $height, true);
}
}
Return
}

Size after scaling
$crop _w = $width;
$crop _h = $height;

Where to crop after zooming
$crop _x = 0;
$crop _y = 0;

if (($src _width/$src _height) < ($width/$height)) {
The ratio of the width to height is smaller than the target width and height ratio, and the magnification is intercepted from the head by target
$crop _h = intval ($src _height* $width/$src _width);
Crop from top without calculation $crop _y
}else{
The ratio of wide to high is greater than the target width and height proportional to the center clipping by the target width
$crop _w = intval ($src _width* $height/$src _height);
$crop _x = Intval (($crop _w-$width)/2);
}

if ($this->type = = = ' gif ') {
$this->_resizegif ($crop _w, $crop _h, True, $width, $height, $crop _x, $crop _y);
} else {
$this->image->thumbnailimage ($crop _w, $crop _h, True);
$this->image->cropimage ($width, $height, $crop _x, $crop _y);
}
}

/**
* Processing GIF images requires a picture processing for each frame
* @param unknown $t _w Relaxation
* @param unknown $t _h Zoom High
* @param string $isCrop whether to crop
* @param number $c _w clipping width
* @param number $c _h cut high
* @param number $c _x clipping coordinates x
* @param number $c _y crop coordinates y
*/
Private Function _resizegif ($t _w, $t _h, $isCrop =false, $c _w=0, $c _h=0, $c _x=0, $c _y=0) {
$dest = new Imagick ();
$color _transparent = new Imagickpixel ("Transparent"); Transparent Color
foreach ($this->image as $img) {
$page = $img->getimagepage ();
$tmp = new Imagick ();
$tmp->newimage ($page [' width '], $page [' height '], $color _transparent, ' gif ');
$tmp->compositeimage ($img, Imagick::composite_over, $page [' X '], $page [' Y ']);

$tmp->thumbnailimage ($t _w, $t _h, True);
if ($isCrop) {
$tmp->cropimage ($c _w, $c _h, $c _x, $c _y);
}

$dest->addimage ($tmp);
$dest->setimagepage ($tmp->getimagewidth (), $tmp->getimageheight (), 0, 0);
$dest->setimagedelay ($img->getimagedelay ());
$dest->setimagedispose ($img->getimagedispose ());

}
$this->image->destroy ();
$this->image = $dest;
}


/**
* Change image size
* $fit: Fit to size mode
* ' Force ': Forces the image to form $width X $height size
* ' scale ': scales the picture proportionally in the safe box $width x $height, the image size is not exactly equal to $width x when the output is scaled $height
* ' Scale_fill ': Scale the picture proportionally in the safe box $width X $height, where there is no pixel in the security box, fill the color,
* Background fill color can be set when using this parameter $BG _color = Array (255,255,255) (red, green, blue, transparency)
* Transparency (0 opaque-127 fully transparent)) Other: Smart mode can scale the image and load the middle part of the image $width X $height pixel size
* $fit = ' force ', ' scale ', ' Scale_fill ' When: Output full image
* $fit = Image azimuth value, output the corresponding relationship between the image letter and the image in the specified position is as follows:
* North_west North North_east
* West Center East
* South_west South South_east
*/
Public Function resize_to ($width = $height, $fit = ' center ', $fill _color = Array (255,255,255,0)) {
Switch ($fit) {
Case ' Force ':
if ($this->type = = = ' gif ') {
$image = $this->image;
$canvas = new Imagick ();

$images = $image->coalesceimages ();
foreach ($images as $frame) {
$img = new Imagick ();
$img->readimageblob ($frame);
$img->thumbnailimage ($width, $height, false);

$canvas->addimage ($IMG);
$canvas->setimagedelay ($img->getimagedelay ());
}
$image->destroy ();
$this->image = $canvas;
} else {
$this->image->thumbnailimage ($width, $height, false);
}
Break
Case ' scale ':
if ($this->type = = = ' gif ') {
$image = $this->image;
$images = $image->coalesceimages ();
$canvas = new Imagick ();
foreach ($images as $frame) {
$img = new Imagick ();
$img->readimageblob ($frame);
$img->thumbnailimage ($width, $height, true);

$canvas->addimage ($IMG);
$canvas->setimagedelay ($img->getimagedelay ());
}
$image->destroy ();
$this->image = $canvas;
} else {
$this->image->thumbnailimage ($width, $height, true);
}
Break
Case ' Scale_fill ':
$size = $this->image->getimagepage ();
$SRC _width = $size [' width '];
$SRC _height = $size [' height '];

$x = 0;
$y = 0;

$DST _width = $width;
$DST _height = $height;

if ($src _width * $height > $SRC _height * $width) {
$DST _height = intval ($width * $src _height/$src _width);
$y = Intval (($height-$DST _height)/2);
} else {
$DST _width = intval ($height * $src _width/$src _height);
$x = Intval (($width-$DST _width)/2);
}

$image = $this->image;
$canvas = new Imagick ();

$color = ' Rgba ('. $fill _color [0]. ',' . $fill _color [1]. ',' . $fill _color [2]. ',' . $fill _color [3]. ')';
if ($this->type = = = ' gif ') {
$images = $image->coalesceimages ();
foreach ($images as $frame) {
$frame->thumbnailimage ($width, $height, true);

$draw = new Imagickdraw ();
$draw->composite ($frame->getimagecompose (), $x, $y, $dst _width, $dst _height, $frame);

$img = new Imagick ();
$img->newimage ($width, $height, $color, ' gif ');
$img->drawimage ($draw);

$canvas->addimage ($IMG);
$canvas->setimagedelay ($img->getimagedelay ());
$canvas->setimagepage ($width, $height, 0, 0);
}
} else {
$image->thumbnailimage ($width, $height, true);

$draw = new Imagickdraw ();
$draw->composite ($image->getimagecompose (), $x, $y, $dst _width, $dst _height, $image);

$canvas->newimage ($width, $height, $color, $this->get_type ());
$canvas->drawimage ($draw);
$canvas->setimagepage ($width, $height, 0, 0);
}
$image->destroy ();
$this->image = $canvas;
Break
Default:
$size = $this->image->getimagepage ();
$SRC _width = $size [' width '];
$SRC _height = $size [' height '];

$crop _x = 0;
$crop _y = 0;

$crop _w = $src _width;
$crop _h = $src _height;

if ($src _width * $height > $SRC _height * $width) {
$crop _w = intval ($src _height * $width/$height);
} else {
$crop _h = intval ($src _width * $height/$width);
}

Switch ($fit) {
Case ' north_west ':
$crop _x = 0;
$crop _y = 0;
Break
Case ' North ':
$crop _x = Intval (($src _width-$crop _w)/2);
$crop _y = 0;
Break
Case ' North_east ':
$crop _x = $src _width-$crop _w;
$crop _y = 0;
Break
Case ' West ':
$crop _x = 0;
$crop _y = Intval (($src _height-$crop _h)/2);
Break
Case ' center ':
$crop _x = Intval (($src _width-$crop _w)/2);
$crop _y = Intval (($src _height-$crop _h)/2);
Break
Case ' East ':
$crop _x = $src _width-$crop _w;
$crop _y = Intval (($src _height-$crop _h)/2);
Break
Case ' south_west ':
$crop _x = 0;
$crop _y = $src _height-$crop _h;
Break
Case ' South ':
$crop _x = Intval (($src _width-$crop _w)/2);
$crop _y = $src _height-$crop _h;
Break
Case ' South_east ':
$crop _x = $src _width-$crop _w;
$crop _y = $src _height-$crop _h;
Break
Default:
$crop _x = Intval (($src _width-$crop _w)/2);
$crop _y = Intval (($src _height-$crop _h)/2);
}

$image = $this->image;
$canvas = new Imagick ();

if ($this->type = = = ' gif ') {
$images = $image->coalesceimages ();
foreach ($images as $frame) {
$img = new Imagick ();
$img->readimageblob ($frame);
$img->cropimage ($crop _w, $crop _h, $crop _x, $crop _y);
$img->thumbnailimage ($width, $height, true);

$canvas->addimage ($IMG);
$canvas->setimagedelay ($img->getimagedelay ());
$canvas->setimagepage ($width, $height, 0, 0);
}
} else {
$image->cropimage ($crop _w, $crop _h, $crop _x, $crop _y);
$image->thumbnailimage ($width, $height, true);
$canvas->addimage ($image);
$canvas->setimagepage ($width, $height, 0, 0);
}
$image->destroy ();
$this->image = $canvas;
}
}

Add a watermark Picture
Public Function Add_watermark ($path, $x = 0, $y = 0) {
$watermark = new Imagick ($path);
$draw = new Imagickdraw ();
$draw->composite ($watermark->getimagecompose (), $x, $y, $watermark->getimagewidth (), $watermark Getimageheight (), $watermark);

if ($this->type = = = ' gif ') {
$image = $this->image;
$canvas = new Imagick ();
$images = $image->coalesceimages ();
foreach ($image as $frame) {
$img = new Imagick ();
$img->readimageblob ($frame);
$img->drawimage ($draw);

$canvas->addimage ($IMG);
$canvas->setimagedelay ($img->getimagedelay ());
}
$image->destroy ();
$this->image = $canvas;
} else {
$this->image->drawimage ($draw);
}
}

Add watermark Text
Public Function Add_text ($text, $x = 0, $y = 0, $angle = 0, $style = Array ()) {
$draw = new Imagickdraw ();
if (Isset ($style [' Font ']))
$draw->setfont ($style [' font ']);
if (Isset ($style [' font_size ']))
$draw->setfontsize ($style [' font_size ']);
if (Isset ($style [' Fill_color ']))
$draw->setfillcolor ($style [' Fill_color ']);
if (Isset ($style [' Under_color ']))
$draw->settextundercolor ($style [' Under_color ']);

if ($this->type = = = ' gif ') {
foreach ($this->image as $frame) {
$frame->annotateimage ($draw, $x, $y, $angle, $text);
}
} else {
$this->image->annotateimage ($draw, $x, $y, $angle, $text);
}
}

Save to specified path
Public Function save_to ($path) {
Compress picture quality
$this->image->setimageformat (' JPEG ');
$this->image->setimagecompression (imagick::compression_jpeg);
$a = $this->image->getimagecompressionquality () * 0.60;
if ($a = = 0) {
$a = 60;
}
$this->image->setimagecompressionquality ($a);
$this->image->stripimage ();

if ($this->type = = = ' gif ') {
$this->image->writeimages ($path, true);
} else {
$this->image->writeimage ($path);
}
}

Output image
Public function output ($header = True) {
if ($header)
Header (' Content-type: '. $this->type);
echo $this->image->getimagesblob ();
}
Public Function Get_width () {
$size = $this->image->getimagepage ();
return $size [' width '];
}
Public Function Get_height () {
$size = $this->image->getimagepage ();
return $size [' height '];
}

Set image type, default to Source type
Public Function Set_type ($type = ' png ') {
$this->type = $type;
$this->image->setimageformat ($type);
}

Get Source Image Type
Public Function Get_type () {
return $this->type;
}

Public Function get_file_size () {
if ($this->image) {
return 0;//$this->image->getimagelength (); Getimagelength not find
}else{
return 0;
}
}

Public Function Get_file_type () {
if ($this->image) {
return $this->image->getimagemimetype ();
}else{
return 0;
}
}

Public Function Get_sha1 () {
if ($this->image) {
Return SHA1 ($this->image->__tostring ());
}else{
Return ';
}
}

Whether the current object is a picture
Public Function Is_image () {
if ($this->image)
return true;
Else
return false;
}

/*
* Add a border $width: Left and Right border width $height: top and Bottom Border width $color: color: RGB color ' rgb (255,0,0) ' or 16 binary color ' #FF0000 ' or color word ' white '/' red ' ...
*/
Public function border ($width, $height, $color = ' rgb (220, 220, 220) ') {
$color = new Imagickpixel ();
$color->setcolor ($color);
$this->image->borderimage ($color, $width, $height);
}
Public function blur ($radius, $sigma) {
$this->image->blurimage ($radius, $sigma);
}//Blur
Public Function Gaussian_blur ($radius, $sigma) {
$this->image->gaussianblurimage ($radius, $sigma);
}//Gaussian Blur
Public Function Motion_blur ($radius, $sigma, $angle) {
$this->image->motionblurimage ($radius, $sigma, $angle);
}//Motion blur
Public Function Radial_blur ($radius) {
$this->image->radialblurimage ($radius);
}//Radial blur
Public Function add_noise ($type = null) {
$this->image->addnoiseimage ($type = = null? Imagick::noise_impulse: $type);
}//Add noise
Public Function level ($black _point, $gamma, $white _point) {
$this->image->levelimage ($black _point, $gamma, $white _point);
}//Adjust the color scale
Public function modulate ($brightness, $saturation, $hue) {
$this->image->modulateimage ($brightness, $saturation, $hue);
}//adjust brightness, saturation, hue
Public function Charcoal ($radius, $sigma) {
$this->image->charcoalimage ($radius, $sigma);
}//Sketch
Public Function Oil_paint ($radius) {
$this->image->oilpaintimage ($radius);
}//Oil painting effect
Public Function flop () {
$this->image->flopimage ();
}//Flip horizontally
Public Function Flip () {
$this->image->flipimage ();
}//Flip vertically
}

http://www.bkjia.com/PHPjc/755838.html www.bkjia.com true http://www.bkjia.com/PHPjc/755838.html techarticle PHP uses the Imagick module to scale, crop, compress pictures including GIF image zoom cropping copy code code as follows:/** * Picture cropping * cropping rule: * 1. Height is empty or zero by width ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.