How PHP uses Imagick to generate pictures _php tips

Source: Internet
Author: User
Tags transparent color php programming strlen

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.

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.