Mrityunjay Singh
Mrityunjay Singh

Reputation: 65

how to divide image into three equal parts in php imagemagick and combine with some padding and output as single image

I want to display an image into three parts as triptych with the help of imagemagick PHP library. I saw the same example here

http://www.rubbleimages.com/Canvas.php.

Can anyone help me on this issue please

Upvotes: 1

Views: 2588

Answers (2)

Mrityunjay Singh
Mrityunjay Singh

Reputation: 65

$cmd = " convert $photo  -crop 33.5%x100%  +repage  -raise 5x1      "."    monet_horizontal_%d.jpg";

exec($cmd);

$cmd = " convert monet_horizontal_0.jpg wt.png monet_horizontal_1.jpg wt.png monet_horizontal_2.jpg  +append "."  abc.jpg";

exec($cmd);

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207465

I am hopeless at PHP but something like this seems to work for me:

#!/usr/local/bin/php -f
<?php

   $padding=10;
   $info = getimagesize("input.jpg");
   $width=$info[0];
   $height=$info[1];

   // Calculate dimensions of output image
   $canvasWidth=$width+4*$padding;
   $canvasHeight=$height+2*$padding;

   // create white canvas
   $output = imagecreatetruecolor($canvasWidth, $canvasHeight);
   $background = imagecolorallocate($output, 255, 255, 255);
   imagefill($output, 0, 0, $background);

   // read in original image
   $orig = imagecreatefromjpeg("input.jpg");

   // copy left third to output image
   imagecopy($output, $orig,$padding,             $padding,             0, 0, $width/3, $height);
   // copy central third to output image
   imagecopy($output, $orig,2*$padding+$width/3,  $padding,      $width/3, 0, $width/3, $height);
   // copy right third to output image
   imagecopy($output, $orig,3*$padding+2*$width/3,$padding,    2*$width/3, 0, $width/3, $height);

   // save output image
   imagejpeg($output,"result.jpg");
?>

If I start with this:

enter image description here

I get this as a result

enter image description here

I added the black outline afterwards so you can see the extent of the image.

Upvotes: 2

Related Questions