How PHP uses Imagick extensions to implement merged images

Source: Internet
Author: User
This article mainly introduces PHP using Imagick Extension Implementation of the merged image method, combined with examples of PHP based on the imagick extension of the process of processing pictures of the specific steps and related operations skills, the need for friends can refer to the next


The first step:

Step1: Adjust the dimensions to 590 x 590$a = ROOT. '/' .  ' A ';//scan Directory $dira = scandir ($a); $im = new Imagick;foreach ($dirA as $item) {//Skip directories and thumbnails if ($item = = = '. ' | | $item = = = ') ' || Strstr ($item, '. db ')) {continue;} Read the picture $im->readimage ($a. '/' . $item);//Get picture width x High $geo = $im->getimagegeometry (); if ($geo [' width '] = = = 590 && $geo [' height '] = = = 590) {//Width high match, jump OVER} else {//resize to 590 x 590im->resizeimage (590, 590, gmagick::filter_undefined, 1, TRUE);} Save the picture to a different directory $im->writeimage (ROOT. '/_a/'. $item);//Release Resource $im->destroy ();}

Step Two:

Step2: Merge picture and name//scan Directory $files = Scandir (ROOT. '/_a '); $k = 0;foreach ($files as $item) {//Skip directories and thumbnails if ($item = = = '. ' | | $item = = = ') ' | | strstr ($item, '. db ') {continue ;} The width of the text picture $twidth = 570;//The high $theight of the text picture = 141;//Gets the picture name $pathinfo = PathInfo ($item); $filename = $pathinfo [' filename '];//initial Image Object $text = new imagick;//Initialize the drawing object $draw = new imagickdraw;//Set the font, here is the Microsoft Black $draw->setfont (' Font/msyh.ttf ') placed under the font of the website )///Text size $draw->setfontsize (40);//Text color $draw->setfillcolor (new Imagickpixel (' #000000 '));//Text alignment $draw-> Settextalignment (imagick::align_left);//Get text message, mainly long width, because to achieve in the picture center $ A = $text->queryfontmetrics ($draw, $filename) ;//Add Text $draw->annotation (($twidth-$a [' textWidth '])/2, $filename);//Build Image $text->newimage ($twidth, $ Theight, New Imagickpixel (' #ffffff '));//Picture Format $text->setimageformat (' png ');//Draw Picture $text->drawimage ($draw);// Create a new blank image to make the canvas $canvas = new Imagick; $canvas->newimage (570, 661, ' White '); $canvas->setimageformat (' png ');//Read picture $pic = new Imagick;$pic->readimage (ROOT. '/_a/'. $item); $pic->scaleimage (470, 470, TRUE);//merge pictures into canvas $canvas->compositeimage ($pic, Imagick::composite_over, 50, 50);//merge text into canvas $canvas->compositeimage ($text, Imagick::composite_over, 0, 520);//Save the picture to another directory $canvas-> Writeimage (ROOT. '/com_a/'. $item); $k ++;echo "{$k} files proceeded.\n";}

Step Three:

Step3: Merge every 20 to one page//scan Directory $files = Scandir (ROOT. '/com_a ');//group the picture $i = $j = 0; $group = Array (); foreach ($files as $item) {  if ($item = = = '. ' | | $item = = = ' ... ' | | strs TR ($item, '. db ')) {    continue;  }  $i + +;  $group [$j] = $item;  if ($i% = = = 0) {    $j + +;  }} $total = count ($group);//concatenation of pictures by group, A4 paper size, 4x5 combination of foreach ($group as $k = = $v) {  $canvas = new Imagick;  $canvas->newimage (2480, 3508, ' White ');  $canvas->setimageformat (' png ');  $i = $j = 0;  foreach ($v as $item) {    $im = new Imagick (ROOT. '/com_a/'. $item);    150 left margin is reserved    $x = $i * 570;    Top margin of 130    $y = + $j * 661;    $canvas->compositeimage ($im, Imagick::composite_over, $x, $y);    Every 4 rows    if (($i + 1)% 4 = = = 0) {      $i = 0;      $j + +;    } else {      $i + +;    }  }  $canvas->writeimage (ROOT. '/merge_a/'. $k. '. png ');  $c = $k + 1;  echo "Group {$c}/{$total} done.\n";}

The above is the whole content of this article, I hope that everyone's study has helped.


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.