Example, PHP code to add watermark to images.
-
- /*
- //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");//Create from xx file
- */
- /*
- +------ -----------------------
- | Generate thumbnails & watermarked image classes
- +-------------- ----------------
- */
- Class Gimage{
-
- var $input_type = ""; //Input image format
- var $output_type = "jpg"; //Output Image format
- var $limit = 0; // Image size limit
- var $filename = ""; // Enter the file name of the image (can also be image data directly)
- var $jpeg_quality = 90; // jpeg image quality
- var $save_file = ''; //Output file name
- var $wm_text = ""; //Watermark text (Chinese is not supported:'( )
- 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 the 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
|