1: Process-oriented writing method
Specify picture path $src = ' 001.png ';//Get picture information $info = getimagesize ($SRC);//Get picture extension $type = image_type_to_extension ($info [2],false) ;//Dynamically import the picture into memory $fun = "imagecreatefrom{$type}"; $image = $fun (' 001.png ');//Specify font Color $col = Imagecolorallocatealpha ($ IMAGE,255,255,255,50);//Specify the font content $content = ' HelloWorld ';//Add text to the picture imagestring ($image, 5,20,30, $content, $col);// Specifies the input type header (' Content-type: '. $info [' MIME ']);//dynamic output image to the browser $func = "image{$type}"; $func ($image);// Destroy picture Imagedestroy ($image);
2: Object-oriented Implementation method
Class Image_class {private $image; Private $info; /** * @param $src: Picture path * Load image into memory */function __construct ($src) {$info = getimagesize ($SRC); $type = Image_type_to_extension ($info [2],false); $this info = $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 containing RGBA * @param $text: What you want to add * manipulate images in memory, add text watermarks to pictures */Public Function Fontmark ($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 picture to the browser */Public function Show () {header (' Content-type: '. $this, info[' mime '); $fun = ' image '. $this->info[' type ']; $fun ($this->image); } /* * Destroy Picture * * Function __destruct () {Imagedestroy ($this->image); }}//call to class $obj = new Image_class (' 001.png '), $obj->fontmark (20,20,30,array (255,255,255,60), ' Hello '), $obj- Show ();