Memory problem with imagecreatefromstring function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthieu MARC

    Memory problem with imagecreatefromstring function


    Hi everybody,

    I'm writting a script which take image data from mysql (blob) and draw
    the image to screen.

    At the end of the message you will find my php code. I put lots of
    numbered message about the memory usage.

    The image in the database is about 520ko.


    Here is the output in my browser :

    -1 - 34816
    0 - 123672
    0bis - 652064
    1 - 6693928
    2 - 6693928
    3 - 8957256
    4 - 8957336

    What I see is just before the use of the function imagecreatefrom string,
    the script is using about 650ko, and just after about 6,5Mo !

    I do not understand why it is using so much memory for just a 'little'
    picture.

    I'm using redhat fedora 1 linux distribution.

    php-4.3.3
    httpd-2.0.47
    gd-2.0.15

    Is someone understand my problem ?

    Thanks

    Matthieu MARC



    ------ PHP CODE -----
    <?php

    echo "-1 - ".memory_get_us age() . "\n<br>";
    include_once("i nclude/include.php");

    $photo_id = $_POST['id'];

    if ( $photo_id == '' ){
    $photo_id = $_GET['id'];
    }

    echo "0 - ".memory_get_us age() . "\n<br>";
    $photo_data = db_getphotodata fromid(session_ getuser(),$phot o_id);


    if ( $photo_data != NULL ){

    # header("Content-Type: ".$photo_da ta['type']);

    $size = 600;

    echo "0bis - ".memory_get_us age() . "\n<br>";

    $src = imagecreatefrom string($photo_d ata['data']);
    unset($photo_da ta);

    echo "1 - ".memory_get_us age() . "\n<br>";

    $width = imagesx($src);
    $height = imagesy($src);
    $aspect_ratio = $height/$width;

    if ($height <= $size) {
    $new_w = $width;
    $new_h = $height;
    } else {
    $new_h = $size;
    $new_w = abs($new_h / $aspect_ratio);
    }

    echo "2 - ".memory_get_us age() . "\n<br>";

    $img = imagecreatetrue color ($new_w,$new_h) ;

    echo "3 - ".memory_get_us age() . "\n<br>";

    imagecopyresamp led
    ($img,$src,0,0, 0,0,$new_w,$new _h,$width,$heig ht);

    echo "4 - ".memory_get_us age() . "\n<br>";

    imagejpeg($img, '', 90);

    echo "5 - ".memory_get_us age() . "\n<br>";

    imagedestroy($i mg);
    } else {

    header ("Content-Type: text/html");
    print ("no photo found");
    }

    ?>

    ------ END PHP CODE ------

  • Agelmar

    #2
    Re: Memory problem with imagecreatefrom string function

    A few things... one, you should NOT call unset() on a MySQL result. Call
    mysql_free_resu lt.
    2. gd images take up a considerable amount of memory, and if it's using a
    shared module (e.g. php was compiled with --enable-gd=shared or whatever) as
    opposed to it being compiled in, that will take a bit more memory as well.

    Matthieu MARC wrote:[color=blue]
    > Hi everybody,
    >
    > I'm writting a script which take image data from mysql (blob) and draw
    > the image to screen.
    >
    > At the end of the message you will find my php code. I put lots of
    > numbered message about the memory usage.
    >
    > The image in the database is about 520ko.
    >
    >
    > Here is the output in my browser :
    >
    > -1 - 34816
    > 0 - 123672
    > 0bis - 652064
    > 1 - 6693928
    > 2 - 6693928
    > 3 - 8957256
    > 4 - 8957336
    >
    > What I see is just before the use of the function
    > imagecreatefrom string, the script is using about 650ko, and just
    > after about 6,5Mo !
    >
    > I do not understand why it is using so much memory for just a 'little'
    > picture.
    >
    > I'm using redhat fedora 1 linux distribution.
    >
    > php-4.3.3
    > httpd-2.0.47
    > gd-2.0.15
    >
    > Is someone understand my problem ?
    >
    > Thanks
    >
    > Matthieu MARC
    >
    >
    >
    > ------ PHP CODE -----
    > <?php
    >
    > echo "-1 - ".memory_get_us age() . "\n<br>";
    > include_once("i nclude/include.php");
    >
    > $photo_id = $_POST['id'];
    >
    > if ( $photo_id == '' ){
    > $photo_id = $_GET['id'];
    > }
    >
    > echo "0 - ".memory_get_us age() . "\n<br>";
    > $photo_data = db_getphotodata fromid(session_ getuser(),$phot o_id);
    >
    >
    > if ( $photo_data != NULL ){
    >
    > # header("Content-Type: ".$photo_da ta['type']);
    >
    > $size = 600;
    >
    > echo "0bis - ".memory_get_us age() . "\n<br>";
    >
    > $src = imagecreatefrom string($photo_d ata['data']);
    > unset($photo_da ta);
    >
    > echo "1 - ".memory_get_us age() . "\n<br>";
    >
    > $width = imagesx($src);
    > $height = imagesy($src);
    > $aspect_ratio = $height/$width;
    >
    > if ($height <= $size) {
    > $new_w = $width;
    > $new_h = $height;
    > } else {
    > $new_h = $size;
    > $new_w = abs($new_h / $aspect_ratio);
    > }
    >
    > echo "2 - ".memory_get_us age() . "\n<br>";
    >
    > $img = imagecreatetrue color ($new_w,$new_h) ;
    >
    > echo "3 - ".memory_get_us age() . "\n<br>";
    >
    > imagecopyresamp led
    > ($img,$src,0,0, 0,0,$new_w,$new _h,$width,$heig ht);
    >
    > echo "4 - ".memory_get_us age() . "\n<br>";
    >
    > imagejpeg($img, '', 90);
    >
    > echo "5 - ".memory_get_us age() . "\n<br>";
    >
    > imagedestroy($i mg);
    > } else {
    >
    > header ("Content-Type: text/html");
    > print ("no photo found");
    > }
    >[color=green]
    >>[/color]
    >
    > ------ END PHP CODE ------[/color]


    Comment

    • Andy Hassall

      #3
      Re: Memory problem with imagecreatefrom string function

      On Fri, 09 Jan 2004 15:25:05 +0100, Matthieu MARC
      <matthieu.marc@ crisi.unicaen.f r> wrote:
      [color=blue]
      >I'm writting a script which take image data from mysql (blob) and draw
      >the image to screen.
      >
      >At the end of the message you will find my php code. I put lots of
      >numbered message about the memory usage.
      >
      >The image in the database is about 520ko.[/color]

      In what format?
      [color=blue]
      >What I see is just before the use of the function imagecreatefrom string,
      > the script is using about 650ko, and just after about 6,5Mo ![/color]

      If it's a compressed format in the database (JPEG, PNG, GIF), expanding from
      520kb to 6.5Mb isn't too surprising - GD is likely to be using an uncompressed
      format in memory.

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

      Comment

      • Chung Leong

        #4
        Re: Memory problem with imagecreatefrom string function

        You should resize the picture first, then it into the database. Resampling a
        image takes quite a bit of time. You don't want to do it on every request.

        Uzytkownik "Matthieu MARC" <matthieu.marc@ crisi.unicaen.f r> napisal w
        wiadomosci news:btmdkn$h3j 1$1@baligan1.un icaen.fr...[color=blue]
        >
        > Hi everybody,
        >
        > I'm writting a script which take image data from mysql (blob) and draw
        > the image to screen.
        >
        > At the end of the message you will find my php code. I put lots of
        > numbered message about the memory usage.
        >
        > The image in the database is about 520ko.
        >
        >
        > Here is the output in my browser :
        >
        > -1 - 34816
        > 0 - 123672
        > 0bis - 652064
        > 1 - 6693928
        > 2 - 6693928
        > 3 - 8957256
        > 4 - 8957336
        >
        > What I see is just before the use of the function imagecreatefrom string,
        > the script is using about 650ko, and just after about 6,5Mo !
        >
        > I do not understand why it is using so much memory for just a 'little'
        > picture.
        >
        > I'm using redhat fedora 1 linux distribution.
        >
        > php-4.3.3
        > httpd-2.0.47
        > gd-2.0.15
        >
        > Is someone understand my problem ?
        >
        > Thanks
        >
        > Matthieu MARC
        >
        >
        >
        > ------ PHP CODE -----
        > <?php
        >
        > echo "-1 - ".memory_get_us age() . "\n<br>";
        > include_once("i nclude/include.php");
        >
        > $photo_id = $_POST['id'];
        >
        > if ( $photo_id == '' ){
        > $photo_id = $_GET['id'];
        > }
        >
        > echo "0 - ".memory_get_us age() . "\n<br>";
        > $photo_data = db_getphotodata fromid(session_ getuser(),$phot o_id);
        >
        >
        > if ( $photo_data != NULL ){
        >
        > # header("Content-Type: ".$photo_da ta['type']);
        >
        > $size = 600;
        >
        > echo "0bis - ".memory_get_us age() . "\n<br>";
        >
        > $src = imagecreatefrom string($photo_d ata['data']);
        > unset($photo_da ta);
        >
        > echo "1 - ".memory_get_us age() . "\n<br>";
        >
        > $width = imagesx($src);
        > $height = imagesy($src);
        > $aspect_ratio = $height/$width;
        >
        > if ($height <= $size) {
        > $new_w = $width;
        > $new_h = $height;
        > } else {
        > $new_h = $size;
        > $new_w = abs($new_h / $aspect_ratio);
        > }
        >
        > echo "2 - ".memory_get_us age() . "\n<br>";
        >
        > $img = imagecreatetrue color ($new_w,$new_h) ;
        >
        > echo "3 - ".memory_get_us age() . "\n<br>";
        >
        > imagecopyresamp led
        > ($img,$src,0,0, 0,0,$new_w,$new _h,$width,$heig ht);
        >
        > echo "4 - ".memory_get_us age() . "\n<br>";
        >
        > imagejpeg($img, '', 90);
        >
        > echo "5 - ".memory_get_us age() . "\n<br>";
        >
        > imagedestroy($i mg);
        > } else {
        >
        > header ("Content-Type: text/html");
        > print ("no photo found");
        > }
        >
        > ?>
        >
        > ------ END PHP CODE ------
        >[/color]


        Comment

        Working...