PHP images add Chinese and Image watermark Code _php tutorial

Source: Internet
Author: User
Tags imagejpeg
$ico _pic is the watermark you want to add watermark image, the other parameters are detailed description, if you look at this type of code can be downloaded to save PHP file and then use the call method behind said to call this generated watermark image class code.

$ico _pic is you want to add watermark image watermark, the other parameters are detailed description, if you look at this type of code can be downloaded to save the PHP tutorial file and then use the call method behind the call to invoke this generated watermark image class code.
*/

Class smallpic{

Private $SRC _pic;//Original
Private $ico _pic = "003.png";//Watermark Map
Private $ico _text = "watermark";//Watermark Text
Private $small _width;//thumbnail width
Private $small _height;//thumbnail height
Private $is _ico_pic = true;//whether to add a picture watermark
Private $is _text = true;//If text watermark is added
Private $SRC _x = 20;//watermark in the x-coordinate of the original
Private $SRC _y = 20;//watermark in the y-coordinate of the original
Private $ut = "utf-8";//Text encoding
Private $font _color = "#990000";//Text watermark Color
Private $SAMLL _pic_name = "smallpic";//The name of the small graph
Private $big _pic_name = "bigpic";//The name of the large map


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;
}


/**
* Get some basic information about the image, type array
*/
function Getimageinfo ($image) {
Return @getimagesize ($image);
}

/**
* Load the picture into PHP
* $image incoming image
*/
Function getimage ($image) {
$image _info = $this->getimageinf O ($image);
Switch ($image _info[2]) {
Case 1:
$img = @imagecreatefromgif ($image),
Break,
Case 2:
$img = @imagec Reatefromjpeg ($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 large image
@imagegif ($big _pic, $this->big_pic_name. GIF ");
Output small Image
@imagegif ($new _pic, $this->samll_pic_name. GIF ");
Break
Case 2:
Output large image
@imagejpeg ($big _pic, $this->big_pic_name. JPG ");
Output small Image
@imagejpeg ($new _pic, $this->samll_pic_name. JPG ");
Break
Case 3:
Output large image
@imagepng ($big _pic, $this->big_pic_name. PNG ");
Output small Image
@imagepng ($new _pic, $this->samll_pic_name. PNG ");
Break
}
}

function Checkfile ($file) {
if (!file_exists ($file)) {
Die ("Picture:". $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 proportionally or proportionally
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 hit the picture watermark
if ($this->is_ico_pic) {
//Hit picture watermark
@imagecopy ($big _pic, $new _pic, $this->src_x, $this- >src_y,0,0, $new _pic_info[0], $new _pic_info[1]);
}
//whether to play a text watermark
if ($this->is_text) {
//Set text color
$text _color = @imagecolorallocate ($big _pic, $rgb [0],$ RGB[1], $rgb [2]);
//Convert text encoding
$text = @iconv ($this->ut, "Utf-8", $this->ico_text),
//Text watermark
@imagettftext ($big _pic, 12,0, $this->src_x, $this->src_y, $text _color, "Simkai_0.ttf", $text);
}
//New artboard for new picture
$new _pic = @imagecreatetruecolor ($small _pic_width, $small _pic_height);
//Generate thumbnails
@ Imagecopyresized ($new _pic, $big _pic,0,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);
}

/**
* Function functions within the class convert #000000 to 255,255,255
*/
Private Function Convcolor () {
$rgb = Array ();
$color = Preg_replace ("/#/", "", $this->font_color);
$c = Hexdec ($color);
$r = ($c >>) & 0xFF;
$g = ($c >> 8) & 0xFF;
$b = $c & 0xFF;
$rgb [0] = $r;
$rgb [1] = $g;
$rgb [2] = $b;
return $RGB;
}
}

Calling methods

$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 ();

?>

http://www.bkjia.com/PHPjc/633001.html www.bkjia.com true http://www.bkjia.com/PHPjc/633001.html techarticle $ico _pic is the watermark you want to add watermark image, the other parameters are detailed description, if you are looking for this type of code can be downloaded to save the PHP file and then use the caller after said ...

  • 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.