PHP class Library to the existing image plus text watermark, the code is not very perfect, welcome everyone to advise! The code is as follows:
-
- /*php Picture Plus text Watermark class Library
- qq:3697578482 sad song.
- This kind of library temporarily supports only text watermark, the position is the bottom right corner, color random
- Call Method:
- 1, in the need to watermark the top of the file to introduce the class library:
- Include_once ' imageclass.php ';
- 2. Declaring a new class:
- $tpl =new Image_fu;
- 3. Provide parameters for image watermark:
- $TPL->img (image path, watermark text, font path, font size, font angle);
- For example: $tpl->img (' abc.jpg ', ' This is the watermark text ', ' Ziti.ttf ', 30,0)
- */
- 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 a 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];//Pictures 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 image failed ');
- }
- }
- Show pictures
- function Img_show () {echo ' image. ' border= ' 0 "alt=" '. $this->img_text. '/> ';}
- Freeing memory
- Private Function img_nothing () {
- unset ($this->img_info);
- Imagedestroy ($this->img_im);
- }
- }
- ?>
Copy Code |