For some reason I get the following error when I try to pull down an image larger then 40kb from a MSSQL database.

<br />
<b>Fatal error</b>: Out of memory (allocated 265027584) (tried to allocate 528482304 bytes) in <b>/home/public_html/test/image.php</b> on line <b>31</b><br />

Here is the code that I am using.

<?php
header("Content-type: image/jpeg;" );
$myServer = "";
$myUser = "";
$myPass = "";
$myDB = "";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT * ";
$query .= "FROM Pictures ";
$query .= "WHERE (ID = ".$_REQUEST[P].")";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);

//display the results
while($row = mssql_fetch_array($result))
{

$image = imagecreatefromstring(gzinflate($row["Picture"]));

imagejpeg($image, NULL, 75);

imagedestroy($image);
}
//close the connection
mssql_close($dbhandle);
?>

I increased the memory_limit to 900mb and it still gives me that error.

Anything that I can do to fix it?

    You probably need to use stripslashes() on the data before using gzinflate()

    Considering it is trying to allocate a very large amount of memory, maybe you should see how big the data being returned is.

      One thing to note is that the amount of memory needed by the PHP imagecreate functions is dependent on the number of pixels in the image times the color depth (typically 24 bits per pixel, possibly 32 bits), as it creates a bitmap of it in memory. Therefore while the JPEG compression of an image might only need 40KB of storage, if it's a large enough image it's possible that PHP will need much more memory to create its image bitmap. Whether this by itself is the issue I can't say at this point, but for debugging you could put in a [man]memory_get_usage/man call right before the imagecreate call to see how much memory is being used before you try to create the image.

        Here is the memory usage .

        63256
        "Content-type: image/jpeg"

        64068
        "_REQUEST[P]"

        129420
        "mssql_query($query)"

        129496
        "mssql_num_rows($result)"

        259020
        "mssql_fetch_array($result)"

        <br />
        <b>Fatal error</b>: Out of memory (allocated 265027584) (tried to allocate 528482304 bytes) in <b>/home/public_html/test/image.php</b> on line <b>36</b><br />

        Also stripslashes() gives me an invalid string when it gets to gzinflate().

          Do you happen to know the dimensions of the image in question (in pixels)?

            All the images are the same size, and I don't get that error with all of them, only some. The sizes are 260px × 144px.

              Certainly does not sound anywhere near big enough to need that much memory. Can you separate out the gzinflate() from the imagecreate() (perhaps assigning the gzinflate result to a variable, then using that variable with the imagecreate) and see if you can narrow down which of the two is trying to allocate all that memory? (I've not worked with the gz functions so don't know what the "gotchas" might be, but a 260x144 image should not need more than about 150,000 bytes, worst case.)

                do you know how the data was added to the db?

                  The jpeg image is compressed in RFC 1951 then its uploaded to the database. The compression is done from within a VB program and its saved to the database as binary.

                  But for some apparent reason I'm only getting this error on certain images, the ones that are larger then 40kb.

                  The error happens when it gets to gzinflate($imagedata);

                  I also separated gzinflate like NogDog stated and I still get the error.

                  Even without using the imagecreatefromstring and just printing the gzinflate code.

                    I found the culprit . It was a mssql setting on the php.ini file

                    mssql.textlimit - had to increase the value.

                    THANK YOU TO ALL OF YOU THAT TOOK YOUR TIME TO HELP ME RESOLVE THIS ISSUE.

                      Write a Reply...