PHP uses imagick extension to merge images. phpimagick

Source: Internet
Author: User

PHP uses imagick extension to merge images. phpimagick

This example describes how PHP uses imagick extension to merge images. We will share this with you for your reference. The details are as follows:

My girlfriend is a foreign trade company. Recently, I need to create a product book with 20 pictures on every A4 paper. I want to know a small case. My buddy will click ps. However, when she sent the picture to me, she was almost scared. There were nearly 10000 images, so the ps would have to vomit blood and die...

Fortunately, I will still click php. Well, write a small program to complete the puzzle. Because the images are arranged by numbers and each image is required to be numbered, my idea is: 1. first, scale all the images to a uniform size. combine each image and number into one figure 3. combine 20 images into one image. Image processing uses ImageMagick and php imagick extensions. The code below is annotated in detail:

Step 1:

// Step 1: adjust the size to 590x590 $ a = ROOT. '/'. 'A'; // scan directory $ dirA = scandir ($ a); $ im = new Imagick; foreach ($ dirA as $ item) {// skip directory and thumbnail if ($ item = '. '| $ item = '.. '| strstr ($ item ,'. db') {continue;} // read the image $ im-> readImage ($. '/'. $ item); // obtain the image width x height $ geo = $ im-> getImageGeometry (); if ($ geo ['width'] ==== 590 & $ geo ['height'] === 590) {// the width and height match, skip} else {// resize to 590 x 590im-> resizeImage (590,590, Gmagick: FILTER_UNDEFINED, 1, TRUE );} // Save the image to another directory $ im-> writeImage (ROOT. '/_ /'. $ item); // release the resource $ im-> destroy ();}

Step 2:

// Step 2: Merge the image and name // scan the directory $ files = scandir (ROOT. '/_ a'); $ k = 0; foreach ($ files as $ item) {// skip directory and thumbnail if ($ item = '. '| $ item = '.. '| strstr ($ item ,'. db ') {continue;} // The width of the text image $ twidth = 570; // The height of the text image $ theight = 141; // obtain the image name $ pathinfo = pathinfo ($ item); $ filename = $ pathinfo ['filename']; // initialize the image object $ text = new Imagick; // initialize the Drawing Object $ draw = new ImagickDraw; // set the font. Here is the $ draw-> setFont ('font/msyh. ttf'); // text size $ draw-> setFontSize (40); // text color $ draw-> setFillColor (new ImagickPixel ('#000000 ')); // text alignment $ draw-> setTextAlignment (Imagick: ALIGN_LEFT); // obtain text information, mainly length and width, because the image is centered $ a = $ text-> queryFontMetrics ($ draw, $ filename ); // Add the text $ draw-> annotation ($ twidth-$ a ['textidths'])/2, 80, $ filename ); // create an image $ text-> newImage ($ twidth, $ theight, new ImagickPixel ('# ffffff ')); // image format $ text-> setImageFormat ('png '); // draw an image $ text-> drawImage ($ draw ); // create a blank image for canvas $ canvas = new Imagick; $ canvas-> newimage (570,661, 'white'); $ canvas-> setImageFormat ('png '); // read the image $ pic = new Imagick; $ pic-> readImage (ROOT. '/_ /'. $ item); $ pic-> scaleimage (470,470, TRUE); // merge the image to the canvas $ canvas-> compositeImage ($ pic, Imagick: COMPOSITE_OVER, 50, 50); // merge the text into the canvas $ canvas-> compositeimage ($ text, Imagick: COMPOSITE_OVER, 0,520 ); // Save the image to another directory $ canvas-> writeimage (ROOT. '/com_a /'. $ item); $ k ++; echo "{$ k} files proceeded. \ n ";}

Step 3:

// Step 3: Merge every 20 sheets to one page // scan the directory $ files = scandir (ROOT. '/com_a'); // group images $ I = $ j = 0; $ group = array (); foreach ($ files as $ item) {if ($ item = '. '| $ item = '.. '| strstr ($ item ,'. db') {continue;} $ I ++; $ group [$ j] [] = $ item; if ($ I % 20 = 0) {$ j ++ ;}total total = count ($ group); // splice images by group, A4 paper size, 4x5 combination method foreach ($ group as $ k => $ v) {$ canvas = new Imagick; $ canvas-> newimage (2480,350 8, 'white '); $ canvas-> setimageformat ('png '); $ I = $ j = 0; foreach ($ v as $ item) {$ im = new Imagick (ROOT. '/com_a /'. $ item); // The left margin of 150 is reserved for $ x = 150 + $ I * 570; // The top margin of 130 $ y = 130 + $ j * 661; $ canvas-> compositeimage ($ im, Imagick: COMPOSITE_OVER, $ x, $ y); // if ($ I + 1) for every four rows) % 4 = 0) {$ I = 0; $ j ++;} else {$ I ++; }}$ canvas-> writeimage (ROOT. '/merge_a /'. $ k. '.png '); $ c = $ k + 1; echo "Group {$ c}/{$ total} done. \ n ";}

PS: Here are some useful image processing tools for your reference:

Online image conversion BASE64 tool:
Http://tools.jb51.net/transcoding/img2base64

ICO icon generation tool:
Http://tools.jb51.net/aideddesign/ico_img

Online Email icon creation tools:
Http://tools.jb51.net/email/emaillogo

Online image format conversion (jpg/bmp/gif/png) tool:
Http://tools.jb51.net/aideddesign/picext

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.