views:

285

answers:

2

I'm writing a mobile content adaptation plugin for a bespoke CMS in PHP. The CMS contains links to images with absolute URLs which are all 400 pixels wide and vary in height. I'd like to parse the HTML (which is stored in MySQL) and re-scale each image to a new width - this will vary according to the device. I'd also like to cache the images to prevent needlessly resizing them on-the-fly every time the page is loaded

What's the best way for me to achieve this in PHP using either ImageMagick or GD, please?

+2  A: 

what about doing something a bit different. basically off load the caching/resizing to an on demand model. so say your application is being run on device A, which requires 200x200 images. you'd change the image links to:

<img src="/images/image.php?height=200&width=200&source=filename.jpg" />

image.php could be a script which does the following:

  • make sure the existing file exists, and grab it from cache if it exists at this size
  • if not, resize the image and cache it

the next time your app looks for that image, it would be sent back at the 200px size. alternatively, if the app is now looking for a 300x300 image, that would be built/cached on the new request.

Owen
A: 

SEE http://stackoverflow.com/questions/140734/best-way-to-cache-resized-images-using-php-and-mysql
for really good ideas on caching mechanism (especially apache webserver intervention concept)

and

http://phpthumb.sourceforge.net/ which encapsulates using both/either ImageMagick or GD.

micahwittman