1 (edited by twohawks 2007-03-20 21:54)

Topic: Memory management/buffering php issue - expert help needed

Hi Folks,  I need the experts on this one... will you please check me on this?

I am getting the following error message when I pump up the file size allocation in PBB Gallery, i.e., if I set the maximum file height to somewhere over 4000, the following is returned:

Fatal error: Allowed memory size of 41943040 bytes exhausted (tried to allocate 2087 bytes) in /xxxxxx/public_html/forumdirectory/include/functions.php on line 888

What I am trying to do is allow for the possibility of very tall images (snapshots taken from scrolling documents).  The actual file sizes are only about 250kb. 

I suspected the problem is something to do with output buffering and using "imagecreatetruecolor" in php, and after reading this (http://us3.php.net/manual/en/ref.outcontrol.php#54102) and trying it out I have at least confirmed it is definitely related to 'memory need' for processing the image, (and possibly output buffering, but I am uncertain there).  I.e., if I apply <code: php_value memory_limit 50M> to my htaccess file I can cause it to work (meaning the file uploads and processes sucessfully).

Of course, I am very concerned about leaving a memory limit setting so high `8^P.  I cannot fathom this is truely necessary?!  There must be some other way to be addressing 'the need' here, but I need your insights to help sort out the understanding to the proper method/approach to use (?).  Do I try some sort of output buffering 'in chunks' or something (but this isn't really to do with outputting)?? 

Over my head... Really need some expert advice here.  Please help!

Cheers,
TwoHawks

PS: Please don't flame me for cross-posting.  I originally posted about this inside the pbb galley topic at punres, but my sense is that this is not really getting the attention it requires 'being buried' in there (where it seems it has not been seen by those who may be able to make a differnce ;^).  So I decided it needed its own topic.

TwoHawks
Love is the Function
No Form is the Tool

Re: Memory management/buffering php issue - expert help needed

Moved to Programming wink

Re: Memory management/buffering php issue - expert help needed

Without looking at any code,

What is the reason you're putting the data into a new truecolor image?

Re: Memory management/buffering php issue - expert help needed

I found this comment:

d dot duquenoy at kdland dot org
01-Jun-2006 08:26
My function to know how much bytes imagecreate or imagecreatetruecolor require before using it.
<?php
function getNeededMemoryForImageCreate($width, $height, $truecolor) {
  return $width*$height*(2.2+($truecolor*3));
}
?>

Re: Memory management/buffering php issue - expert help needed

Hi Folks,
..sorry for the delayed response, I have sudden pressing family matters that are requiring a lot of my time at the moment, thus I have scant time ffor work at the momment.... so please be patient for my responses, ...and please don't tune out, this is very important to me!

sooo...
@Jansson: I did not originally write this mod, so I will not know the answer to your question until I investigate further, which I will try to do within the next couple days  (good question ;^)

@Smarty's: thank you for that link and that code.  I am going to check it out.  I wonder if I can use it on the fly for setting the memory only while that particular command is running, or if I will still have to set that in htaccess as a permanent mem usage allowance (which I don't want to do).  I would bet that I cannot do it on the fly.  That's why I want to find out if I can chunk-out the command mem usage with some sort of buffering... I mean, I am wondering if that's how this sort of thing would be typically handled, or what....

Thanks so much for responding (both of you)... I will look into it further as I can and be watching and posting back here ;^)

TwoHawks
Love is the Function
No Form is the Tool

Re: Memory management/buffering php issue - expert help needed

twohawks: You might: my point was simply that processing something with a larger height will take a great deal of memory.

7 (edited by twohawks 2007-03-24 03:44)

Re: Memory management/buffering php issue - expert help needed

Okay, well here's what I (can) gather so far...

    Imagecreate commands necessarily take up about (width x height x 5) bytes of memory for processing.  There is no way as yet that I have found for buffering or chunking the memory allocation for this sort of processing, or chunking the process itself.
    What I found I can do is run a 'ini_set' command against the memory_limit only during certain script executions so I do not have to leave the default mem usage settings ridiculously high. 
   What I plan on doing is writing a script/function that will allow for special cases where I wish to allow automatic memory allocation for a specific type of upload.  Later on I think I might script in an option to simply upload certain types of pics, and then only generate thumbnails for those.... so memory allocation management issues may be reduced.
   These seem to be the options available for this sort of thing for now.

BTW, to answer your question, Jansson, I imagine that the reason for using imagecreatetruecolor is that,  because we are not directly uploading the images, but resizing, and thus recreating, the images, ...for certain types of images it thus requires recreating them with truecolor if one wishes to maintain the full-color integrity.  This reasoning seemed pretty 'simply apparent' to me once I read up on it.

   Although I am thinking about rebuilding the uploader at some point to allow for direct uploading (as opposed to image recreation) in certain circumstances, and this would certainly get around certain memory issues, this possibly will not allow for certain other types of control that are available when using php to re-manage (proprietary issues with certain) images.  For anyone reading this, this is something to keep in mind ;^)

Thanks for your input, guys.  I am open to any further news on this subject, should it come around ;^)  I hope others may find this information useful as well.
Cheers,
TwoHawks

TwoHawks
Love is the Function
No Form is the Tool