Php image operations (image watermarking)

Source: Internet
Author: User
Php image operations (image watermarking)
We provide you with a php image Operation class, which can be used to add watermarks to images. For more information, see.

Complete Image operation code.

     ChangeSize (80, 50); // change the size $ img-> changeSize (80); // change the size $ img-> create (); $ img-> free (); $ img = new Image('pic.jpg '); $ img-> addWatermark('logo.jpg', 50); // add a watermark. The first parameter is the watermark image address, the second parameter is the transparent value $ img-> create (); $ img-> free (); site http://bbs.it-home.org */Class Image {var $ imageResource = NULL; var $ target = NULL; var $ enableTypes = array (); var $ imageInfo = array (); var $ createFunc = ''; var $ imageType = NULL;/* Construct for this class * @ param string $ image * @ return Image */function Image ($ image = NULL) {// get enables if (imagetypes () & IMG_GIF) {$ this-> enableTypes [] = 'image/GIF';} if (imagetypes () & IMG_JPEG) {$ this-> enableTypes [] = 'I Mage/jpeg ';} if (imagetypes () & IMG_JPG) {$ this-> enableTypes [] = 'image/jpg';} if (imagetypes () & IMG_PNG) {$ this-> enableTypes [] = 'image/png ';} // end get if ($ image! = NULL) {$ this-> setImage ($ image) ;}/ * set a image resource * @ param string $ image * @ return boolean */function setImage ($ image) {if (file_exists ($ image) & is_file ($ image) {$ this-> imageInfo = getimagesize ($ image ); $ img_mime = strtolower ($ this-> imageInfo ['Mime ']); if (! In_array ($ img_mime, $ this-> enableTypes) {exit ('the system cannot operate on this image type. ');} switch ($ img_mime) {case 'image/GIF': $ link = imagecreatefromgif ($ image); $ this-> createFunc = 'imagegif '; $ this-> imageType = 'GIF'; break; case 'image/jpeg ': case 'image/jpg': $ link = imagecreatefromjpeg ($ image ); $ this-> createFunc = 'imagejpeg '; $ this-> imageType = 'jpeg'; break; case 'image/png ': $ link = imagecreatefrompng ($ image); $ This-> createFunc = 'imagepng '; $ this-> imageType = 'PNG'; break; default: $ link = 'unknow '; $ this-> imageType = 'unknow'; break;} if ($ link! = 'Unknow') {$ this-> imageResource = $ link;} else {exit ('this image type cannot be changed. ');} unset ($ link); return true;} else {return false;}/* set header */function setHeader () {switch ($ this-> imageType) {case 'GIF': header ('content-type: image/GIF'); break; case 'jpeg ': header ('content-type: image/jpeg '); break; case 'PNG ': header ('content-type: image/png'); break; default: exit ('Can not set header. '); bre Ak;} return true;}/* change the image size * @ param int $ width * @ param int $ height * @ return boolean */function changeSize ($ width, $ height =-1) {if (! Is_resource ($ this-> imageResource) {exit ('you cannot change the image size, maybe because you have not set the image source. ');} $ s_width = $ this-> imageInfo [0]; $ s_height = $ this-> imageInfo [1]; $ width = intval ($ width ); $ height = intval ($ height); if ($ width <= 0) exit ('image width must be greater than zero. '); if ($ height <= 0) {$ height = ($ s_height/$ s_width) * $ width;} $ this-> target = imagecreatetruecolor ($ width, $ height); if (@ imagecopyresized ($ this-> target, $ this-> imageResource, 0, 0, 0, 0, $ width, $ height, $ s_width, $ s_height) return true; else return false ;} /* Add watermark * @ param string $ image * @ param int $ app */function addWatermark ($ image, $ app = 50) {if (file_exists ($ image) & is_file ($ image) {$ s_info = getimagesize ($ image);} else {exit ($ image. 'The file does not exist. ') ;}$ r_width = $ s_info [0]; $ r_height = $ s_info [1]; if ($ r_width> $ this-> imageInfo [0]) exit ('watermark image must be smaller than the object '); If ($ r_height> $ this-> imageInfo [1]) exit ('watermark image must be smaller than the target image '); switch ($ s_info ['Mime ']) {case 'image/GIF': $ resource = imagecreatefromgif ($ image); break; case 'image/jpeg ': case 'image/jpg ': $ resource = imagecreatefromjpeg ($ image); break; case 'image/png': $ resource = imagecreatefrompng ($ image); break; default: exit ($ s_info ['Mime ']. the 'Type cannot be used as the watermark source. '); break;} $ this-> target = & $ this-> imageResource; Imagecopymerge ($ this-> target, $ resource, $ this-> imageInfo [0]-$ r_width-5, $ this-> imageInfo [1]-$ r_height-5, 0, 0, $ r_width, $ r_height, $ app); imagedestroy ($ resource); unset ($ resource );} /* create image * @ param string $ name * @ return boolean */function create ($ name = NULL) {$ function = $ this-> createFunc; if ($ this-> target! = NULL & is_resource ($ this-> target) {if ($ name! = NULL) {$ function ($ this-> target, $ name);} else {$ function ($ this-> target);} return true ;} else if ($ this-> imageResource! = NULL & is_resource ($ this-> imageResource) {if ($ name! = NULL) {$ function ($ this-> imageResource, $ name);} else {$ function ($ this-> imageResource);} return true ;} else {exit ('the image cannot be created because the image source is not set. ') ;}}/* free resource */function free () {if (is_resource ($ this-> imageResource) {@ imagedestroy ($ this-> imageResource );} if (is_resource ($ this-> target) {@ imagedestroy ($ this-> target) ;}}}?>

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.