Add watermarks to images and generate scaling charts-PHP source code

Source: Internet
Author: User
Adding watermarks to images and generating scaling charts
1. add a watermark to the image
2. generate a new scaling chart

Php code

 Wateon = C ("WATER_ON"); $ this-> waterImg = C ("WATER_IMG"); $ this-> waterPos = C ("WATER_POS "); $ this-> waterPct = C ("WATER_PCT"); $ this-> waterText = C ("WATER_TEXT"); $ this-> waterFont = C ("WATER_FONT "); $ this-> waterTextSize = C ("WATER_TEXT_SIZE"); $ this-> waterTextColor = C ("WATER_TEXT_COLOR"); $ this-> qua = C ("WATER_QUA "); // scaling chart $ this-> thumbWidth = C ("THUMB_WIDTH"); $ this-> thumbHeight = C ("THUMB_HEIGHT"); $ this-> thumbType = C ("THUMB_TYPE"); $ this-> thumbEndFix = C ("THUMB_ENDFIX");}/** verify whether the image is valid */private function check ($ img) {return is_file ($ img) & getimagesize ($ img) & extension_loaded ("gd ");} /** scaling chart * @ param string $ img Source image * @ param string $ the image stored after the outFile scaling rate * @ param int $ thumbWidth scaling rate chart width * @ param int $ thumbHeight scaling rate chart the height * @ param int $ thumbType method for scaling down */public function thumb ($ img, $ outFile = "", $ thumbWidth = "", $ thumbHeight =" ", $ ThumbType =" ") {if (! $ This-> check ($ img) {return false;} // how to handle a scaling chart $ thumbType = $ thumbType? $ ThumbType: $ this-> thumbType; // scaling chart width $ thumbWidth = $ thumbWidth? $ ThumbWidth: $ this-> thumbWidth; // scale chart height $ thumbHeight = $ thumbHeight? $ ThumbHeight: $ this-> thumbHeight; // Obtain the source image information $ imgInfo = getimagesize ($ img); // the source image width $ imgWidth = $ imgInfo [0]; // original image height $ imgHeight = $ imgInfo [1]; // Obtain the source image type $ imgtype = image_type_to_extension ($ imgInfo [2]); // according to different scaling methods, obtain the size (corresponding size of the source image and thumbnail) $ thumb_size = $ this-> thumbsize ($ imgWidth, $ imgHeight, $ thumbWidth, $ thumbHeight, $ thumbType ); // create the source image $ func = "imagecreatefrom ". substr ($ imgtype, 1); // variable function $ resImg = $ func ($ img); // Create a scaling chart if ($ I Mgtype = ". gif ") {$ res_thumb = imagecreate ($ thumb_size [2], $ thumb_size [3]);} else {$ res_thumb = imagecreatetruecolor ($ thumb_size [2], $ thumb_size [3]);} imagecopyresized ($ res_thumb, $ resImg, 0, 0, 0, $ thumb_size [2], $ thumb_size [3], $ thumb_size [0], $ thumb_size [1]); $ fileInfo = pathinfo ($ img); // File information $ outFile = $ outFile? $ OutFile: $ fileInfo ['filename']. $ this-> thumbEndFix. $ fileInfo ['extension']; // file name $ outFile = $ fileInfo ["dirname"]. "/". $ outFile; // add the directory $ func = "image ". substr ($ imgtype, 1); $ func ($ res_thumb, $ outFile); return $ outFile;} private function thumbSize ($ imgWidth, $ imgHeight, $ thumbWidth, $ thumbHeight, $ thumbType) {// scale chart size $ w = $ thumbWidth; $ h = $ thumbHeight; // source image size $ img_w = $ imgWidth; $ img_h = $ imgHeight; switch ($ thumbType ){ Case 1: // fixed width, auto-increment height $ h = $ w/$ imgWidth * $ imgHeight; break; case 2: // fixed height, width from $ w = $ h/$ imgHeight * $ imgWidth; break; case 3: if ($ imgHeight/$ thumbHeight> $ imgWidth/$ thumbWidth) {$ img_h = $ imgWidth/$ thumbWidth * $ thumbHeight;} else {$ img_w = $ imgHeight/$ thumbHeight * $ thumbWidth;} return array ($ img_w, $ img_h, $ w, $ h );} /** @ param string $ img Source image * @ param string $ image generated after the outImg watermark is added * @ param int $ pos watermark position * @ param int $ pct Transparency * @ param text $ text watermark text * @ param string $ waterImg watermark image */public function water ($ img, $ outImg = null, $ pos = "", $ pct = "", $ text = "", $ waterImg = "", $ textColor = "") {if (! $ This-> check ($ img) {return false;} // the image generated after adding the watermark $ outImg = $ outImg? $ OutImg: $ img; // Watermark Position $ pos = $ pos? $ Pos: $ this-> waterPos; // transparency $ pct = $ pct? $ Pct: $ this-> waterPct; // watermark text $ text = $ text? $ Text: $ this-> waterText; // watermark image $ waterImg = $ waterImg? $ WaterImg: $ this-> waterImg; // verify the watermark image $ waterImgOn = $ this-> check ($ waterImg); // watermark text color $ textColor = $ textColor? $ TextColor: $ this-> waterTextColor; // source image information $ imgInfo = getimagesize ($ img); // source image width $ imgWidth = $ imgInfo [0]; // original image height $ imgHeight = $ imgInfo [1]; switch ($ imgInfo [2]) {case 1: $ resImg = imagecreatefromgif ($ img); break; case 2: $ resImg = imagecreatefromjpeg ($ img); break; case 3: $ resImg = imagecreatefrompng ($ img); break;} if ($ waterImgOn) {// watermark image valid // watermark information $ waterInfo = getimagesize ($ waterImg); // watermark width $ waterWidth = $ waterInfo [0 ]; // Watermark height $ waterHeight = $ waterInfo [1]; // create different types of gif jpeg png $ w_img = null based on different situations; switch ($ waterInfo [2]) {case 1: $ w_img = imagecreatefromgif ($ waterImg); break; case 2: $ w_img = imagecreatefromjpeg ($ waterImg); break; case 3: $ w_img = imagecreatefrompng ($ waterImg) ;}} else {// The watermark image is invalid. use the text watermark if (empty ($ text) | strlen ($ textColor )! = 7) {return false;} // Obtain the text watermark box information $ textInfo = imagettfbbox ($ this-> waterTextSize, 0, $ this-> waterFont, $ text ); // text width $ textWidth = $ textInfo [2]-$ textInfo [6]; // text height $ textHeight = $ textInfo [3]-$ textInfo [7];} // Watermark Position $ x = $ y = 20; switch ($ pos) {case 1: break; case 2: $ x = ($ imgWidth-$ waterWidth)/2; break; case 3: $ y = $ imgWidth-$ waterWidth-10; break; case 4: $ x = ($ imgHeight-$ waterHeight)/2; break; case 5: $ x = ($ img Width-$ waterWidth)/2; $ y = ($ imgHeight-$ waterHeight)/2; break; case 6: $ x = $ imgWidth-$ waterWidth-10; $ y = ($ imgHeight-$ waterHeight)/2; break; case 7: $ x = $ imgHeight-$ waterHeight-10; break; case 8: $ x = ($ imgWidth-$ waterWidth)/2; $ y = $ imgHeight-$ waterHeight-10; break; case 9: $ x = $ imgWidth-$ waterWidth-10; $ y = $ imgHeight-$ waterHeight-10; break; default: $ x = mt_rand (20, $ imgWidth-$ waterWidth); $ y = mt_rand (20, $ imgH Eight-$ waterHeight);} if ($ waterImgOn) {// add a watermark as an image when the watermark image is valid. if ($ waterInfo [2] = 3) {imagecopy ($ resImg, $ w_img, $ x, $ y, 0, 0, $ waterWidth, $ waterHeight);} else {imagecopymerge ($ resImg, $ w_img, $ x, $ y, $ waterInfo, $ waterHeight, $ pct) ;}} else {// The watermark image is invalid, add $ red = hexdec (substr ($ this-> waterTextColor,) as a text watermark; $ greem = hexdec (substr ($ this-> waterTextColor )); $ blue = hexdec (substr ($ this-> waterTextColor, 5, 2); $ color = I Magecolorallocate ($ resImg, $ red, $ greem, $ blue); imagettftext ($ resImg, $ this-> waterTextSize, 0, $ x, $ y, $ color, $ this-> waterFont, $ text);} // output Image switch ($ imgInfo [2]) {case 1: imagegif ($ resImg, $ outImg); break; case 2: imagejpeg ($ resImg, $ outImg); break; case 3: imagepng ($ resImg, $ outImg); break ;} // garbage collection if (isset ($ resImg) {imagedestroy ($ resImg);} if (isset ($ w_img) {imagedestroy ($ w_img );} return true ;}}?>
 1, // watermark switch "WATER_IMG" => ". /data/logo.png ", // watermark image" WATER_POS "=> 9, // Watermark Position" WATER_PCT "=> 80, // watermark transparency "WATER_TEXT" => "http://www.caoxiaobin.cn", "WATER_FONT" => ". /data/simsunb. ttf ", // watermark font" WATER_TEXT_COLOR "=>" #333333 ", // hexadecimal representation of the text color" WATER_TEXT_SIZE "=> 16, // text size "WATER_QUA" => 80, // Image compression ratio // Thumbnail "THUMB_WIDTH" => 150, // shrinkage image width "THUMB_HEIGHT" => 150, // Thumbnail height "THUMB_TYPE" => 1, // fixed width of thumbnail processing 1, fixed height auto-Increment 2, fixed width auto-increment // the size of the thumbnail remains unchanged, crop the source image "T HUMB_ENDFIX "=>" _ thmub "// Thumbnail suffix);?>
