Php image adding Chinese and image watermark code _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
Add Chinese and image watermark codes for php images. $ Ico_pic is the watermark image you want to add a watermark to the image. other parameters are described in detail, if you look for this type of code, you can download and save it as a php file. then, the caller $ ico_pic mentioned later is the watermark image you want to add a watermark to the image. other parameters are described in detail, if you look for such code, you can download and save it as a php file, and then use the call method described later to call the watermark image generation code in this document.

$ Ico_pic is the watermark image you want to add a watermark to the image. other parameters are described in detail, if you look for this type of code, you can download and save it as a php Tutorial file and use the call method described later to call the watermark image generation code in this document.
*/

Class smallpic {

Private $ src_pic; // source image
Private $ ico_pic = "003.png"; // watermark image
Private $ ico_text = "watermark"; // watermark text
Private $ small_width; // The width of the thumbnail.
Private $ small_height; // The height of the thumbnail.
Private $ is_ico_pic = true; // whether to add an image watermark
Private $ is_text = true; // whether to add a text watermark
Private $ src_x = 20; // the x coordinate of the watermark in the source image
Private $ src_y = 20; // y coordinate of the watermark in the source image
Private $ ut = "UTF-8"; // text encoding
Private $ font_color = "#990000"; // text watermark color
Private $ samll_pic_name = "smallpic"; // The name of the thumbnail.
Private $ big_pic_name = "bigpic"; // name of the big chart


Function _ construct ($ src_pic, $ small_width, $ small_height ){
$ This-> checkfile ($ src_pic );
$ This-> checkfile ($ this-> ico_pic );
$ This-> src_pic = $ src_pic;
$ This-> small_width = $ small_width;
$ This-> small_height = $ small_height;
}

Private function _ get ($ property_name ){
Return $ this-> $ property_name;
}

Private function _ set ($ property_name, $ value ){
Return $ this-> $ property_name = $ value;
}


/**
* Obtain basic information about the image. the type is array.
*/
Function getimageinfo ($ image ){
Return @ getimagesize ($ image );
}

/**
* Load the image to php.
* $ Images
*/
Function getimage ($ image ){
$ Image_info = $ this-> getimageinfo ($ image );
Switch ($ image_info [2]) {
Case 1:
$ Img = @ imagecreatefromgif ($ image );
Break;
Case 2:
$ Img = @ imagecreatefromjpeg ($ image );
Break;
Case 3:
$ Img = @ imagecreatefrompng ($ image );
Break;
}
Return $ img;
}

Function createimageforsuffix ($ big_pic, $ new_pic ){
$ Image_info = $ this-> getimageinfo ($ this-> src_pic );
Switch ($ image_info [2]) {
Case 1:
// Output a large image
@ Imagegif ($ big_pic, $ this-> big_pic_name. ". gif ");
// Output the thumbnail
@ Imagegif ($ new_pic, $ this-> samll_pic_name. ". gif ");
Break;
Case 2:
// Output a large image
@ Imagejpeg ($ big_pic, $ this-> big_pic_name. ". jpg ");
// Output the thumbnail
@ Imagejpeg ($ new_pic, $ this-> samll_pic_name. ". jpg ");
Break;
Case 3:
// Output a large image
@ Imagepng ($ big_pic, $ this-> big_pic_name. ". png ");
// Output the thumbnail
@ Imagepng ($ new_pic, $ this-> samll_pic_name. ". png ");
Break;
}
}

Function checkfile ($ file ){
If (! File_exists ($ file )){
Die ("image:". $ file. "does not exist! ");
}
}

Function createsmallimage (){
$ Big_pic = $ this-> getimage ($ this-> src_pic );
$ Big_pic_info = $ this-> getimageinfo ($ this-> src_pic );
$ New_pic = $ this-> getimage ($ this-> ico_pic );
$ New_pic_info = $ this-> getimageinfo ($ this-> ico_pic );
$ Rgb = $ this-> convcolor ();

// Determine whether to scale in width or in high proportion
If ($ big_pic_info [0]> $ big_pic_info [1]) {
$ Ratio = $ this-> small_width/(int) $ big_pic_info [0];
$ Small_pic_width = $ this-> small_width;
$ Small_pic_height = (int) ($ big_pic_info [1] * $ ratio );
} Else {
$ Ratio = $ this-> small_height/(int) $ big_pic_info [1];
$ Small_pic_height = $ this-> small_height;
$ Small_pic_width = (int) ($ big_pic_info [0] * $ ratio );
}

// Echo $ small_pic_width = (int) ($ big_pic_info [0] * $ ratio );
// Echo $ small_pic_height = (int) ($ big_pic_info [1] * $ ratio );

// Whether to watermark the image
If ($ this-> is_ico_pic ){
// Watermark the image
@ Imagecopy ($ big_pic, $ new_pic, $ this-> src_x, $ this-> src_y, 0,0, $ new_pic_info [0], $ new_pic_info [1]);
}
// Whether to add a text watermark
If ($ this-> is_text ){
// Set the text color
$ Text_color = @ imagecolorallocate ($ big_pic, $ rgb [0], $ rgb [1], $ rgb [2]);
// Convert the text encoding
$ Text = @ iconv ($ this-> ut, "UTF-8", $ this-> ico_text );
// Add a text watermark
@ Imagettftext ($ big_pic, 12, 0, $ this-> src_x, $ this-> src_y, $ text_color, "simkai_0.ttf", $ text );
}
// Create a canvas for a new image
$ New_pic = @ imagecreatetruecolor ($ small_pic_width, $ small_pic_height );
// Generate a thumbnail
@ Imagecopyresized ($ new_pic, $ big_pic, 0, 0, 0, $ small_pic_width, $ small_pic_height, $ big_pic_info [0], $ big_pic_info [1]);
// Output diagram
$ This-> createimageforsuffix ($ big_pic, $ new_pic );
}

/**
* Functions inside the class convert #000000 to 255,255,255
*/
Private function convcolor (){
$ Rgb = array ();
$ Color = preg_replace ("/#/", "", $ this-> font_color );
$ C = hexdec ($ color );
$ R = ($ c> 16) & 0xff;
$ G = ($ c> 8) & 0xff;
$ B = $ c & 0xff;
$ Rgb [0] = $ r;
$ Rgb [1] = $ g;
$ Rgb [2] = $ B;
Return $ rgb;
}
}

// Call method

$ Pic = new smallpic ("002.jpg", 600,300 );
$ Pic-> is_text = true;
$ Pic-> is_ico_pic = true;
$ Pic-> ico_pic = "./images/004.png ";
$ Pic-> ico_text = "happy new year! ";
// $ Pic-> src_x = 80;
$ Pic-> src_y = 80;
$ Pic-> ut = "UTF-8 ";
$ Pic-> font_color = "#0521f8 ";
$ Pic-> samll_pic_name = "hslsamll ";
$ Pic-> big_pic_name = "hslbig ";
$ Pic-> createsmallimage ();

?>

Watermark is the watermark image you want to add a watermark to the image. other parameters are described in detail, if you look for this kind of code, you can download and save it as a php file and use the called party mentioned later...

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.