example, PHP adds a watermark code to a picture.
- /*
- Example
- $image = new Gimage ();
- $image->limit = 600;//length and width limit
- $image->wm_text= "www.linuxlaptop.cn";//Watermark Text
- $image->wm_fontfile= "Font/xsuni.ttf";//font file
- $image->wm_color= "#ff0000 ″;
- $image->save_file = "ltcn.jpg";//Save to XX file
- $image->create ("linuxlaptop.jpg");//created from XX file
- */
- /*
- +------------------------------
- | Create thumbnails & watermark-like pictures
- +------------------------------
- */
- Class gimage{
- var $input _type = ""; Enter the format of the picture
- var $output _type = "jpg"; Format of the output picture
- var $limit = 0; Picture size limit
- var $filename = ""; Enter the file name of the picture (it can also be the image data directly)
- var $jpeg _quality = 90; JPEG picture quality
- var $save _file = "; Output file name
- var $wm _text = ""; Watermark text (not supported in Chinese: ' ()
- var $wm _size = 12; Watermark Text Size
- var $wm _angle = 0; Watermark Text Angle
- var $wm _x = 30; Watermark X Coordinate
- var $wm _y = 30; Watermark y-coordinate
- var $wm _color = "#cccccc"; Watermark Color
- var $wm _fontfile = "Geodesic.ttf";//Watermark font file
- function Create ($filename = "")
- {
- if ($filename) $this->filename = $filename;
- if (! $this->input_type) $this->get_type ();
- if (! $this->output_type) $this->output_type = $this->input_type;
- if ($this->input_type = = "jpg") $this->input_type = "jpeg";
- if ($this->output_type = = "jpg") $this->output_type = "jpeg";
- Switch ($this->input_type) {
- Case ' gif ':
- $src _img=imagecreatefromgif ($this->filename);
- Break
- Case ' JPEG ':
- $src _img=imagecreatefromjpeg ($this->filename);
- Break
- Case ' PNG ':
- $src _img=imagecreatefrompng ($this->filename);
- Break
- Default
- $src _img=imagecreatefromstring ($this->filename);
- Break
- }
- $src _w=imagesx ($src _img);
- $src _h=imagesy ($src _img);
- if ($src _w>= $src _h) {
- if ($src _w> $this->limit) {
- $new _w= $this->limit;
- $new _h= ($this->limit/$src _w) * $SRC _h;
- }
- }
- else{
- if ($src _h> $this->limit) {
- $new _h= $this->limit;
- $new _w= ($this->limit/$src _h) * $SRC _w;
- }
- }
- if ($new _h) {
- $DST _img=imagecreatetruecolor ($new _w, $new _h);
- Imagecopyresampled ($dst _img, $src _img,0,0,0,0, $new _w, $new _h,imagesx ($src _img), Imagesy ($src _img));
- }
- else{
- $DST _img = $src _img;
- }
- if ($this->wm_text)
- {
- if (Preg_match ("/([a-f0-9][a-f0-9]) ([a-f0-9][a-f0-9]) ([a-f0-9][a-f0-9])/i", $this->wm_color, $color))
- {
- $red = Hexdec ($color [1]);
- $green = Hexdec ($color [2]);
- $blue = Hexdec ($color [3]);
- }
- $WM _color = Imagecolorallocatealpha ($dst _img, $red, $green, $blue, 90);
- Imagettftext ($dst _img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm _color, $this- Wm_fontfile, $this->wm_text);
- }
- if ($this->save_file)
- {
- Switch ($this->output_type) {
- Case ' gif ':
- $src _img=imagepng ($dst _img, $this->save_file);
- Break
- Case ' JPEG ':
- $src _img=imagejpeg ($dst _img, $this->save_file, $this->jpeg_quality);
- Break
- Case ' PNG ':
- $src _img=imagepng ($dst _img, $this->save_file);
- Break
- Default
- $src _img=imagejpeg ($dst _img, $this->save_file, $this->jpeg_quality);
- Break
- }
- }
- Else
- {
- Header ("Content-type:image/{$this->output_type}");
- Switch ($this->output_type) {
- Case ' gif ':
- $src _img=imagepng ($dst _img);
- Break
- Case ' JPEG ':
- $src _img=imagejpeg ($dst _img, "", $this->jpeg_quality);
- Break
- Case ' PNG ':
- $src _img=imagepng ($dst _img);
- Break
- Default
- $src _img=imagejpeg ($dst _img, "", $this->jpeg_quality);
- Break
- }
- }
- Imagedestroy ($dst _img);
- }
- function Get_type ()//Get image file type
- {
- $name _array = Explode (".", $this->filename);
- if (Preg_match ("/\." ( Jpg|jpeg|gif|png) $/", $this->filename, $matches))
- {
- $this->input_type = Strtolower ($matches [1]);
- }
- Else
- {
- $this->input_type = "string";
- }
- }
- }
Copy Code |