How php uses Imagick to generate images

Source: Internet
Author: User
Tags transparent color
This article describes how to use Imagick to generate images in php. The example shows how to use Imagick to generate images in php.

This article describes how to use Imagick to generate images in php. The example shows how to use Imagick to generate images in php.

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
Solves the problem of Garbled text writing in images, and adds 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.

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.