Page 1 of 1

Delete source image after resizing

Posted: 2010-03-26T14:08:19-07:00
by gregory
Hi,

Is it possible to delete an image with "convert" after resizing? The problem is that I try to delete an image using PHP function unlink() after using shell_exec("convert -resize ...") but sometimes image is still in progress so PHP can't delete an image and just ignore unlink() function. After several days I have a tons of images weren't deleted and I have to delete them manually.

Re: Delete source image after resizing

Posted: 2010-03-26T14:36:04-07:00
by fmw42
No, not as far as I know. You cannot delete the input image after resizing it with convert with IM. With mogrify you can write over the input image.

Re: Delete source image after resizing

Posted: 2010-03-27T01:19:58-07:00
by Bonzo
Thats strange; I am currently working on a code that is modifying and deleting hundreds of images without a problem.
I use exec rather than shell_exec does that make a difference ?
I would have thought that once the image has been read into ImageMagick it could be deleted.
What about adding a sleep between the IM code and the unlink() http://php.net/manual/en/function.sleep.php

Re: Delete source image after resizing

Posted: 2010-03-27T01:59:08-07:00
by gregory
It's not possible to use sleep() because the priority is to make an image than delete it.
I would have thought that once the image has been read into ImageMagick it could be deleted.
I also tried to use a system() function:
system("convert -resize ...");
unlink("path/to/file");

Some files still can not be deleted with this code while IM is in progress.
The only option is to make a new script to delete these images using cron.

Re: Delete source image after resizing

Posted: 2010-03-27T09:33:23-07:00
by fmw42
Are you trying to delete the input or output. If the output, you can use temp files such as PNG: or JPG:

Re: Delete source image after resizing

Posted: 2010-03-27T09:58:53-07:00
by gregory
I'm trying to delete the input file after the output file created.

Re: Delete source image after resizing

Posted: 2010-03-27T11:00:37-07:00
by fmw42
Only way I can think to do it uses some unix:

convert zelda3b.jpg \( +clone -resize 50% -write zelda3b_small.jpg \) null: | rm zelda3b.jpg

you may be able to leave off the pipe to rm and use your PHP unlink to delete the input. Since the clone is made before the resize, then the input can be deleted at any time after the clone is made and before or while the resize. The clone will go away after the convert finishes.

However, might it be a problem with your server and a delay in updating the file list in the directory. Even though the image is created, the file list may not yet be finished updating, especially if you have many many images in the file list.

Re: Delete source image after resizing

Posted: 2010-03-27T12:09:52-07:00
by magick
You can delete an image after its read with the ephemeral tag. Its not advertised because its destructive. Try this:
  • -> convert logo: logo.jpg
    -> convert ephemeral:logo.jpg logo.png
    -> ls logo.jpg logo.png
    ls: cannot access logo.jpg: No such file or directory
    logo.png

Re: Delete source image after resizing

Posted: 2010-03-27T13:09:20-07:00
by fmw42
very interesting. i never noticed that before, but see http://www.imagemagick.org/Usage/files/#ephemeral