<p>I'm writing a mobile content adaptation plugin for a bespoke CMS in PHP. The CMS contains <code>&lt;img/&gt;</code> links to images with absolute URLs which are all 400 pixels wide and vary in height.</p>

<p>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</p>

<p>What's the best way for me to achieve this in PHP using either ImageMagick or GD? </p>

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.