如何使用php imagick合并图像

时间:2014-05-17 05:52:10

标签: php imagick

我很高兴使用php imagick来使用。 基本上我想合并两个图像。 我编码了这个

    $logo = new Imagick("img/logo.png");
    $name  ="Jhon N. Doe";
    $title ="Web Programmer";
    $address= "abc road xyz";
    $website = "www.acfh.com";
    $phone = "0555555";
    $email ="mizff@yahoo.com";

    $image = new Imagick();
    $draw = new ImagickDraw();
    $pixel = new ImagickPixel( '#f5f5f5' );

    $image->newImage(350, 170, $pixel);

    //$logo->newImage(100, 100,$logo);
    /* Black text */
    $draw->setFillColor('#6b6a6a');

    /* Font properties */
    $draw->setFont('./font/MyriadPro-Regular.otf');
    $draw->setFontSize(10);

    /* Create text */
    $image->annotateImage($draw, 230, 150, 0, $email);
    $image->annotateImage($draw, 102, 75, 0, $title);
    $image->annotateImage($draw, 10, 150, 0, $address);
    $image->annotateImage($draw, 230, 160, 0, $phone);


    /* Output the image */

    //for name 
    $draw->setFillColor('#333333');
    $draw->setFontSize( 30 );
    $image->annotateImage($draw, 30, 60, 0, $name);

    $draw->setGravity(Imagick::GRAVITY_CENTER);
    $image->compositeImage($logo, Imagick::COMPOSITE_DEFAULT, 230, 20);
    $image->resetIterator();
    $combined = $image->appendImages(true);
    $combined->setResolution(72,72);
    $combined->setImageFormat("jpg");
    header("Content-Type: image/jpg");
    echo $combined;
    }

这给了一张卡片。现在我想多次在A4或leageal页面上重复它并将其保存为pdf。请帮助我。

1 个答案:

答案 0 :(得分:4)

从代码中删除以下行

$image->resetIterator();
$combined = $image->appendImages(true);
header("Content-Type: image/jpg");
echo $combined;

您还可以使用compositeimage()函数组合图像。

/* Set new imagick object with required height and width based on the dimension of your print paper*/
/* add the following code */

$output = new Imagick();
//sample image. width is double to your actual card width which was 350px. set height as required
$output->newimage( 700, 842, "none");

/* Add the Card to the blank image that we created */

$output->compositeimage($image->getimage(), Imagick::COMPOSITE_COPY, 0, 0);
$output->compositeimage($image->getimage(), Imagick::COMPOSITE_COPY, 350, 0);

您可以将上述代码放在一个循环中,以自动将卡片缝合到最终图像中的不同位置

/* sample example */ 

$height = "your card's height";
$width = "your card's width";

for( $row=0; $row<5 ; $row++ ){

    for($col=0; $col<2; $col++){
    $output->compositeimage($image->getimage(), Imagick::COMPOSITE_COPY, $row*$height, $col*$width);
    }
}

/* write you file to some file*/
$output->writeimage("/path-to-your-file.jpg");
$output->destroy();