How to Create New Canvas and Composite Two Images

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.

How to Create New Canvas and Composite Two Images

Post by northwest »

I've been trying to make this work for long hours now, I got to the point where I could composite two images. However I'm currently having trouble with (1)"reading multiple images", then individually (2)"compositing them on different canvases" and then (3)"writing them as the same name as the orginal images". Also, (4)"How do you center the original image on the canvas?" I tried using DrawSetGravity(), but I just didn't know how to get an image centered rather than a line of text.

Another thing that I tried was exec() to execute imagick commands. The code was much shorter than the PHP magickwand code. (5)"Is it more convenient to just execute commands through exec()?". It's odd that I don't have more control over the images with PHP magickwand functions than with Imagick command line, because for example, it is much easier to just center the original image on the canvas just by including the parameter, "-gravity center".

Here is the "main" flow of the current program:
1. Read an original image
2. Create a 640x480 white canvas
3. Composite original image on to the canvas
4. Write the composited image on to a jpeg file.

Code: Select all


// Get the image
$mgk_wnd1 = NewMagickWand();
MagickReadImage($mgk_wnd1, "1.gif");
// MagickReadImages($mgk_wnd1, glob("*.gif"));

// Normalization

// MagickGetNumberImages($mgk_wnd1);

// Create a white 640x480 jpeg canvas
$mgk_wnd2 = NewMagickWand();
MagickNewImage($mgk_wnd2, 640, 480, "white");
MagickSetformat($mgk_wnd2, "jpg");

// Set offsets x and y
$x_offset = 100;
$y_offset = 100;

// Composite both original image and the canvas
MagickCompositeImage($mgk_wnd2, $mgk_wnd1, MW_OverCompositeOp, $x_offset, $y_offset);

// Enhance image

// Write the image to a file
MagickWriteImage($mgk_wnd2, "image.jpg");

// Clean up wands
if($mgk_wnd1 && $mgk_wnd2) {
	$mgk_wnd1 = DestroyMagickWand($mgk_wnd1);
	$mgk_wnd2 = DestroyMagickWand($mgk_wnd2);

// use a variable for command line contents
$cmd1 = "convert -size 640x480 xc: white canvas3.jpg";

$cmd2 = "composite -gravity center \*.gif canvas3.jpg \*.gif";



Re: How to Create New Canvas and Composite Two Images

Post by xenie357 »

post deleted