1. add a watermark to the image 2. generate a new scaling chart
- Class Image {
- // Watermark configuration items
- Private $ wateon;
- Private $ waterImg;
- Private $ waterPos;
- Private $ waterPct;
- Private $ waterText;
- Private $ waterFont;
- Private $ waterTextSize;
- Private $ waterTextColor;
- Private $ qua;
- // Thumbnail configuration items
- Private $ thumbWidth;
- Private $ thumbHeight;
- Private $ thumbType;
- Private $ thumbEndfix;
- // Constructor
- Public function _ construct (){
- $ This-> 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 that 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 shrinkage chart width
- * @ Param int $ thumbHeight shrinkage chart height
- * @ Param int $ thumbType scaling
- */
- Public function thumb ($ img, $ outFile = "", $ thumbWidth = "", $ thumbHeight = "", $ thumbType = ""){
- If (! $ This-> check ($ img )){
- Return false;
- }
- // Scaling chart processing method
- $ ThumbType = $ thumbType? $ ThumbType: $ this-> thumbType;
- // Scale chart width
- $ ThumbWidth = $ thumbWidth? $ ThumbWidth: $ this-> thumbWidth;
- // Scale chart height
- $ ThumbHeight = $ thumbHeight? $ ThumbHeight: $ this-> thumbHeight;
- // Obtain the source image information
- $ ImgInfo = getimagesize ($ img );
- // Original image width
- $ ImgWidth = $ imgInfo [0];
- // Original image height
- $ ImgHeight = $ imgInfo [1];
- // Obtain the source image type
- $ Imgtype = image_type_to_extension ($ imgInfo [2]);
- // Obtain the size of the source image and the size of the thumbnail based on different scaling methods)
- $ 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 ($ imgtype = ". 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;
- // Original image size
- $ Img_w = $ imgWidth;
- $ Img_h = $ imgHeight;
- Switch ($ thumbType ){
- Case 1:
- // Fixed width and auto-increment height
- $ H = $ w/$ imgWidth * $ imgHeight;
- Break;
- Case 2: // fixed height, self-width
- $ 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 adding the watermark to outImg
- * @ 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 the watermark is added
- $ 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;
- // Original image information
- $ ImgInfo = getimagesize ($ img );
- // Original 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 based on different situations
- $ W_img = null;
- 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 a 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 information 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 = ($ imgWidth-$ 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, $ imgHeight-$ 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, $ waterWidth, $ waterHeight );
- } Else {
- Imagecopymerge ($ resImg, $ w_img, $ x, $ y, 0, $ waterInfo, $ waterHeight, $ pct );
- }
- } Else {// The watermark image is invalid. add it as a text watermark.
- $ Red = hexdec (substr ($ this-> waterTextColor, 1, 2 ));
- $ Greem = hexdec (substr ($ this-> waterTextColor, 3, 2 ));
- $ Blue = hexdec (substr ($ this-> waterTextColor, 5, 2 ));
- $ Color = imagecolorallocate ($ 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;
- }
- }
- ?>
- Return array (
- // Watermark processing
- "WATER_ON" => 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 text color
- "WATER_TEXT_SIZE" => 16, // text size
- "WATER_QUA" => 80, // Image compression ratio
- // Thumbnail
- "THUMB_WIDTH" => 150, // scaling chart 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, and the source image is cropped
- "THUMB_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 lowercase 1 uppercase
- */
- 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 a KEY using a variable function
- $ _ NewArr [$ _ k] = is_array ($ v )? Array_change_key_case_d ($ v): $ v;
- }
- Return $ _ newArr;
- }
- /*
- * Read and set configuration items
- * @ Param $ name the void configuration item name. if not specified, all configuration items are returned.
- * @ Param $ value the value of the void configuration item
- * @ Param $ value: false null. only the $ name value is used.
- */
- 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 ));
- }
- // If $ name is a string, no value in $ value indicates that the value of the configuration item is obtained, and a value indicates that the configuration item is changed.
- If (is_string ($ name )){
- $ Name = strtoupper ($ name );
- // 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;
- }
- }
- }
|