Use ImageMagick of PHP;

Source: Internet
Author: User
I have previously written a project that requires the PHP Imagick class for image operations. I personally feel that there are many Imagick documents, but they are messy. I will explain the simple use of Imagick through an example;

(1) create a new image with a width of PX, a height of PX, and a black image in png format.

$img =new Imagick();$img->newImage(500,300,'black','png')

(2) overlapping images;

Suppose we select $ img in example 1 as the background. in this case, we overlay the second image to the image and select a Spurs gdp combination;

The path of image 2 is assumed to be $ imageSrc = "/home/XXXX/spurs.png ";

Step 1: instantiate this image

$ Imgtwo = new Imagick ($ imageSrc );

To overlay the image, we need to set the same size for the two images. First, we need to specify the size of the first image (ps: Here we can also specify the size ).

$ Height = $ img-> getImageHeight (); // Get image 1 height $ width = $ img-> getImageWidth (); // Get image 1 width

Step 2: resize the image

$imgtwo->resizeImage($width,$height,Imagick::FILTER_LANCZOS,1);

ResizeImage parameter description

bool Imagick::resizeImage ( int $columns , int $rows , int $filter , float $blur [, bool $bestfit = false ] )

Parameter description:

Columns image width

Rows Image height

Filter, used to filter images, with Gaussian filte based on the situation

Blur = 1 is virtual, and blur =-1 is sharpening

Part 3: overlay images

Use compositeImage ();

bool Imagick::compositeImage ( Imagick $composite_object , int $composite , int $x , int $y [, int $channel = Imagick::CHANNEL_ALL ] )

Parameter description:

Composite_object: Imagick object used for merged images

Composite: combines operations and defines operation constants. For details, see the merging operation constant list.

X: The abscissa relative to the top left of the image vertex (0, 0)

Y: the vertical coordinate relative to the top left of the image vertex (0, 0)

Channel: you can input a channel constant to enable the channel mode. To support multiple channels, you can merge multiple channel constants through binary operations.

Ps: Here we will overwrite Image 2 to image 1.

$img->compositeImage($imgtwo,$image->getImageCompose(),0,0)

The generated image is as follows:

Last

1. you can view images directly on the webpage, but add a header;

header("Content-Type: image/png");echo $img;

2. images can be generated in a specified directory;

Img.png $ file = "./img.png" is generated under the current directory; $ img-> writeimage(?file==}this will generate the image img.png in the current directory;

(3) configure text on the image

Use the ImagickDraw class;

Step 1 instantiate the ImagickDraw class:

$draw=new ImagickDraw();

Set font color

$draw->setFillColor(new ImagickPixel('white'));

Set font size

$draw->setFontSize('25');

Set font

$draw->setFont("../fonts/Arial.ttf");

Set font direction

$draw->setTextAlignment(Imagick::ALIGN_RIGHT);

Ps:

Imagick: ALIGN_RIGHT to the right

Imagick: Left of ALIGN_LEFT

Imagick: Intermediate ALIGN_CENTER

Set the font encoding format

$draw->setTextEncoding("utf-8")

Draw text

$draw->annotation(200,200,'GDP');

Draw on the bottom plate;

$img->drawImage($draw);

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.