SirXaph Posted April 22, 2009 Share Posted April 22, 2009 Hi! I'm new. I have searched though, I think this is my host being strange but I figured I'd post here in case there was any kind of workaround. I'm trying to retrofit a simple php image gallery & trying to iron out the bugs, yet the most irritating bug I can find seems to actually be to do with server settings or something like that. The code in question checks to see if a cached thumbnail and resized version of the image exist, then if they don't calls a resize function which loads the image, resizes it, and saves the cached thumbnails for future use. The problem occurs when the images get over 2000 pixels x 1500 (exactly - 1999 x 1499 is fine). Now, from what I understand if there's an error then I should expect either an error, or at the very least the function should return false like the php manual says it should. Unfortunately the function doesn't seem to actually return anything at all - the whole php script crashes at this point with a 500 server error. I have to comment out or make the imagecreatefromjpeg call point to a smaller image to get the remainder of the code to run. Just to check the server error wasn't caused by anything else I made this code snippet: <?php $im = imagecreatefromjpeg("dscf3717.jpg"); header('Content-Type: image/jpeg'); imagejpeg($im); imagedestroy($im); ?> And I made an exact duplicate with a smaller image file. Those php files are here: http://antarctica.go-forth.co.uk/photos/test.php http://antarctica.go-forth.co.uk/photos/test1.php and the images are here: http://antarctica.go-forth.co.uk/error.jpg http://antarctica.go-forth.co.uk/photos/dscf3717.jpg Just so you know: I don't get any memory errors - I get no errors at all - though the memory limit for my server is apparently at 40M which should be enough (2000 pixels x 1500 should be a memory footprint of about 17M according to the formula people worked out in the comments on the php manual). The part of the resize code that's causing issues: switch(strtolower($path["extension"])){ case "jpeg": case "jpg": $original=imagecreatefromjpeg($source); break; case "gif": $original=imagecreatefromgif($source); break; case "png": $original=imagecreatefrompng($source); break; default: break; } if (!$original) { $original=imagecreatefromjpeg("../error.jpg"); header('Content-type: image/jpeg'); header('Content-transfer-encoding: binary'); imagejpeg($original, null, $resizequality); exit(); } as you can see I'm checking for a false return from imagecreatefromjpeg, and substituting the error image when that happens, however because the code seems to just stop, rather than returning an error this part of the code isn't executed. Suggestions? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted April 22, 2009 Share Posted April 22, 2009 This will be your host! Your script is probably exhausting memory and this is the response setup by your hosting company. Quote Link to comment Share on other sites More sharing options...
SirXaph Posted April 22, 2009 Author Share Posted April 22, 2009 Thanks That was the conclusion I came to as well. Seems like a really odd way of doing things though; why not just set the maximum memory usage in php.ini and prevent me from overriding - at least that'd give me a usable error message. I've sent a message to my host politely explaining that it's 2009 now, the amount of ram even an 8 megapixel image takes up should be pretty negligible these days provided it's not happening all the time (which it isn't, that's why I'm caching the resized images); I'll see what they say. If anyone knows of a workaround allowing an image to be resized without loading the whole thing into memory first or something similar then please let me know. It's disappointing to have to shrink my images down in order to put them into a gallery. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted April 22, 2009 Share Posted April 22, 2009 I would use ImageMagick (convert command via cli) rather than php gd functions to manipulate your images as no memory will be leaked from the php script. Some shared hosts have this program installed on their servers. I have a cheap host that has it and I am able to use via exec(). Worth asking. Quote Link to comment Share on other sites More sharing options...
SirXaph Posted April 24, 2009 Author Share Posted April 24, 2009 Thanks Neil I am now having more success with resizing the images using an imagemagick exec() call - for example: http://antarctica.go-forth.co.uk/photos/sp_resize.php?source=./2009-03-14_In_Transit/DSCF0686.jpg However, occasionally the exec calls seem to crash (or at least, not work), resulting in no image being created (which in my code causes an error icon to be loaded instead when the time comes to load and display the resized image); Sometimes though it seems the whole resize script crashes resulting in no image being returned at all.. it doesn't happen all the time or with the same images when it does; a simple refresh fixes it. I guess that would be my host again doing funky stuff and killing processes that would've used too much memory were we still in 1998... the fact that I can notice it on my site when it's just me browsing it is a bit worrying, considering I'm the only person with the url right now. I can foresee it being terrible if traffic was higher. Any thoughts as to how I can minimise these issues? The code already tries to limit the number of times the resizes are done (because obviously it shouldn't be doing the exec again for a certain image after the resized version has been saved).. I'm not totally familiar with exec() style functions and am wondering if there's a better way of doing it than this: $command = "convert " . $src . " -resize " . $maxwidth . "x" . $maxheight . " " . $dest; exec($command, $out, $rcode); Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted April 24, 2009 Share Posted April 24, 2009 Maybe better hosting. Dedicated server. Quote Link to comment Share on other sites More sharing options...
SirXaph Posted April 24, 2009 Author Share Posted April 24, 2009 Yeah I was thinking maybe that. Problem there really is that it likely means that my code isn't as portable or transferable as I'd like it to be but I guess such is life. Oh, and you were faster at posting than I was at editing, not sure if my edits above make any difference or help at all though, I just figured I'd add the basic code I'm using. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.