Passthrough problem or exec?

Step 1:
I am using exec to call a shell script which uses Imagemagick to manipulate an image and save it.

Step 2:
I am then creating an Imagemagick command to put the image from Step 1 onto another image.

Step 3:
I am then using passthrough to process and display the image from step 2

The problems I am having are:
Some images are not displayed at all and I get the box with the red cross
Some images have the same Step 1 image on two or three images in step 3

I am processing about 20 images and I assumed that the code was not finishing before starting again.
I tried putting a sleep(3) in different places but it did not seem to have any effect.

Can somebody let me know where I am going wrong and what I need to do to sort this problem?


<?php
$art = '';

$art = escapeshellcmd ( $_GET['art']);

// Default values for shell script
$radius = '52';
$pitch = '10';
$background = 'none';
$vertual_pixel = 'transparent';
$length = '130';

// Run the shell script to create the label - a tmpory image created but we will delete it later
exec(" /home/user/cylinderize -r $radius -l $length -p $pitch -b $background -v $vertual_pixel $art label.miff"); 

// Composite the label onto the bottle	
$cmd = "convert blank.jpg label.miff -trim -gravity center -geometry +0+50 -composite +repage JPG:-";

// Display the image
header("Content-type: image/jpeg");
passthru($cmd, $retval);

// Delete the tempory label	
unlink("label.miff");
?>

If it’s possible for more than one instance of this php script to be executing at the same time, you will problems, because they all share the same temporary file. Think about what would happen if one script is reading the file while another is in the middle of writing to it.

If you really need to use a temp file for the label, see tempnam() to make a unique one. If Imagemagick doesn’t like the file extension created, then other solutions exist.

You might not even need a tmp file though. I have no experience with that IM from the command line, but It might be able to read from stdin instead of a file. Then you can just pipe the output from the first command into the second, avoiding a tmp file altogether.

Thanks for the ideas crmalibu; I tried porting but I had problems getting the images into the correct order.
I then changed the name of the tempory image and it seems to be working OK now.

Hm, the docs definitely say it can

Not sure if it will mess up the order though.

Does this work?


$cmd = " /home/user/cylinderize -r $radius -l $length -p $pitch -b $background -v $vertual_pixel $art miff:- | convert blank.jpg miff:fd:0 -trim -gravity center -geometry +0+50 -composite +repage JPG:-";

You might not need the miff: label.

I will try your suggestion later; this is what I did but the images are in the wrong order and -swap did not work.

 $cmd = "/home/usr/cylinderize -r $radius -l $length -p $pitch -b $background -v $vertual_pixel $art miff:- | convert blank_bottle.jpg -swap -trim -gravity center -geometry +0+50 -composite +repage - JPG:-";

Its similar to what you suggested.

I get this error with your code crmalibu:

convert: unable to open image `fd:0’: No such file or directory @ blob.c/OpenBlob/2403.

Docs said the numbered file descriptors are as of version 6.4.9-3

miff:- might work instead.

I had not read the instructions and my server is 6.4.8 !
What a coincidence :rolleyes:

lol