Hi,
This is a feature request or a support request depending on if what I want to do is possible or not:
I've created a custom action that load 2 images, resize them and put them side by side in a new image.
During the process, I'm forced to create a temporary image and save it on the server.
But at the end, after the image has been returned by custom action and transfered by imagecache module to the imagecache directory, this temporary image becomes useless.
I need a way to remove it but I don't know how as it should happen out of the custom action's PHP code part.
Any help?

CommentFileSizeAuthor
#11 imagecache.patch1.29 KBanrikun

Comments

dman’s picture

I know that is maybe a problem in imageMagick, but with GD, I'd think that most of the processing is on the image object in memory.
This means I would expect you could delete that image yourself in your custom code. Later imagecache actions will not load from file again.

Or you can add another customaction at the end of the process before imagecache returns.

Or maybe I'm not sure exactly what the problem is. Can you post the code?

anrikun’s picture

You guessed well: I'm using ImageMagick!
Actually, I've modified imagecache module itself a bit so that I can use ImageMagick to convert the first 2 pages of a PDF file into a "double" thumbnail.
(See http://drupal.org/node/460132)
I use the following custom action to generate the thumbnail.
My code might not be perfect but it works, with the only problem that the temporary image $img3 ending with ".pdf.jpg" is not deleted:

<?php
if (!_imageapi_imagemagick_convert($image->source.'[0]', $image->source.'0.png', array(0 => '-thumbnail 220'))) return FALSE;
if (!_imageapi_imagemagick_convert($image->source.'[1]', $image->source.'1.png', array(0 => '-thumbnail 220'))) return FALSE;
$img1 = imagecreatefrompng($image->source.'0.png');
$img2 = imagecreatefrompng($image->source.'1.png');
$img3 = imagecreatetruecolor(440, 294);
imagecopy($img3, $img1, 0, 0, 0, 0, 220, 294);
imagecopy($img3, $img2, 220, 0, 0, 0, 220, 294);
imagejpeg($img3, $image->source.'.jpg', 85);
imagedestroy($img1);
imagedestroy($img2);
imagedestroy($img3);
file_delete($image->source.'0.png');
file_delete($image->source.'1.png');
$image->source .= '.jpg';
return $image;
?>

As you can see, I use GD to concat the 2 thumbnails into one. Maybe if I could do this with ImageImagick I wouldn't need a temporary image anymore...

dman’s picture

I see what you are doing. Makes sense in a sequential way.

I'm not too sure how imageapi works with imagemagick... BUT I think that if you were to finish with
$image = imageapi_image_open($file); instead of just setting image->source then the processing could continue and you'd be able to discard that temp image then...
In GD anyway. Hm, no in imagemagick you do still need that file around until the very end. Tricky.

In imageapi_imagemagick, you concatenate instructions onto the $image->ops[] array, and they all get run at the end. I'm sure somehow with imagemagick pipeline you can insert the right ops that would get the result, but damned if I know how that works. Like You, I'd break it into steps I can understand.

I can only say I'd do it all in GD - because then the processing is done in memory we can pass around. You can STILL use the imagemagick pdf convert in your custom action, but you'd be able to clean up afterwards and carry on in GD.

anrikun’s picture

I can only say I'd do it all in GD - because then the processing is done in memory we can pass around. You can STILL use the imagemagick pdf convert in your custom action, but you'd be able to clean up afterwards and carry on in GD.

First I had tried to do it that way, but convert doesn't seem to work if ImageMagick is not selected as imageapi's toolkit.
As it's written in http://drupal.org/node/460132: Don't forget to change your ImageAPI to Imagemagick
:-(

dman’s picture

I would have guessed that's because you call '_imageapi_imagemagick_convert' which needs the include available. ... although the code seems to be there if the module is just enabled.
Did you re-open the file after imagemagick was done with it? You'd need to do $image = imageapi_image_open($file); before going back to GD or the GD pipeline will still have a handle on the old image. Just changing the ->file is not enough once the process has started.

anrikun’s picture

Hi again dman!

I've finally managed to do what I wanted and what you advised me to:
- use _imageapi_imagemagick_convert while GD is the default toolkit
- use GD after PDF conversion so that there's no useless temporary file remaining

But $image = imageapi_image_open($file); can't be used as it breaks the reference to the original $image object that is passed between actions.
I've also had to patch imagecache.module again so that it doesn't complain about the PDF source file and the fact that I use ImageMagick while GD is the selected toolkit.

If someone is interested in this, I can post the code, but I don't know if this issue is the best place for that?

anrikun’s picture

Title: Delete temporary images created during customs actions » Generate PDF thumbnails using _imageapi_imagemagick_convert while GD is the default toolkit
Category: feature » support
dubs’s picture

Hi Anrikun,

I would be interested to see the code - I need to do this too and it sounds like you've got it working.

Thanks,

Dubs

anrikun’s picture

Can you please tell me what kind of thumbnail you want to generate? 1 page, 2 pages?
See an example of what my code achieves:
http://www.lelieududesign.com/en/sociology-watch
(you want be able to download PDFs but at least, you'll be able to see thumbnails!)
It is what you need?

dubs’s picture

Hi again,

That's perfect - either one page or 2 pages is fine. I can probably adapt the code to suit as I'm not 100% sure whether to just use the first page for now.

Thanks again for offering to post your code....

Dubs

anrikun’s picture

StatusFileSize
new1.29 KB

I've posted everything here:
http://drupal.org/node/641372

Please post a comment if some of the explanations are not clear and... tell me if it works :-)

(the patch below is to use during the tutorial)

magnus’s picture

Status: Active » Needs review
fietserwin’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

D6 EOL. This module's D6 issues already haven't received any attention for over a year. Closing them all unconditionally now.