I have a problem that I thought was easily solved, but is turning out a little more difficult than anticipated.
I am working on a site which someone can upload images for a product, and it resizes them twice (500×500 and 150×150) on the fly. I am trying to keep the aspect ratio, for example if I had 600×500 image it would resize to 500×417 and 150×125.
I have found (lots) of code that does this, such as this class, however I always run into memory issues. I think the class isn’t unloading/destroying the temporary images after resizing.
I had the host upgrade my memory-limit to 64M from 32, however I run out of memory later. I’m stuck with PHP 5.2.13 as well, so no garbage collector for me.
My PHP is fairly new, so it’s very alien to me how classes work, otherwise I would try adding the imagedestroy() function where required. I fiddled with the linked class for hours without success.
Can anyone either:
-Point me in the direction of a magic class that keeps aspect as well as unloading/destroying temporary images after resizing is complete, to keep RAM usage under control?
-Give me some (much needed!) guidance on where and how I could modify a class (such as the one posted) to destroy temporary images.
-Tell me if I’m going around this extremely wrong?
Oh, and I had the WideImage class working at one point, and it worked great, then suddenly stopped. I spent hours trying to fix it with no success.
My PHP install has GD, but not ImageMagick.
Thanks!
Well, I managed to solve my own problem.
I sat down, wiped all my resize code, and started from scratch, and wrote this wonderful little function to do what I needed.
Feed in target width and height, as well as the location of the input image (eg “uploads/images/testimage.jpg”), and where you want it saved (“uploads/images/resizedtestimage.jpg”).
Hope this snippet helps someone in the future!