PHP Image Add text watermark Method Summary

Source: Internet
Author: User
Tags image identifier

Method One:

<?PHPHeader("Content-type:text/html;charset=utf-8");//Specify picture Path$src= "Img/a.png";//Get picture information$info=getimagesize($src);//get the picture name extension$type= Image_type_to_extension ($info[2],false);//echo $type;//exit;//the picture into memory dynamically$fun= "imagecreatefrom{$type}";$image=$fun("Img/a.png");//Var_dump ($image);//exit;//Specify font color$col= Imagecolorallocatealpha ($image, 255,255,255,50);//Var_dump ($col);//exit;//Specify font content$content= ' HelloWorld ';//$content = "<span style= ' color:red;font-style:italic; ' > ". $content." </span> ";//echo $content;//exit;//add text to the pictureImagestring ($image, 80,200, 90,$content,$col);//Specifying input TypesHeader(' Content-type: '.$info[' MIME ']);//dynamic output images to the browser$func= "image{$type}";$func($image);//Destroying picturesImagedestroy ($image); ?>

Method Two:

<?PHPHeader("Content-type:text/html;charset=utf-8");classImage_class {Private $image; Private $info; /** * @param $src: Picture path * load Picture into memory*/  function__construct ($src){    $info=getimagesize($src); $type= Image_type_to_extension ($info[2],false); $thisinfo =$info; $this->info[' type '] =$type; $fun= "Imagecreatefrom".$type; $this-image =$fun($src); }  /** * @param $fontsize: Font size * @param $x: The x position of the font in the picture * @param $y: The y position of the font in the picture * @param $color: The color of the font is an array containing RGBA * @param $text: What you want to add * manipulate images in memory to add text watermarks to pictures*/   Public functionFontmark ($fontsize,$x,$y,$color,$text){    $col= Imagecolorallocatealpha ($this->image,$color[0],$color[1],$color[2],$color[3]); Imagestring ($this->image,$fontsize,$x,$y,$text,$col); }  /** Output images to the browser*/   Public functionShow () {Header(' Content-type: '.$this-Info[' MIME ']); $fun= ' image '.$this->info[' type ']; $fun($this-image); }  /** * Destroy pictures*/  function__destruct () {Imagedestroy ($this-image); }}//calls to a class$obj=NewImage_class (' Img/a.png ');$obj->fontmark (20,150,50,Array(255,255,0,60), ' Hello ');$obj-Show ();?>

Method Three:

<?PHPHeader("Content-type:text/html;charset=utf-8");//ini_set ("Display_errors", true); /*How to add a text watermark to a picture*/$DST _path= ' Img/b.png ';$DST= Imagecreatefromstring (file_get_contents($DST _path));/*imagecreatefromstring ()--Creates a new image from the image stream in the string, returns an image identifier that expresses the image Image format from a given string that is automatically monitored as long as PHP supports JPEG,PNG,GIF,WBMP,GD2.*///echo "AAA";//exit;$font= ' Font/arial.ttf ';$black= Imagecolorallocate ($DST, 255, 0, 0); Imagefttext ($DST, 50, 50, 160, 180,$black,$font, ' Hello ');/*Imagefttext ($img, $size, $angle, $x, $y, $color, $fontfile, $text) $ IMG Returns image resource by image creation function size to use the font size of the watermark angle (angle) The tilt angle of the text, if it is 0 degrees representing the text from left to right, if it is 90 degrees from the top down X, Y the starting position of the first text of the watermark text color is the fontfile of the watermark text and you want to use the path of the TrueType font*/List($DST _w,$DST _h,$DST _type) =getimagesize($DST _path);/*list (mixed $varname [, mixed $ ...] --Assigning values in an array to some variables like array (), this is not a real function, but a language structure, and List () assigns a set of variables in one step*//*What information can I get from getimagesize ()? The GetImageSize function returns all information about the image, including size, type, and so on*/Switch($DST _type){   Case1://GIF    Header("Content-type:image/gif"); Imagegif ($DST);  Break;  Case2://JPG    Header("Content-type:image/jpeg"); Imagejpeg ($DST);  Break;  Case3://PNG    Header("Content-type:image/png"); Imagepng ($DST);  Break; default: Break; /*imagepng--Output the image in PNG format to a browser or file Imagepng () exports the GD image Stream (image) in PNG format to the callout output (usually a browser), or if filename is given with filename, it is output to a file */}imagedestroy ($DST); ?>

PHP Image Add text watermark Method Summary

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.