Php uploads an image watermark (image watermark, text watermark)

Source: Internet
Author: User
Tags imagecopy imagejpeg
The code is as follows: Copy code

/*
* Created on 2010-6-21
 *
* The class for control image
 *
* Made by s71ence
 *
* @ $ Img_path: Image path
* @ $ Is_auto_reduce: whether the image is automatically compressed according to the size level. 1 is
* @ $ Is_appoint whether to manually compress or zoom in 1 is
* @ $ Multiple manually specify the compression/amplification ratio
* @ $ Is_water_str whether to add the watermark text 1 is
* @ $ Water_str watermark text
* @ $ Is_watermark whether to add watermark image 1 is
* @ $ Logo_path: watermark image path
* @ $ Is_display whether Image 1 is displayed
* @ $ Is_create whether the compressed image 1 is generated
 *
* Note:
* 1. When a new image is generated, the image cannot be displayed, that is, $ isdisplay and $ iscreate cannot be set to 1 at the same time.
* 2. When the image width or height is less than 1000, you must manually set the compression ratio for compression.
* 3. We do not recommend that you enable the watermark. To enable the watermark, we recommend that the size of the original image be less than 1000.
* 4. The watermark text cannot contain Chinese characters
* 5. The newly generated images are in the original directory file and support n layers.
*/

Class image_control
 {
Private $ img_path;
Private $ is_auto_reduce;
Private $ is_appoint;
Private $ multiple;
Private $ is_water_str;
Private $ water_str;
Private $ is_watermark;
Private $ logo_path;
Private $ is_display;
Private $ is_create;

Function _ construct ($ img_path, $ is_auto_reduce, $ is_appoint, $ multiple, $ is_water_str, $ water_str, $ is_watermark, $ logo_path, $ is_display, $ is_create)
  {
$ This-> img_path = $ img_path;
$ This-> is_auto_reduce = $ is_auto_reduce;
$ This-> is_appoint = $ is_appoint;
$ This-> multiple = $ multiple;
$ This-> is_water_str = $ is_water_str;
$ This-> water_str = $ water_str;
$ This-> is_watermark = $ is_watermark;
$ This-> logo_path = $ logo_path;
$ This-> is_display = $ is_display;
$ This-> is_create = $ is_create;
  }

Function img_control ()
  {
// Obtain the source image
$ Img_info = getimagesize ($ this-> img_path );

Switch ($ img_info [2])
  {
Case 1:
$ Img_get = @ imagecreatefromgif ($ this-> img_path );
Break;

Case 2:
$ Img_get = @ imagecreatefromjpeg ($ this-> img_path );
Break;

Case 3:
$ Img_get = @ imagecreatefrompng ($ this-> img_path );
Break;
  }

// Text watermark
If ($ this-> is_water_str = 1)
  {
// Imagettftext (source image, text size, text rotation, Watermark start coordinate x, Watermark start coordinate y, $ te, 'simhei. Ttf', $ str );
$ Te = imagecolorallocate ($ img_get, 255,255,255 );
$ Str = iconv ("gbk", "UTF-8", $ this-> water_str); // watermark text
Imagettftext ($ img_get, 16, 0, $ img_info [0]-200, $ img_info [1]-20, $ te, 'msyh. Ttf', $ str );
  }

// Image watermark
If ($ this-> is_watermark = 1)
  {
// Watermark image processing
$ Logo_info = getimagesize ($ this-> logo_path );

Switch ($ logo_info [2])
   {
Case 1:
$ Logo = @ imagecreatefromgif ($ this-> logo_path );
Break;

Case 2:
$ Logo = @ imagecreatefromjpeg ($ this-> logo_path );
Break;

Case 3:
$ Logo = @ imagecreatefrompng ($ this-> logo_path );
Break;
   }

// Watermark logo image
// Function description: imagecopy (source image, watermark image, Watermark coordinate x, Watermark coordinate y, Watermark Image start coordinate x, Watermark Image start coordinate y, and 'watermark Image width ', 'watermark Image high ');
Imagecopy ($ img_get, $ logo, 0, 0, 0, $ logo_info [0], $ logo_info [1]);
  }

// Automatic image compression: auto compression based on Image size
// Imagecopyresized (canvas, source image, canvas start x coordinate, canvas start y coordinate, source image start x coordinate, source image start x coordinate, new image width, New Image height, original image width, original image height );
If ($ this-> is_auto_reduce = 1)
  {
If ($ img_info [0] >=3000 | $ img_info [1] >=3000)
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.03, $ img_info [1] * 0.03); // Generate a canvas
Imagecopyresized ($ new_image_get, $ img_get, 0.03, 0.03, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
   }
Else if ($ img_info [0]> = 2500 | $ img_info [1]> = 2500)
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.04, $ img_info [1] * 0.04 );
Imagecopyresized ($ new_image_get, $ img_get, 0.04, 0.04, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
   }
Else if ($ img_info [0]> = 2000 | $ img_info [1]> = 2000)
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.05, $ img_info [1] * 0.05 );
Imagecopyresized ($ new_image_get, $ img_get, 0.05, 0.05, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
   }
