I'm writing a mobile content adaptation plugin for a bespoke CMS in PHP. The CMS contains `<img/>` 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?