This example describes how PHP uses Imagick to generate pictures. Share to everyone for your reference. Specifically as follows:
Use Imagick to generate pictures here
Solve the problem of writing garbled text in the picture, add supported fonts
Public Function Getpic () {header (' content-type:text/html; Charset=utf-8 ');
$text = ' Cofco Tun River (sh600737) ';//Cofco River (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 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), $num *8
), 1, $style);
$this ($imagick, substr ($watermark, $num, 1),->add_text ($num *8), 5, 1, $style);
}//return;
$style [' font_size '] = 20;
$style [' fill_color '] = ' #FF0000 '; $style [' font '] = './msyh.ttf '; Microsoft ya Black font to solve Chinese garbled//$text =mb_convert_encoding ($text, ' UTF-8 ');
Iconv ("GBK", "Utf-8//ignore", $text); $this->add_text ($imagick, $text, 2, 0, $sTyle);
Header (' Content-type: ' Strtolower ($imagick->getimageformat ()));
echo $imagick->getimagesblob (); //Add 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->annotateima
GE ($draw, $x, $y, $angle, $text);
}} else {$imagick->annotateimage ($draw, $x, $y, $angle, $text);
}
}
I hope this article will help you with your PHP programming.