Delete source image after resizing

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
gregory
Posts: 67
Joined: 2009-08-02T17:20:23-07:00
Authentication code: 8675309

Delete source image after resizing

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Delete source image after resizing

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Delete source image after resizing

Post 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
gregory
Posts: 67
Joined: 2009-08-02T17:20:23-07:00
Authentication code: 8675309

Re: Delete source image after resizing

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Delete source image after resizing

Post by fmw42 »

Are you trying to delete the input or output. If the output, you can use temp files such as PNG: or JPG:
gregory
Posts: 67
Joined: 2009-08-02T17:20:23-07:00
Authentication code: 8675309

Re: Delete source image after resizing

Post by gregory »

I'm trying to delete the input file after the output file created.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Delete source image after resizing

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Delete source image after resizing

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Delete source image after resizing

Post by fmw42 »

very interesting. i never noticed that before, but see http://www.imagemagick.org/Usage/files/#ephemeral
Post Reply