M Ikram Zafar
M Ikram Zafar

Reputation: 150

How to merge images using php imagick

I am very new to use php imagick to use . Basically I want to merge two images. I have coded this

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

    $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;
    }

This gives a card . Now I want to repeat it on A4 or leageal page multiple times and save it to pdf. Kindly help me .

Upvotes: 0

Views: 13346

Answers (1)

Pushpak Patel
Pushpak Patel

Reputation: 828

Remove the following lines from your code

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

You can also combine images using compositeimage() function.

/* 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);

You can place the above code in a loop to automatically stitch the card to different position in final image

/* 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();

Upvotes: 6

Related Questions