Image watermark and thumbnail image

Source: Internet
Author: User
Tags imagejpeg rtrim
<?php/** * @title: Image watermark * @autor: Zms * @version: 2011-7-26 * Because of the limitations of the GD library, you can now use only gif,jpg,png three formats */////////////Use example//instantiation/ /Parameter Description: path, whether to use Watermark (default:false) $img = new Image_watermark (true);//Instantiate//Set original figure $img->dst_image (' cartoon. jpg '); Add text//Parameter description: Content, Position (default: middle position of picture), transparency (defulat:127 0-127), size (default:12), Angle (default:0), Color (rgb:default : 255,255,255), (font: DEFAULT:SHONGTI.TTF, code: DEFAULT:UTF-8) $img->set_font (' Test text,www.baidu.com ', "300,422", 50, 15, 50, ' 36,93,219 '); $img->set_font (' Test text,www.baidu.com '); Add watermark Picture//Parameter Description: path, transparency (default:0), location: default: Image in the lower right corner of the image in the coordinates from X3, x4 start, Width is x5, the height of the X6 is copied to the original image in the coordinates x1 and x2 position. '//If the height-width setting of the watermark is greater than the size of the original figure, do not hit the watermark $img->src_image (' 2010723136342131.jpg ', ' 80 ', "0,0,460,150,200,200"); $img->src_image (' 2010723136342131.jpg ', ' 80 '); $img->src_image (' 2010723136342131.jpg ', ' 80 ', ' 0,0,55,56,150,520 '); Zoom picture (0-?)//Parameter description: Multiples (defautl:1), (position x1,x2,x3,x4) copies the coordinates of the image from X3, X4, to the position where the coordinates are X1 and x2 in the Clipboard. $img->resize_image (1); Write the watermark image to local//Parameter description: Overwrite the original picture (Default:false)//$img->write_img (FALSE); Output watermark image to browser $img->print_img ();  Class Image_watermark {private $DST _img = "";//Original picture True Color image private $dst _img_url = "";//original image address private $dst _img_info = "";//original picture information private $src _img = "";//Watermark picture True Color Image private $src _img_url = "";//Watermark Image Address private $src _img_info = ""    ;//Watermark Picture information private $resize _img = "";//Zoom picture//Set the image name of the input code, solve the Chinese name picture garbled problem private $img _in_encode = "UTF-8";    function Set_img_in_encode ($encode) {$this->img_in_encode = $encode; }//-------------------/** * @param object $dst _image: Original picture * @param object $use [optional]: true, false whether Use watermark * @param object $cover [optional]: true, false to overwrite old picture (true: Overwrite false: Generate new diagram) * @return */function __con struct ($use = False) {header ("content-type:text/html;        Charset=utf-8 ");                if ($use) {//Use watermark if (!extension_loaded (' GD ')) {echo "GD library is not open, please set Php.int to open GD library";      exit;//Interrupt All operations      }} else {echo "does not open the Watermark function, please turn it on when instantiating the method"; exit;//Interrupt all operations}}//end __construct/** * cleanup * @return * * Function __destruct () {@i    Magedestroy (); }/** * Set original figure * @param object $DST _image * @return */function Dst_image ($dst _image) {//        The Chinese name picture garbled solves $dst _image = @iconv ($this->img_in_encode, "gb2312", $dst _image);        $this->dst_img_url = $dst _image;        1. Get picture information $this->dst_img_info = @getimagesize ($this->dst_img_url);            2. Verify if the picture is available if ($this->check_type ($this->dst_img_info[2]) {//2.1. Get picture information, identify the code, create a true color image        $this->create_image ($this->dst_img_info[2], ' DST '); } else {echo "original picture file format Error!            Now only support: Gif,jpg,png ";  exit;//Interrupt All Operations}}/** * Set watermark * @param object $SRC _image: Watermark Address * @param object $SRC _image_style [OPTIONAL]: x1,x2,x3,x4,x5,x6 * The coordinates in the image from X3, x4 start, Width is x5, highA portion of the X6 is copied to the position where the coordinates are X1 and x2 in the original image. * @param object $transparent [optional]: Transparency has a value range from 0 to * * @return */function Src_image ($src _image, $TRANSP Arent = +, $src _image_style = "0,0,0,0,100,100") {$transparent = $transparent >= 0 && $transparent &L t;= 100?        $transparent: 100;        The Chinese name picture garbled solves $src _image = @iconv ($this->img_in_encode, "gb2312", $src _image);        $this->src_img_url = $src _image;        1. Get picture information $this->src_img_info = @getimagesize ($this->src_img_url);        $src _image_style = Trim (LTrim (RTrim ($src _image_style, ","), ","));                $SRC _image_style_array = Split (",", $src _image_style);            if ($src _image_style = = "0,0,0,0,100,100") {$src _image_style_array[0] = $this->dst_img_info[0]-100;        $SRC _image_style_array[1] = $this->dst_img_info[1]-100; }//To determine whether the watermark will be larger than the size of the original diagram, if it is exceeded, do not watermark if ($src _image_style_array[4] < $this->dst_img_info[0] && $src _image_style_array[5] < $this->dst_img_info[1]) {//2. Verify that the picture is available if ($this->ch Eck_type ($this->src_img_info[2])) {//2.1. Get picture information, identify the encoding, create a true color image $this->create_image ($this                ->src_img_info[2], ' src '); @imagecopymerge ($this->dst_img, $this->src_img, $src _image_style_array[0], $src _image_style_array[1], $src _ IMAGE_STYLE_ARRAY[2], $src _image_style_array[3], $src _image_style_array[4], $src _image_style_array[5], $            Transparent); } else {echo "watermark picture file format Error!                Now only support: Gif,jpg,png "; exit;//Interrupt All Operations}}}/** * Identify the encoding, create a true color image * @param object $img _info: Image Format * @param object $ Type: The identity image is a watermark or the original ' DSK ' ' src ' * @return */function Create_image ($img _info, $type) {if ($type = = ' DST '        ) {$url = $this->dst_img_url;        } elseif ($type = = ' src ') {$url = $this->src_img_url; } switch ($img_info) {case "1": $img = @imagecreatefromgif ($url);            Break                Case "2": $img = @imagecreatefromjpeg ($url);            Break                Case "3": $img = @imagecreatefrompng ($url);            Break        Default:break;        }//end switch if ($type = = ' DST ') {$this->dst_img = $img;        } elseif ($type = = ' src ') {$this->src_img = $img; }}//end create_image/** * Set text * @param object $content: Content * @param object $transparent [Optiona L]: Transparency has values from 0 to 127.  0 means completely opaque, 127 is fully transparent * @param object $fontfile [optional]: Font file (. ttf) * @param object $col [optional]: Font color RGB "255,255,255" * @param object $coordinate [optional]: coordinates "x, y" * @param object $size [optional]: size int * @ param object $angle [optional]: angle representation of the angle, 0 degrees of text read from left to right. Higher values indicate counterclockwise rotation * @param object $encode [optional]: Encode "UTF-8"    * @return */function Set_font ($content, $coordinate = "0,0", $transparent = 0, $size = A, $angle = 0, $col = "0,0,0", $fontfile = "Shongti.ttf") {$transparent = $transparent >= 0 && $transparent <= 127? $tran        sparent:0;        1. The color split $col = Trim (LTrim (RTrim ($col, ","), ","));        $col _array = Split (",", $col);        $col = Imagecolorallocate ($this->dst_img, $col _array[0], $col _array[1], $col _array[2]);               $col = @imagecolorallocatealpha ($this->dst_img, $col _array[0], $col _array[1], $col _array[2], $transparent);        $coordinate = Trim (LTrim (RTrim ($coordinate, ","), ","));        $coordinate _array = Split (",", $coordinate);            if ($coordinate = = "0,0") {$coordinate _array[0] = $this->dst_img_info[0]/2;        $coordinate _array[1] = $this->dst_img_info[1]/2; } @imagettftext ($this->dst_img, $size, $angle, $coordinate _array[0], $coordinate _array[1], $col, $fontfile, $content); }//end set_font/** * Zoom picture * @param object $resize [optional]: Zoom * @param object $coordinate [Optiona     L]: Clipboard start Position: x1,x2,x3,x4 * Copy the coordinates of the image from X3, X4 to the position where the coordinates are X1 and x2 in the Clipboard. * @return */function resize_image ($resize = 1, $coordinate = "0,0,0,0") {$resize = $resize > 0? $resiz        E:1;  Create clipboard $this->resize_img = @imagecreatetruecolor ($this->dst_img_info[0] * $resize, $this->dst_img_info[1]        * $resize);        $coordinate = Trim (LTrim (RTrim ($coordinate, ","), ","));        $coordinate _array = Split (",", $coordinate); Cut Image @imagecopyresized ($this->resize_img, $this->dst_img, $coordinate _array[0], $coordinate _array[1], $coor DINATE_ARRAY[2], $coordinate _array[3], $this->dst_img_info[0] * $resize, $this->dst_img_info[1] * $resize, $    This->dst_img_info[0], $this->dst_img_info[1]); /** * Writes the watermark image to local * @param object $cover [optional]: Overwrite original picture * @return */    function write_img ($cover = False) {$c = $cover? $this->str_img_name (): $this->str_img_name (TRUE);                    Switch ($this->dst_img_info[2]) {case "1": if ($this->resize_img! = "") {                @imagegif ($this->resize_img, $c);                } else {@imagegif ($this->dst_img, $c);            } break;                Case "2": if ($this->resize_img! = "") {@imagejpeg ($this->resize_img, $c);                } else {@imagejpeg ($this->dst_img, $c);            } break;                Case "3": if ($this->resize_img! = "") {@imagepng ($this->resize_img, $c);                } else {@imagepng ($this->dst_img, $c);            } break;        Default:break; }//end Switch}//end write_img/** * willWatermark after image output to browser * @return */function print_img () {switch ($this->dst_img_info[2]) {case "1")                : Header ("Content-type:image/gif");                if ($this->resize_img! = "") {@imagegif ($this->resize_img);                } else {@imagegif ($this->dst_img);            } break;                Case "2": Header ("Content-type:image/jpeg");                if ($this->resize_img! = "") {@imagejpeg ($this->resize_img);                } else {@imagejpeg ($this->dst_img);            } break;                Case "3": Header ("Content-type:image/png");                if ($this->resize_img! = "") {@imagepng ($this->resize_img);                } else {@imagepng ($this->dst_img);            } break;    Default:break;    }//end Switch}//end write_img/** * Verify file Format * @param object $type: Picture format * 1:gif 2:jpg,jpeg 3:PN G * @return */function Check_type ($type) {return $type = = "1"? true: $type = = "2"? True: $type = = "3"?    Ture:false;        }//end check_type/** * Intercept filename * $new: New picture * @return */function Str_img_name ($new = False) { if (Strstr ($this->dst_img_url, "/")) {//Determine if the file name is the last level $path = Trim (substr ($this->dst_img_url, 0, STRRP            OS ($this->dst_img_url, "/"))); Intercept file name $file _name = Trim (substr ($this->dst_img_url, Strrpos ($this->dst_img_url, "/"), strlen ($this->d        (St_img_url)));        } else {$file _name = Trim ($this->dst_img_url);        if ($new) {$file _name = Str_replace (".", "_new.", $file _name);    } return $path. $file _name; }//end str_img_name}//end class?>
  • 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.