Php uses Imagick to generate images,
This example describes how to use Imagick to generate images in php. Share it with you for your reference. The details are as follows:
Use Imagick to generate images
Fixed the problem of Garbled text writing in images and added supported fonts.
Public function getPic () {header ('content-Type: text/html; charset = UTF-8 '); $ text = 'zhongliang Tunhe (sh600737)'; // Zhongliang Tunhe (sh600737) $ watermark = '305988103123zczcxzas '; $ len = strlen ($ text); $ width = 10.5 * ($ len-8)/3*2 + 8); $ height = 26; $ imagick = new Imagick (); $ color_transparent = new ImagickPixel ('# ffffff'); // transparent color $ imagick-> newImage ($ width, $ height, $ color_transparent, 'jpg '); // $ imagick-> borderimage (' #000000 ', 1, 1); $ style ['font _ size'] = 12; $ style ['fill _ color'] = '#000000'; for ($ num = strlen ($ watermark); $ num> = 0; $ num --) {$ this-> add_text ($ imagick, substr ($ watermark, $ num, 1), 2 + ($ num * 8), 30, 1, $ style ); $ this-> add_text ($ imagick, substr ($ watermark, $ num, 1), 2 + ($ num * 8), 5, 1, $ style );} // return; $ style ['font _ size'] = 20; $ style ['fill _ color'] = '# ff0000 '; $ style ['font'] = '. /msyh. ttf'; // The font solves Chinese garbled characters. // $ text = mb_convert_encoding ($ text, 'utf-8'); // iconv ("GBK ", "UTF-8 // IGNORE", $ text); $ this-> add_text ($ imagick, $ text, 2, 20, 0, $ style); header ('content-type: '. strtolower ($ imagick-> getImageFormat (); echo $ imagick-> getImagesBlob ();} // Add the watermark text public function add_text (& $ imagick, $ text, $ x = 0, $ y = 0, $ angle = 0, $ style = array () {$ draw = new ImagickDraw (); if (isset ($ style ['font']) $ draw-> setFont ($ style ['font']); if (isset ($ style ['font _ size']) $ draw-> setFontSize ($ style ['font _ size']); if (isset ($ style ['fill _ color']) $ draw-> setFillColor ($ style ['fill _ color']); if (isset ($ style ['under _ color']) $ draw-> setTextUnderColor ($ style ['under _ color']); if (isset ($ style ['font _ family ']) $ draw-> setfontfamily ($ style ['font _ family']); if (isset ($ style ['font']) $ draw-> setfont ($ style ['font']); $ draw-> settextencoding ('utf-8'); if (strtolower ($ imagick-> getImageFormat () = 'gif') {foreach ($ imagick as $ frame) {$ frame-> annotateImage ($ draw, $ x, $ y, $ angle, $ text) ;}} else {$ imagick-> annotateImage ($ draw, $ x, $ y, $ angle, $ text );}}
I hope this article will help you with php programming.