PHP image processing class implementation code

Source: Internet
Author: User
Tags imagejpeg transparent watermark

CopyCode The 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 $ 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;
}
}
?>

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.