IMagick to generate GIF and skip every n image

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

IMagick to generate GIF and skip every n image

Post by burt46 » 2017-02-21T11:52:03-07:00

Hi,

I have a IMagick PHP code that i use from a cron job to generate GIF files from folders of images. The problem is that the gifs are around 3 - 10mb in size and take too long to download.

Is it possible to generate a gif but say every n image in a folder? I have seen this is possible to extract frames from a GIF but not to generate a GIF.

I have tried various methods to reduce the GIF size and the sizes above are already optimised.

Grateful for any advice.

Many thanks
Burt

User avatar
fmw42
Posts: 22095
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: IMagick to generate GIF and skip every n image

Post by fmw42 » 2017-02-21T13:02:26-07:00

You would have to write a script loop to take only every other images from the folder to make your gif. You could for example save a list of every other file into a variable or as text file and read from that text file.

The IM frame command [X-Y] does not have a skip feature, as far as I know.

It is generally a good idea to identify your IM version and platform when asking questions.

burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: IMagick to generate GIF and skip every n image

Post by burt46 » 2017-02-21T13:10:09-07:00

Thanks for the reply. I guess this is a separate feature to IM to generate the txt file? Perhaps a cron job can do this too.

Linux, IM version 6.6.3

User avatar
fmw42
Posts: 22095
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: IMagick to generate GIF and skip every n image

Post by fmw42 » 2017-02-21T13:10:40-07:00

Another idea would be to delete every other image in the command sequence. In command line, suppose you have 10 images in the folder, you could do

Code: Select all

convert -delay 50  *.jpg -delete 1,3,5,7,9 -loop 0 result.gif
IM 6.6.3 is ancient (about 350 versions old). I would suggest you upgrade if you can.

burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: IMagick to generate GIF and skip every n image

Post by burt46 » 2017-02-21T13:22:18-07:00

Thanks for the feedback. I thought about this but it would delete my master images that i need for other scripts.

I cant change the IM version as it is controlled by my hosting company

User avatar
fmw42
Posts: 22095
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: IMagick to generate GIF and skip every n image

Post by fmw42 » 2017-02-21T13:29:21-07:00

using -delete does not delete images from your folder, only from the in memory sequence loaded by *.jpg.

However, it would be better to load only the images you want.

Code: Select all

cd somedirectory

imgArr=(`ls`)
num=${#imgArr[*]}

list=""
for ((i=0; i<num; i=i+2)); do
list="$list ${imgArr[$i]}"
done

convert -delay 50 $list -loop 0 result.gif

burt46
Posts: 20
Joined: 2016-06-06T11:48:45-07:00
Authentication code: 1151

Re: IMagick to generate GIF and skip every n image

Post by burt46 » 2017-02-21T13:30:37-07:00

Perfect. Many thanks for the help.

User avatar
fmw42
Posts: 22095
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: IMagick to generate GIF and skip every n image

Post by fmw42 » 2017-02-21T15:04:29-07:00

If you just want JPG, then use

Code: Select all

imgArr=(`ls *.jpg`)

Post Reply