Php generates thumbnails and adds watermarks. This simple GD Library operation [php]? Php *** PHP lecture Forum: www. zixue. it weibo: weibo. comYshibaYY Channel: 88354001 * a simple GD Library operation for image operations
[Php]
/****
Yan 18 public welfare PHP Lecture Hall
Discussion: http://www.zixue.it
Micro Bo: http://weibo.com/Yshiba
YY Channel: 88354001
****/
/***
Image operations
First, you must obtain the image size and type information.
Watermark: copies the specified watermark to the target and adds the transparency effect.
Thumbnail: copies a large image to a small image.
***/
Class ImageTool {
// ImageInfo analyzes image information
// Return array ()
Public static function imageInfo ($ image ){
// Determine whether an image exists
If (! File_exists ($ image )){
Return false;
}
$ Info = getimagesize ($ image );
If ($ info = false ){
Return false;
}
// Info is analyzed as an array.
$ Img ['width'] = $ info [0];
$ Img ['height'] = $ info [1];
$ Img ['ext '] = substr ($ info ['Mime'], strpos ($ info ['Mime '],'/') + 1 );
Return $ img;
}
/*
Watermarking
Parm String $ dst and other operation images
Parm String $ water watermark thumbnail
Parm String $ save. If this parameter is not specified, the original graph is replaced by default.
*/
Public static function water ($ dst, $ water, $ save = NULL, $ pos = 2, $ alpha = 50 ){
// Ensure that two images exist first
If (! File_exists ($ dst) |! File_exists ($ water )){
Return false;
}
// First, ensure that the watermark cannot be larger than the image to be operated
$ Dinfo = self: imageInfo ($ dst );
$ Winfo = self: imageInfo ($ water );
If ($ winfo ['height']> $ dinfo ['height'] | $ winfo ['width']> $ dinfo ['width']) {
Return false;
}
// Read the two images on the canvas! However, the image may be png or jpeg. what function is used to read the image?
$ Dfunc = 'imagecreatefrom '. $ dinfo ['ext'];
$ Wfunc = 'imagecreatefrom '. $ winfo ['ext'];
If (! Function_exists ($ dfunc) |! Function_exists ($ wfunc )){
Return false;
} Www.2cto.com
// Dynamically load the function to create the canvas
$ Dim = $ dfunc ($ dst );
// Create the canvas to be operated
$ Wim = $ wfunc ($ water );
// Create a watermark canvas
// Calculate the pasted coordinates based on the Watermark Position
Switch ($ pos ){
Case 0:
// Upper left corner
$ Posx = 0;
$ Posy = 0;
Break;
Case 1:
// Upper right corner
$ Posx = $ dinfo ['width']-$ winfo ['width'];
$ Posy = 0;
Break;
Case 3:
// Lower left corner
$ Posx = 0;
$ Posy = $ dinfo ['height']-$ winfo ['height'];
Break;
Default:
$ Posx = $ dinfo ['width']-$ winfo ['width'];
$ Posy = $ dinfo ['height']-$ winfo ['height'];
}
// Add a watermark
Imagecopymerge ($ dim, $ wim, $ posx, $ posy, 0, 0, $ winfo ['width'], $ winfo ['height'], $ alpha );
// Save
If (! $ Save ){
$ Save = $ dst;
Unlink ($ dst );
// Delete the source image
}
$ Createfunc = 'image'. $ dinfo ['text'];
$ Createfunc ($ dim, $ save );
Imagedestroy ($ dim );
Imagedestroy ($ wim );
Return true;
}
/**
Create a thumbnail using thumb
Proportional scaling, left white on both sides
**/
Public static function thumb ($ dst, $ save = NULL, $ width = 200, $ height = 200 ){
// First, determine whether the image to be processed does not exist.
$ Dinfo = self: imageInfo ($ dst );
If ($ dinfo = false ){
Return false;
}
// Calculate the scaling ratio
$ Calc = min ($ width/$ dinfo ['width'], $ height/$ dinfo ['height']);
// Create the canvas of the original graph
$ Dfunc = 'imagecreatefrom '. $ dinfo ['ext'];
$ Dim = $ dfunc ($ dst );
// Create a thumbnail
$ Tim = imagecreatetruecolor ($ width, $ height );
// Create a white fill thumbnail
$ White = imagecolorallocate ($ tim, 255,255,255 );
// Fill the thumbnail
Imagefill ($ tim, 0, 0, $ white );
// Copy and scale down
$ Dwidth = (int) $ dinfo ['width'] * $ calc;
$ Dheight = (int) $ dinfo ['height'] * $ calc;
$ Paddingx = (int) ($ width-$ dwidth)/2;
$ Paddingy = (int) ($ height-$ dheight)/2;
Imagecopyresampled ($ tim, $ dim, $ paddingx, $ paddingy, 0, 0, $ dwidth, $ dheight, $ dinfo ['width'], $ dinfo ['height']);
// Save the image
If (! $ Save ){
$ Save = $ dst;
Unlink ($ dst );
}
$ Createfunc = 'image'. $ dinfo ['text'];
$ Createfunc ($ tim, $ save );
Imagedestroy ($ dim );
Imagedestroy ($ tim );
Return true;
}
}
// Print_r (ImageTool: imageInfo ('./home.jpg '));
/* Www.2cto.com
Echo ImageTool: water ('./home.jpg', './smallfeng.png', 'home1.jpg', 0 )? 'OK': 'fail ';
Echo ImageTool: water ('./home.jpg', './smallfeng.png', 'home2.jpg', 1 )? 'OK': 'fail ';
Echo ImageTool: water ('./home.jpg', './smallfeng.png', 'home3.jpg', 2 )? 'OK': 'fail ';
Echo ImageTool: water ('./home.jpg', './smallfeng.png', 'home4.jpg', 3 )? 'OK': 'fail ';
*/
Why [php]? Php/*** Yan 18 public welfare PHP lecture altar: http://www.zixue.it micro Bo: http://weibo.com/Yshiba YY Channel: 88354001 *** // *** want to operate pictures...