gggggggg
gggggggg

Reputation: 41

Bulk Image Resizing with PHP and ImageMagick

In the early days of my site, I allowed people to upload any size image they wanted. I now have client and server side config to limit new images to 2000px max, while maintaiing the origional ratio.

So from the old config I have a heap of directories with large images that I need to scale down, I would like to do this on the server side in batch (due to client side bandwidth limitations), and for the life of me I cant work it out. I realise it will cause server side high CPU, so I will do it in groups spread over a few days.

Due to Host RAM limitations I cant use GD for scaling, so I have ImageMagick avail to me.

An example of a group I would do in 1 batch looks like this:
/ Images / 01 / 101 / abc.jpg
/ Images / 01 / 101 / random.jpg
/ Images / 01 / 101 / randomfile.jpg
/ Images / 01 / 184 / random.jpg
/ Images / 01 / 184 / photo.jpg
/ Images / 03 / 372 / randomnumber.jpg
/ Images / 03 / 372 / randomanything.jpg

So I am after something that can:

I am stuck, and would love any help please.

Thanks Greg

Upvotes: 0

Views: 1836

Answers (1)

Artefacto
Artefacto

Reputation: 97815

Recursively go through all directories and sub-directories looking for JPGs, when it finds one

Use a combination of RecursiveDirectoryIterator and RecursiveIteratorIterator, possibly together with RegexIterator. There are a few examples in this site.

Work out the current ratio

Unnecessary.

Rename the origional file to origional.jpg.old

See rename.

resize the file to a max of 2000px in height/width, while mainting aspect ratio

See Imagick::resizeImage or Imagick::scaleImage.

write file to disk using the origional.jpg file name

Imagick::writeImage

check that the new origional.jpg is a valid happy file

Unnecessary.

And if its a happy file delete origional.jpg.old

See unlink.

Upvotes: 3

Related Questions