Else if ($ img_info [0]> = 1500 | $ img_info [1]> = 1500)
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.08, $ img_info [1] * 0.08 );
Imagecopyresized ($ new_image_get, $ img_get, 0.08, 0.08, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
   }
Else if ($ img_info [0]> = 1000 | $ img_info [1]> = 1000)
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.1, $ img_info [1] * 0.1 );
Imagecopyresized ($ new_image_get, $ img_get, 0.1, 0.1, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
   }
Else if ($ img_info [0]> = 500 | $ img_info [1]> = 500)
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.2, $ img_info [1] * 0.2 );
Imagecopyresized ($ new_image_get, $ img_get, 0.2, 0.2, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
   }
Else if ($ img_info [0]> = 300 | $ img_info [1]> = 300)
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 0.3, $ img_info [1] * 0.3 );
Imagecopyresized ($ new_image_get, $ img_get, 0.3, 0.3, $ img_info [0] *, $ img_info [1] *, $ img_info [0], $ img_info [1]);
   }
Else
   {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * 1, $ img_info [1] * 1 );
Imagecopyresized ($ new_image_get, $ img_get, 0, 0, 0, $ img_info [0] * 1, $ img_info [1] * 1, $ img_info [0], $ img_info [1]);
   }
  }

// Manual image compression
// Imagecopyresized (canvas, source image, canvas start x coordinate, canvas start y coordinate, source image start x coordinate, source image start x coordinate, new image width, New Image height, original image width, original image height );
If ($ this-> is_appoint)
  {
$ New_image_get = imagecreatetruecolor ($ img_info [0] * $ this-> multiple, $ img_info [1] * $ this-> multiple); // Generate a canvas
Imagecopyresized ($ new_image_get, $ img_get, 0, 0, 0, $ img_info [0] * $ this-> multiple, $ img_info [1] * $ this-> multiple, $ img_info [0], $ img_info [1]);
  }

// Image output
If ($ this-> is_display = 1)
  {
Header ("content-type: image/jpeg ");
Return imagejpeg ($ new_image_get );
  }

// New image generation
If ($ this-> is_create = 1)
  {
$ New_name = explode ("/", $ this-> img_path );
$ New_name_string = "";

For ($ I = 0; $ I <count ($ new_name)-1; $ I ++)
   {
$ New_name_string. = $ new_name [$ I]. "/";
   }

$ New_img_path = $ new_name_string. "new". $ new_name [$ I];

If (imagejpeg ($ new_image_get, $ new_img_path) & imagejpeg ($ img_get, $ this-> img_path ))
   {
Setcookie ("img_new_path", $ new_img_path );
// Return "image generated successfully! <Br/> New image: ". $ new_img_path." <br/> source image: ". $ this-> img_path;
   }
Else
   {
Return "image generation failed. Please check whether the configuration is correct! ";
   }
  }
  }

Function _ desctruct ()
  {
// Clear
  }
 }

// Call method

The code is as follows: Copy code

/* $ Img_path = "../users/user_photo/t2.jpg"; // path of the operated image
$ Is_auto_reduce = 1; // whether the image is automatically compressed according to the size level. 1 is
$ Is_appoint = 0; // whether to manually compress 1 is
$ Multiple = 0.5; // manually specify the compression ratio
$ Is_water_str = 0; // whether to add watermark text
$ Water_str = "www.111cn.net"; // watermark text
$ Is_watermark = 0; // whether to add watermark image 1 is
$ Logo_path = "../image/logo_about.gif"; // watermark image path
$ Is_display = 0; // whether to display image 1 is
$ Is_create = 1; // whether the compressed image 1 is generated
$ Img = new image_control ($ img_path, $ is_auto_reduce, $ is_appoint, $ multiple, $ is_water_str, $ water_str, $ is_watermark, $ logo_path, $ is_display, $ is_create );
Echo $ img-> img_control ();*/

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.