/** Case-insensitive data key detection */function array_key_exists_d ($ key, $ arr) {$ _ key = strtolower ($ key ); foreach ($ arr as $ k =>$ v) {if ($ _ key = strtolower ($ k) {return true ;}}} /** recursively change the KEY (KEY name) of the array * @ param array; * @ stat int 0 lower case 1 upper case */function array_change_key_case_d ($ arr, $ stat = 0) {$ func = $ stat? "Strtoupper": "strtolower"; $ _ newArr = array (); if (! Is_array ($ arr) | empty ($ arr) {return $ _ newArr;} foreach ($ arr as $ k => $ v) {$ _ k = $ func ($ k); // convert the case sensitivity of the KEY using the variable function $ _ newArr [$ _ k] = is_array ($ v )? Array_change_key_case_d ($ v): $ v;} return $ _ newArr;}/** Read and set configuration item * @ param $ name void configuration item name, if you do not enter the value of all configuration items * @ param $ value void configuration item * @ param $ value false null, only the $ name value */function C ($ name = null, $ value = null) {static $ config = array (); // static variable $ config stores all configuration items if (is_null ($ name) {return $ config ;} // if $ name is an array if (is_array ($ name) {return $ config = array_merge ($ config, array_change_key_case_d ($ name, 1);} // $ nam E is a string. if $ value has no value, the value of the configuration item is obtained. if (is_string ($ name) {$ name = strtoupper ($ name), the configuration item is changed ); // Obtain the configuration item value if (is_null ($ value) {return array_key_exists_d ($ name, $ config )? $ Config [$ name]: null;} else {// set the value $ config [$ name] = $ value; return true ;}}}
Related Article

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.