One of the websites I host and operate ran into some problems today with the image upload facility. For some reason it wouldn’t resize any images with an error showing that too much memory was being used. Uploading the same image via a different website I host worked fine, which I found a little strange.
To cut a long story short I managed to get things working again by switching from the inbuilt GD library to Imagick, the PHP extension to ImageMagick. Here’s the code I used to make a thumnail based on the image uploaded.
function create_thumbnail($file, $new_name, $max_side) { $image = new Imagick($file); // Providing 0 forces thumbnailImage to // maintain aspect ratio $image->thumbnailImage($max_side,0); $thumbnail = $image->writeImage($new_name); $image->destroy(); // Free resources if($thumbnail == true) { return true; } else { return "Imagick Error!"; } }
It went from about 50 lines of code, to just 13. A pretty good improvement considering it does the same thing!
Anyone whose pretty eagled eyed will notice the new Share This link just next to the comments link on each post. I implemented the