Allowed memory size exhausted

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • f.amann@rona.at

    #1

    Allowed memory size exhausted

    hi there!

    i save images in a mysql db and i wrote a script called getimage.php to
    display them.
    in my getimage.php i want to resize my images to display them as
    thumbnails but there always occurs the following error:

    <b>Fatal error</b>: Allowed memory size of 188743680 bytes exhausted
    (tried to allocate 240 bytes) in <b>.../public_html/getimage.php</b> on
    line <b>x</b><br />

    it always tries to allocate either 240 or 60 bytes.
    line x equals: $image_p = imagecreatetrue color($newWidth , $newHeight);

    my configuration:

    PHP Version 4.3.10
    Apache/2.0.49 (Linux/SuSE)
    GD Version: bundled (2.0.15 compatible)
    memory_limit 180M !!!!!!!!

    the machine has 512MB RAM.

    I tried it with a jpg:
    - the size was 163kb.
    - the width was 533px
    - the height was 400px

    it was not a really big image but it also didn't worked with smaller
    ones.

    my code:
    include "inc/Main.inc";
    $Main = new Main();
    $Database = new Database();
    $Database->SQLStatement = "SELECT ImgData, ImgType, ImgExt, ImgWidth,
    ImgHeight FROM ProductPic WHERE ProductId=". $_REQUEST[ pid ];
    $Pic = $Database->Query();
    $type = $Pic[ 0 ][ ImgType ];
    // set headers for image mime type
    Header( "Content-type: $type");


    // request thumbnail
    if ( $_REQUEST[ t ] == "tmb" )
    {
    $newWidth = 60;
    $factor = $newWidth * $Pic[ 0 ][ ImgWidth ];
    $newHeight = $factor * $Pic[ 0 ][ ImgHeight ];

    // resample
    $image_p = imagecreatetrue color($newWidth , $newHeight);
    $image = imagecreatefrom string( $Pic[ 0 ][ ImgData ] );
    imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $newWidth,
    $newHeight, $Pic[ 0 ][ ImgWidth ], $Pic[ 0 ][ ImgHeight ]);

    // output
    switch ( $Pic[ 0 ][ ImgExt ] )
    {
    case 1:
    imagegif($image _p, null, 75);
    break;
    case 2:
    imagejpeg($imag e_p, null, 75);
    break;
    case 3:
    imagepng($image _p, null, 75);
    break;
    }
    }
    else // request full size image
    {
    $data = $Pic[ 0 ][ ImgData ];
    echo $data;
    }

    I know this problem if the memory limit is set to 8 MB but what the
    hell is the solution here?

    the free command on the linux-console returned:

    total used free shared buffers
    cached
    Mem: 508588 122300 386288 0 14572
    61948
    -/+ buffers/cache: 45780 462808
    Swap: 497972 0 497972


    kind regards
    flo

  • d

    #2
    Re: Allowed memory size exhausted

    <f.amann@rona.a t> wrote in message
    news:1139497694 .260108.301920@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > hi there!
    >
    > i save images in a mysql db and i wrote a script called getimage.php to
    > display them.
    > in my getimage.php i want to resize my images to display them as
    > thumbnails but there always occurs the following error:
    >
    > <b>Fatal error</b>: Allowed memory size of 188743680 bytes exhausted
    > (tried to allocate 240 bytes) in <b>.../public_html/getimage.php</b> on
    > line <b>x</b><br />
    >
    > it always tries to allocate either 240 or 60 bytes.
    > line x equals: $image_p = imagecreatetrue color($newWidth , $newHeight);
    >
    > my configuration:
    >
    > PHP Version 4.3.10
    > Apache/2.0.49 (Linux/SuSE)
    > GD Version: bundled (2.0.15 compatible)
    > memory_limit 180M !!!!!!!!
    >
    > the machine has 512MB RAM.
    >
    > I tried it with a jpg:
    > - the size was 163kb.
    > - the width was 533px
    > - the height was 400px
    >
    > it was not a really big image but it also didn't worked with smaller
    > ones.
    >
    > my code:
    > include "inc/Main.inc";
    > $Main = new Main();
    > $Database = new Database();
    > $Database->SQLStatement = "SELECT ImgData, ImgType, ImgExt, ImgWidth,
    > ImgHeight FROM ProductPic WHERE ProductId=". $_REQUEST[ pid ];
    > $Pic = $Database->Query();
    > $type = $Pic[ 0 ][ ImgType ];
    > // set headers for image mime type
    > Header( "Content-type: $type");
    >
    >
    > // request thumbnail
    > if ( $_REQUEST[ t ] == "tmb" )
    > {
    > $newWidth = 60;
    > $factor = $newWidth * $Pic[ 0 ][ ImgWidth ];
    > $newHeight = $factor * $Pic[ 0 ][ ImgHeight ];
    >
    > // resample
    > $image_p = imagecreatetrue color($newWidth , $newHeight);
    > $image = imagecreatefrom string( $Pic[ 0 ][ ImgData ] );
    > imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $newWidth,
    > $newHeight, $Pic[ 0 ][ ImgWidth ], $Pic[ 0 ][ ImgHeight ]);
    >
    > // output
    > switch ( $Pic[ 0 ][ ImgExt ] )
    > {
    > case 1:
    > imagegif($image _p, null, 75);
    > break;
    > case 2:
    > imagejpeg($imag e_p, null, 75);
    > break;
    > case 3:
    > imagepng($image _p, null, 75);
    > break;
    > }
    > }
    > else // request full size image
    > {
    > $data = $Pic[ 0 ][ ImgData ];
    > echo $data;
    > }
    >
    > I know this problem if the memory limit is set to 8 MB but what the
    > hell is the solution here?
    >
    > the free command on the linux-console returned:
    >
    > total used free shared buffers
    > cached
    > Mem: 508588 122300 386288 0 14572
    > 61948
    > -/+ buffers/cache: 45780 462808
    > Swap: 497972 0 497972
    >
    >
    > kind regards
    > flo
    >[/color]

    Take a look at imagedestroy():



    You should use that on all images after you have finished using them, as it
    frees the memory used by/associated with that image.

    One more note - why don't you cache your thumbnails? That way you don't
    have to generate them every time.

    (also, purely fyi, you might want to consider putting quotes around string
    keys in your arrays - PHP accepts it when you don't, but it's regarded as
    bad practice to do so)


    Comment

    • f.amann@rona.at

      #3
      Re: Allowed memory size exhausted

      thank you... the problem was that i just turned my brain on yet ;-)

      the factor i used was: $factor = $newWidth * $Pic[ 0 ][ ImgWidth ];
      but i want to resize, so the factor should be this: $factor = $newWidth
      / $Pic[ 0 ][ ImgWidth ];

      after debugging my $factor the newWidth and newHeight parameters for
      the imagecreatetrue color command were correct!

      Comment

      • d

        #4
        Re: Allowed memory size exhausted

        <f.amann@rona.a t> wrote in message
        news:1139498869 .829399.70920@g 44g2000cwa.goog legroups.com...[color=blue]
        > thank you... the problem was that i just turned my brain on yet ;-)
        >
        > the factor i used was: $factor = $newWidth * $Pic[ 0 ][ ImgWidth ];
        > but i want to resize, so the factor should be this: $factor = $newWidth
        > / $Pic[ 0 ][ ImgWidth ];
        >
        > after debugging my $factor the newWidth and newHeight parameters for
        > the imagecreatetrue color command were correct![/color]

        Good work :)

        Still - don't forget the imagedestroy() function, or caching your
        thumbnails - they'll save you a LOT of work down the road.

        dave


        Comment

        Working...