Image Composition (Name Consistency)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
northwest

Image Composition (Name Consistency)

Post by northwest »

How do you write the composited images as the same filename as the original images?

I got to compositing multiple images on to canvas, but the final images' filename are inconsistent with the original images' filename.

Code: Select all

<?php

// Get the image
$mgk_wnd1 = NewMagickWand();
MagickReadImages($mgk_wnd1, $img_name = glob("*.jpg"));

// Get number of images stored in the wand
$num_img = 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 = 150;
$y_offset = 100;

for ($i = 0; $i < $num_img; $i++) {

	if (MagickGetImageHeight($mgk_wnd1) != 480 && MagickGetImageWidth($mgk_wnd1) != 640) {
		
		echo $img_name[$i] . "<br/>";
		// Composite both original image and the canvas
		MagickCompositeImage($mgk_wnd2, $mgk_wnd1, MW_OverCompositeOp, $x_offset, $y_offset);
		
		// Write the image to a file
		MagickWriteImages($mgk_wnd2, "images/$img_name[$i]", TRUE);
		MagickRemoveImage($mgk_wnd1);
		
	}
	
}

// Enhance image
//MagickEnhanceImage($mgk_wnd2);

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

?>

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Image Composition (Name Consistency)

Post by anthony »

Only the background or destination image of a image composition will retain the image meta-data. There are of course minor exceptions, -flatten for example retains the file name of the first image in the image sequence, even though it generates a 'background canvas' as the destination.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply