1: Process-oriented writing method
//Specify picture Path$src= ' 001.png ';//Get picture information$info=getimagesize($src);//get the picture name extension$type= Image_type_to_extension ($info[2],false);//dynamically import images into memory$fun= "imagecreatefrom{$type}";$image=$fun(' 001.png ');//Specify font Color$col= Imagecolorallocatealpha ($image, 255,255,255,50);//Specify font content$content= ' HelloWorld ';//add text to a pictureImagestring ($image, 5,20,30,$content,$col);//Specifying input TypesHeader(' Content-type: '.$info[' MIME ']);//dynamic output images to the browser$func= "image{$type}";$func($image);//Destroying picturesImagedestroy ($image);
2: Object-oriented Implementation method
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 a An array of Rgba * @param $text: What you want to add * manipulate images in memory to add text watermarks to pictures*/ PublicfunctionFontmark ($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*/ PublicfunctionShow () {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 (' 001.png ');$obj->fontmark (20,20,30,Array(255,255,255,60), ' Hello ');$obj->show ();
The above introduces the use of PHP to add text watermark to the image-object-oriented and process-oriented implementation of the two methods, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.