PHP image processing class implementation code. Copy the code as follows :? Php *** author: yagas * email: yagas60@21cn.com * classImage {** class protection variable * protected $ th_width100; protected $ th_height50; protected
The code is as follows:
/**
* Author: yagas
* Email: yagas60@21cn.com
*/
Class Image
{
/** Class protection variable */
Protected $ th_width = 100;
Protected $ th_height = 50;
Protected $ quality = 85; // Image quality
Protected $ transparent = 50; // watermark transparency
Protected $ background = "255,255,255"; // background color
/**
* Generate a thumbnail file
* @ Param $ src source image file
* @ Param $ dst target file
*/
Public function thumb ($ src, $ dst = null, $ output = true)
{
$ Thumb = array ($ this-> th_width, $ this-> th_height );
$ This-> scale ($ src, $ thumb, $ dst, $ output );
}
/**
* Scale the image by percentage
* @ Param string $ src source image file
* @ Param string $ destination file entered by dst
* @ Param float/array $ zoom scaling ratio. when the floating point type is set to floating point, the float will bloom as a percentage. when the array type is set to array, the row will zoom as specified.
* @ Param boolean $ output whether to generate file output
*/
Public function scale ($ src, $ dst = null, $ zoom = 1, $ output = true)
{
If (! File_exists ($ src) die ('file not exists .');
If (! $ Zoom) die ('The zoom undefine .');
$ Src_im = $ this-> IM ($ src );
$ Old_width = imagesx ($ src_im );
If (is_float ($ zoom )){
// Scale by percentage
$ New_width = $ old_width * $ zoom;
}
Elseif (is_array ($ zoom )){
// Clear scaling size
$ New_width = $ zoom [0];
}
// Whether to define the zoom height
If (! Isset ($ zoom [1]) {
// Proportional scaling
$ Resize_im = $ this-> imageresize ($ src_im, $ new_width );
}
Else {
// Non-proportional scaling
$ Resize_im = $ this-> imageresize ($ src_im, $ new_width, $ zoom [1]);
}
If (! $ Output ){
Header ("Content-type: image/jpeg ");
Imagejpeg ($ resize_im, null, $ this-> quality );
}
Else {
$ New_file = empty ($ dst )? $ Src: $ dst;
Imagejpeg ($ resize_im, $ new_file, $ this-> quality );
}
Imagedestroy ($ im );
Imagedestroy ($ nIm );
}
/**
* Cropping images
* @ Param $ original src file
* @ Param $ dst target file
* @ Param $ output: whether to generate the target file
*/
Public function capture ($ src, $ dst = null, $ output = true ){
If (! File_exists ($ src) die ('file not exists .');
$ Width = $ this-> th_width;
$ Height = $ this-> th_height;
$ Src_im = $ this-> IM ($ src );
$ Old_width = imagesx ($ src_im );
$ Old_height = imagesy ($ src_im );
$ Capture = imagecreatetruecolor ($ width, $ height );
$ Rgb = explode (",", $ this-> background );
$ White = imagecolorallocate ($ capture, $ rgb [0], $ rgb [1], $ rgb [2]);
Imagefill ($ capture, 0, 0, $ white );
// Zoom when the image is larger than the thumbnail
If ($ old_width> $ width & $ old_height> $ height ){
$ Resize_im = $ this-> imageresize ($ src_im, $ width );
// Re-calculate the proportion for cropping when the image proportion is not standard
If (imagesy ($ resize_im) <$ height ){
$ Proportion = $ old_height/$ this-> th_height;
$ Resize_im = $ this-> imageresize ($ src_im, $ old_width/$ proportion );
}
$ Posx = 0;
$ Posy = 0;
}
Else {
// Center the image when it is smaller than the thumbnail
$ Posx = ($ width-$ old_width)/2;
$ Posy = ($ height-$ old_height)/2;
$ Resize_im = $ src_im;
}
Imagecopy ($ capture, $ resize_im, $ posx, $ posy, 0, 0, imagesx ($ resize_im), imagesy ($ resize_im ));
If (! $ Output ){
Header ("Content-type: image/jpeg ");
Imagejpeg ($ capture, null, $ this-> quality );
}
Else {
$ New_file = empty ($ dst )? $ Src: $ dst;
Imagejpeg ($ capture, $ new_file, $ this-> quality );
}
Imagedestroy ($ src_im );
@ Imagedestroy ($ resize_im );
Imagedestroy ($ capture );
}
/**
* Write a watermark image
* @ Param $ src: the image to which the watermark is to be written
* @ Param $ mark watermark image
* @ Param $ transparency of the transparent watermark
*/
Public function mark ($ src, $ mark, $ dst = '', $ output = true)
{
$ Mark_info = getimagesize ($ mark );
$ Src_info = getimagesize ($ src );
List ($ mw, $ mh) = $ mark_info;
List ($ sw, $ sh) = $ src_info;
$ Px = $ sw-$ mw;
$ Py = $ sh-$ mh;
$ Im = $ this-> IM ($ src );
$ Mim = $ this-> IM ($ mark );
Imagecopymerge ($ im, $ mim, $ px, $ py, 0, 0, $ mw, $ mh, $ this-> transparent );
If ($ output ){
$ New_file = empty ($ dst )? $ Src: $ dst;
Imagejpeg ($ im, $ new_file, $ this-> quality );
}
Else
{
Header ('content-type: image/jpeg ');
Imagejpeg ($ im );
}
Imagedestroy ($ im );
Imagedestroy ($ mim );
}
/**
* Get different GD objects through files
*/
Protected function IM ($ file)
{
If (! File_exists ($ file) die ('file not exists .');
$ Info = getimagesize ($ file );
Switch ($ info ['Mime '])
{
Case 'image/GIF ':
$ Mim = imagecreatefromgif ($ file );
Break;
Case 'image/png ':
$ Mim = imagecreatefrompng ($ file );
Imagealphablending ($ mim, false );
Imagesavealpha ($ mim, true );
Break;
Case 'image/jpeg ':
$ Mim = imagecreatefromjpeg ($ file );
Break;
Default:
Die ('file format errors .');
}
Return $ mim;
}
/**
* Scaling the image
* @ Param resource $ src_im Image GD object
* @ Param integer $ width: width of the image
* @ Param integer $ height the height of the image. If no height is set, the image will be scaled proportionally.
* @ Return resuorce $ im returns a GD object
*/
Protected function imageresize ($ src_im, $ width, $ height = null ){
$ Old_width = imagesx ($ src_im );
$ Old_height = imagesy ($ src_im );
$ Proportion = $ old_width/$ old_height;
$ New_width = $ width;
$ New_height = is_null ($ height )? Round ($ new_width/$ proportion): $ height;
// Create a new image and fill in the default background color
$ Im = imagecreatetruecolor ($ new_width, $ new_height );
$ Rgb = explode (",", $ this-> background );
$ White = imagecolorallocate ($ im, $ rgb [0], $ rgb [1], $ rgb [2]);
Imagefill ($ im, 0, 0, $ white );
// Scale the image
Imagecopyresized ($ im, $ src_im, 0, 0, 0, 0, $ new_width, $ new_height, $ old_width, $ old_height );
Return $ im;
}
/**
* Class variable assignment
*/
Public function _ set ($ key, $ value)
{
$ This-> $ key = $ value;
}
/**
* Get the class variable value
*/
Public function _ get ($ key)
{
Return $ this-> $ key;
}
}
?>
The http://www.bkjia.com/PHPjc/320758.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320758.htmlTechArticle code is as follows :? Php/*** author: yagas * email: yagas60@21cn.com */class Image {/** class protection variable */protected $ th_width = 100; protected $ th_height = 50; protected...