Php image processing class (generating thumbnails, adding watermarks, and obtaining Image Information)

Source: Internet
Author: User

Php tutorial image processing class (generating thumbnails, adding watermarks, and obtaining Image Information)
This article provides the image processing class. What he can do is to generate a thumbnail of the image, add a watermark to the image, and obtain the image information. This is a simple and practical function */

Class image
{
Public $ info = array ();

Function _ construct ()
{
! Extension_loaded ('gd ') & exit ("www. bKjia. c0m prompt: the server environment does not support gd library ");
Return true;
}

Function image ()
{
$ This->__ construct ();
}
 
Function thumb ($ image, $ thumb_width = 300, $ thumb_height = 225)
{
$ Info = $ this-> info ($ image );
$ Scale = min (1, min ($ thumb_width/$ info ['width'], $ thumb_height/$ info ['height']); // proportional Scaling
$ Thumb_width = intval ($ info ['width'] * $ scale );
$ Thumb_height = intval ($ info ['height'] * $ scale );
$ Createfunc = 'imagecreatefrom '. ($ info ['type'] = 'jpg '? 'Jpeg ': $ info ['type']);
$ Im = $ createfunc ($ image );
$ Thumb_im = $ info ['type']! = 'Gif' & function_exists ('imagecreatetruecolor ')? Imagecreatetruecolor ($ thumb_width, $ thumb_height): imagecreate ($ thumb_width, $ thumb_height );
Imagecopyresampled ($ thumb_im, $ im, 0, 0, 0, $ thumb_width, $ thumb_height, $ info ['width'], $ info ['height']);
If ($ info ['type'] = 'gif' | $ info ['type'] = 'png ')
{
$ Bgcolor = imagecolorallocate ($ thumb_im, 0,255, 0 );
Imagecolortransparent ($ thumb_im, $ bgcolor );
}
$ Imagefunc = 'image'. ($ info ['type'] = 'jpg '? 'Jpeg ': $ info ['type']);
$ Thumbname = 'thumb _ '. $ info ['name'].'. '. $ info ['type'];
$ Imagefunc ($ thumb_im, $ info ['path']. $ thumbname );
Imagedestroy ($ im );
Imagedestroy ($ thumb_im );
Return $ info ['path']. $ thumbname;
}

Function watermark ($ image, $ pos = 9, $ watermarkimg = 'images/watermark.gif ', $ pct = 65, $ text = '', $ w_font = 5, $ w_color = '# ff0000 ')
{
$ Imageinfo = $ this-> info ($ image );
$ Source_w = $ imageinfo ['width'];
$ Source_h = $ imageinfo ['height'];
$ Imagecreatefunc = 'imagecreatefrom '. ($ imageinfo ['type'] = 'jpg '? 'Jpeg ': $ imageinfo ['type']);
$ Im = $ imagecreatefunc ($ image );
If (! Empty ($ watermarkimg) & file_exists ($ watermarkimg) // Add an image watermark
{
$ Iswaterimage = true;
$ Watermarkinfo = $ this-> info ($ watermarkimg );
$ Width = $ watermarkinfo ['width'];
$ Height = $ watermarkinfo ['height'];
$ Watermarkcreatefunc = 'imagecreatefrom '. ($ watermarkinfo ['type'] = 'jpg '? 'Jpeg ': $ watermarkinfo ['type']);
$ Watermark_im = $ watermarkcreatefunc ($ watermarkimg );
}
Else // Add a text watermark
{
$ Iswaterimage = false;
If (! Empty ($ w_color) & strlen ($ w_color) = 7)
{
$ R = hexdec (substr ($ w_color, 1, 2 ));
$ G = hexdec (substr ($ w_color, 3, 2 ));
$ B = hexdec (substr ($ w_color, 5, 2 ));
}
$ Temp = imagettfbbox (ceil ($ w_font * 2.5), 0, 'fonts/alger. ttf', $ text );
$ Width = $ temp [2]-$ temp [6];
$ Height = $ temp [3]-$ temp [7];
Unset ($ temp );
}
Switch ($ pos)
{
Case 0:
$ Wx = mt_rand (0, ($ source_w-$ width ));
$ Wy = mt_rand (0, ($ source_h-$ height ));
Break;
Case 1:
$ Wx = 5;
$ Wy = 5;
Break;
Case 2:
$ Wx = ($ source_w-$ width)/2;
$ Wy = 5;
Break;
Case 3:
$ Wx = $ source_w-$ width-5;
$ Wy = 5;
Break;
Case 4:
$ Wx = 5;
$ Wy = ($ source_h-$ height)/2;
Break;
Case 5:
$ Wx = ($ source_w-$ width)/2;
$ Wy = ($ source_h-$ height)/2;
Break;
Case 6:
$ Wx = $ source_w-$ width-5;
$ Wy = ($ source_h-$ height)/2;
Break;
Case 7:
$ Wx = 5;
$ Wy = $ source_h-$ height-5;
Break;
Case 8:
$ Wx = ($ source_w-$ width)/2;
$ Wy = $ source_h-$ height-5;
Break;
Default:
$ Wx = $ source_w-$ width-5;
$ Wy = $ source_h-$ height-5;
Break;
}
If ($ iswaterimage)
{
If ($ imageinfo ['type'] = 'png '){
Imagecopy ($ im, $ watermark_im, $ wx, $ wy, 0, 0, $ width, $ height );
} Else {
Imagecopymerge ($ im, $ watermark_im, $ wx, $ wy, 0, 0, $ width, $ height, $ pct );
}
}
Else
{
Imagestring ($ im, $ w_font, $ wx, $ wy, $ text, imagecolorallocate ($ im, $ r, $ g, $ B ));
}
$ Imagefunc = 'image'. ($ imageinfo ['type'] = 'jpg '? 'Jpeg ': $ imageinfo ['type']);
$ Imagefunc ($ im, $ image );
Imagedestroy ($ im );
Return true;
}

Function info ($ image)
{
$ Info = array ();
$ Info ['SIZE'] = filesize ($ image );
$ Imageinfo = getimagesize ($ image );
$ Info ['width'] = $ imageinfo [0];
$ Info ['height'] = $ imageinfo [1];
$ Info ['width _ height'] = $ imageinfo [3];
$ Info ['mime '] = $ imageinfo ['mime'];
Unset ($ imageinfo );
$ Imageinfo = pathinfo ($ image );
$ Info ['path'] = $ imageinfo ['dirname']. '/';
$ Info ['type'] = strtolower ($ imageinfo ['extension']); // image type, excluding '.'
$ Info ['name'] = $ imageinfo ['filename'];
Unset ($ imageinfo, $ name );
$ This-> info = $ info;
Return $ info;
}
}

 

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.