PHP implementation Picture Add text watermark program code

Source: Internet
Author: User
The code is as follows Copy Code


/*php image with text Watermark class Library
This class library only supports the text watermark temporarily, the position is the lower right corner, the color is random

Call Method:
1, the need to add watermark to the top of the file to introduce class library:
Include_once ' imageclass.php ';
2, declare the new class:
$tpl =new Image_fu;
3, to the image watermark provides parameters:
$TPL->img (image path, watermark text, font path, font size, font angle);
For example: $tpl->img (' abc.jpg ', ' This is a watermark text ', ' Ziti.ttf ', 30,0)
*/

The code is as follows:

watermark.php
<?php
Class image_fu{

Private $image;
Private $img _info;
Private $img _width;
Private $img _height;
Private $img _im;
Private $img _text;
Private $img _ttf= ';
Private $img _new;
Private $img _text_size;
Private $img _JD;

function img ($img = ', $txt = ', $ttf = ', $size =12, $jiaodu =0) {
if (Isset ($img) &&file_exists ($img)) {//detect if picture exists
$this->image = $img;
$this->img_text= $txt;
$this->img_text_size= $size;
$this->img_jd= $jiaodu;
if (file_exists ($ttf)) {
$this->img_ttf= $ttf;
}else{
Exit (' Font file: '. $ttf. ') does not exist! ');
}
$this->imgyesno ();
}else{
Exit (' picture file: '. $img. ' does not exist ');
}
}

Private Function Imgyesno () {

$this->img_info =getimagesize ($this->image);
$this->img_width = $this->img_info[0];//picture width
$this->img_height= $this->img_info[1];//Picture High

Detecting picture types
Switch ($this->img_info[2]) {
Case 1: $this->img_im = imagecreatefromgif ($this->image);
Case 2: $this->img_im = imagecreatefromjpeg ($this->image);
Case 3: $this->img_im = imagecreatefrompng ($this->image);
Default:exit (' image format does not support Watermark ');
}

$this->img_text ();
}

Private Function Img_text () {

Imagealphablending ($this->img_im,true);

Set color
$color =imagecolorallocate ($this->img_im,rand (0,255), Rand (0,255), Rand (0,255));
$txt _height= $this->img_text_size;
$txt _jiaodu= $this->img_jd;
$ttf _im=imagettfbbox ($txt _height, $txt _jiaodu, $this->img_ttf, $this->img_text);
$w = $ttf _im[2]-$ttf _im[6];
$h = $ttf _im[3]-$ttf _im[7];
$w = $ttf _im[7];
$h = $ttf _im[8];

unset ($ttf _im);

$txt _y = $this->img_height-$h;
$txt _x = $this->img_width-$w;
$txt _y = 0;
$txt _x = 0;

$this->img_new= @imagettftext ($this->img_im, $txt _height, $txt _jiaodu, $txt _x, $txt _y, $color,

$this->img_ttf, $this->img_text);

@unlink ($this->image);//Delete picture
Switch ($this->img_info[2]) {//Get the format of the background picture
Case 1:imagegif ($this->img_im, $this->image);
Case 2:imagejpeg ($this->img_im, $this->image);
Case 3:imagepng ($this->img_im, $this->image);
Default:exit (' watermark picture failed ');
}

}

Show pictures
function Img_show () {echo ' image. ' "border=" 0 "alt=" '. $this->img_text. ' "/>";}

Free memory
Private Function img_nothing () {
unset ($this->img_info);
Imagedestroy ($this->img_im);
}

}

$ADDWK = new Image_fu ();
$addwk->img (' 2.jpg ', ' testwatermark ', ' Hua Kang Maiden font. TTF ', 33,0);
$addwk->img_show (); Call Display Picture

?>

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.