100KB image that takes up 2mb of memory uses up all 8mb of memory - help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    100KB image that takes up 2mb of memory uses up all 8mb of memory - help!

    I have an image that's only 100K in size, and I am working with 8mb of
    memory. If I do this:

    [PHP]
    print_r(ceil((i nt)ini_get('mem ory_limit') * 10 *
    filesize(actual _path("$locatio nPath/$this->fileName")) / 1000000) .
    'M'); // PRINTS OUT "2M" for 2mb
    ]/PHP]

    The image itself requires far less than the maximum amount of memory
    required.

    However, the moment I do this:

    [PHP]
    $imagestring =
    @file_get_conte nts(actual_path (realpath("$loc ationPath/" .
    $this->{$section . '_name'})));
    [/PHP]

    It locks up and forces a download of "index.php" (which indicates to me
    that the memory allotment of 8mb has been used up).

    How in the world can a 100KB image that uses only 2mb of 8mb use up 8mb
    of memory on file_get_conten ts() alone? I'm completely confused!

    Thanx
    Phil

  • Andy Hassall

    #2
    Re: 100KB image that takes up 2mb of memory uses up all 8mb of memory - help!

    On 25 Sep 2006 13:41:02 -0700, "comp.lang. php" <phillip.s.powe ll@gmail.com>
    wrote:
    >I have an image that's only 100K in size, and I am working with 8mb of
    >memory. If I do this:
    >
    >[PHP]
    >print_r(ceil(( int)ini_get('me mory_limit') * 10 *
    >filesize(actua l_path("$locati onPath/$this->fileName")) / 1000000) .
    >'M'); // PRINTS OUT "2M" for 2mb
    >]/PHP]
    Maybe I'm having a dense moment, but what does this value even mean?
    memory_limit times 10 times size of file then divided by a million?

    Is the output actually "2M" ? If you have a 100,000 byte file, doesn't that
    mean your memory_limit is set to "2" ? (presumably "2M", turning into "2" after
    the cast to int).
    >The image itself requires far less than the maximum amount of memory
    >required.
    >
    >However, the moment I do this:
    >
    >[PHP]
    > $imagestring =
    >@file_get_cont ents(actual_pat h(realpath("$lo cationPath/" .
    >$this->{$section . '_name'})));
    >[/PHP]
    >
    >It locks up and forces a download of "index.php" (which indicates to me
    >that the memory allotment of 8mb has been used up).
    That seems like a guess... what error messages do you get? If you can't see
    them, change the content-type of the output back to text.
    >How in the world can a 100KB image that uses only 2mb of 8mb use up 8mb
    >of memory on file_get_conten ts() alone? I'm completely confused!
    Why would a 100KB image use 2MB? Where did 8MB come from? I'm also confused...

    The usual pitfall is that a 100KB image can easily decompress to several
    megabytes, and that GD uses even more memory than you might expect.

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • comp.lang.php

      #3
      Re: 100KB image that takes up 2mb of memory uses up all 8mb of memory - help!


      Andy Hassall wrote:
      On 25 Sep 2006 13:41:02 -0700, "comp.lang. php" <phillip.s.powe ll@gmail.com>
      wrote:
      >
      I have an image that's only 100K in size, and I am working with 8mb of
      memory. If I do this:

      [PHP]
      print_r(ceil((i nt)ini_get('mem ory_limit') * 10 *
      filesize(actual _path("$locatio nPath/$this->fileName")) / 1000000) .
      'M'); // PRINTS OUT "2M" for 2mb
      ]/PHP]
      >
      Maybe I'm having a dense moment, but what does this value even mean?
      memory_limit times 10 times size of file then divided by a million?
      >
      Is the output actually "2M" ? If you have a 100,000 byte file, doesn't that
      mean your memory_limit is set to "2" ? (presumably "2M", turning into "2" after
      the cast to int).
      >
      Ok I'll make it super simple because I have to explain it so that I
      understand. I want to increase memory because "8M" is 8mb which for
      some images is not enough memory to run imagecreatefrom jpeg, or
      imagecreatefrom gif or imagecreatefrom png.. so I am going to increase it
      from 8MB to something like "130mb" or "130M" if need be for very large
      images. It works every time, even if it's an eventual bad idea.

      However, this image is 87KB in size and if I use the same formula to
      "increase memory", instead I "decrease memory instead"
      The image itself requires far less than the maximum amount of memory
      required.

      However, the moment I do this:

      [PHP]
      $imagestring =
      @file_get_conte nts(actual_path (realpath("$loc ationPath/" .
      $this->{$section . '_name'})));
      [/PHP]

      It locks up and forces a download of "index.php" (which indicates to me
      that the memory allotment of 8mb has been used up).
      >
      That seems like a guess... what error messages do you get? If you can't see
      them, change the content-type of the output back to text.
      I see no errors not even in the PHP error log, absolutely nothing is
      logged. Tried changing content-type to text/plain, to no avail, still
      force-downloads index.php
      >
      How in the world can a 100KB image that uses only 2mb of 8mb use up 8mb
      of memory on file_get_conten ts() alone? I'm completely confused!
      >
      Why would a 100KB image use 2MB? Where did 8MB come from? I'm also confused...
      Sorry but I have no idea whatsoever. Can someone else elaborate?
      >
      The usual pitfall is that a 100KB image can easily decompress to several
      megabytes, and that GD uses even more memory than you might expect.
      >
      --
      Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

      Comment

      • Simon Stewart

        #4
        Re: 100KB image that takes up 2mb of memory uses up all 8mb of memory - help!

        Andy

        What size is the image you are loading in megaPIXELS?
        This is often the limiting factor in PHP GD processing, not the original
        size in Kb or the final size in Kb.
        When I ran into this I found that PHP stopped abruptly with an error
        message.

        -- Simon


        "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
        news:1159234304 .867023.36210@e 3g2000cwe.googl egroups.com...
        >
        Andy Hassall wrote:
        >On 25 Sep 2006 13:41:02 -0700, "comp.lang. php"
        ><phillip.s.pow ell@gmail.com>
        >wrote:
        >>
        >I have an image that's only 100K in size, and I am working with 8mb of
        >memory. If I do this:
        >
        >[PHP]
        >print_r(ceil(( int)ini_get('me mory_limit') * 10 *
        >filesize(actua l_path("$locati onPath/$this->fileName")) / 1000000) .
        >'M'); // PRINTS OUT "2M" for 2mb
        >]/PHP]
        >>
        > Maybe I'm having a dense moment, but what does this value even mean?
        >memory_limit times 10 times size of file then divided by a million?
        >>
        > Is the output actually "2M" ? If you have a 100,000 byte file, doesn't
        >that
        >mean your memory_limit is set to "2" ? (presumably "2M", turning into "2"
        >after
        >the cast to int).
        >>
        >
        Ok I'll make it super simple because I have to explain it so that I
        understand. I want to increase memory because "8M" is 8mb which for
        some images is not enough memory to run imagecreatefrom jpeg, or
        imagecreatefrom gif or imagecreatefrom png.. so I am going to increase it
        from 8MB to something like "130mb" or "130M" if need be for very large
        images. It works every time, even if it's an eventual bad idea.
        >
        However, this image is 87KB in size and if I use the same formula to
        "increase memory", instead I "decrease memory instead"
        >
        >The image itself requires far less than the maximum amount of memory
        >required.
        >
        >However, the moment I do this:
        >
        >[PHP]
        $imagestring =
        >@file_get_cont ents(actual_pat h(realpath("$lo cationPath/" .
        >$this->{$section . '_name'})));
        >[/PHP]
        >
        >It locks up and forces a download of "index.php" (which indicates to me
        >that the memory allotment of 8mb has been used up).
        >>
        > That seems like a guess... what error messages do you get? If you can't
        >see
        >them, change the content-type of the output back to text.
        >
        I see no errors not even in the PHP error log, absolutely nothing is
        logged. Tried changing content-type to text/plain, to no avail, still
        force-downloads index.php
        >
        >>
        >How in the world can a 100KB image that uses only 2mb of 8mb use up 8mb
        >of memory on file_get_conten ts() alone? I'm completely confused!
        >>
        > Why would a 100KB image use 2MB? Where did 8MB come from? I'm also
        >confused...
        >
        Sorry but I have no idea whatsoever. Can someone else elaborate?
        >>
        > The usual pitfall is that a 100KB image can easily decompress to several
        >megabytes, and that GD uses even more memory than you might expect.
        >>
        >--
        >Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
        >http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
        >

        Comment

        • comp.lang.php

          #5
          Re: 100KB image that takes up 2mb of memory uses up all 8mb of memory - help!


          Simon Stewart wrote:
          Andy
          >
          What size is the image you are loading in megaPIXELS?
          This is often the limiting factor in PHP GD processing, not the original
          size in Kb or the final size in Kb.
          When I ran into this I found that PHP stopped abruptly with an error
          message.
          >
          The GIF image is Size: 332 x 287 px = 95284 sq px which is less than
          0.1 sq mpx

          in other words, a tiny image

          Phil
          -- Simon
          >
          >
          "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
          news:1159234304 .867023.36210@e 3g2000cwe.googl egroups.com...

          Andy Hassall wrote:
          On 25 Sep 2006 13:41:02 -0700, "comp.lang. php"
          <phillip.s.powe ll@gmail.com>
          wrote:
          >
          I have an image that's only 100K in size, and I am working with 8mb of
          memory. If I do this:

          [PHP]
          print_r(ceil((i nt)ini_get('mem ory_limit') * 10 *
          filesize(actual _path("$locatio nPath/$this->fileName")) / 1000000) .
          'M'); // PRINTS OUT "2M" for 2mb
          ]/PHP]
          >
          Maybe I'm having a dense moment, but what does this value even mean?
          memory_limit times 10 times size of file then divided by a million?
          >
          Is the output actually "2M" ? If you have a 100,000 byte file, doesn't
          that
          mean your memory_limit is set to "2" ? (presumably "2M", turning into "2"
          after
          the cast to int).
          >
          Ok I'll make it super simple because I have to explain it so that I
          understand. I want to increase memory because "8M" is 8mb which for
          some images is not enough memory to run imagecreatefrom jpeg, or
          imagecreatefrom gif or imagecreatefrom png.. so I am going to increase it
          from 8MB to something like "130mb" or "130M" if need be for very large
          images. It works every time, even if it's an eventual bad idea.

          However, this image is 87KB in size and if I use the same formula to
          "increase memory", instead I "decrease memory instead"
          The image itself requires far less than the maximum amount of memory
          required.

          However, the moment I do this:

          [PHP]
          $imagestring =
          @file_get_conte nts(actual_path (realpath("$loc ationPath/" .
          $this->{$section . '_name'})));
          [/PHP]

          It locks up and forces a download of "index.php" (which indicates to me
          that the memory allotment of 8mb has been used up).
          >
          That seems like a guess... what error messages do you get? If you can't
          see
          them, change the content-type of the output back to text.
          I see no errors not even in the PHP error log, absolutely nothing is
          logged. Tried changing content-type to text/plain, to no avail, still
          force-downloads index.php
          >
          How in the world can a 100KB image that uses only 2mb of 8mb use up 8mb
          of memory on file_get_conten ts() alone? I'm completely confused!
          >
          Why would a 100KB image use 2MB? Where did 8MB come from? I'm also
          confused...
          Sorry but I have no idea whatsoever. Can someone else elaborate?
          >
          The usual pitfall is that a 100KB image can easily decompress to several
          megabytes, and that GD uses even more memory than you might expect.
          >
          --
          Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
          http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

          Comment

          • Simon Stewart

            #6
            Re: 100KB image that takes up 2mb of memory uses up all 8mb of memory - help!

            Phil

            yes that's far in pixels smaller than when I had problems
            the 8Mb memory_limit you have for PHP is low. Hosting companies often
            allocate 16 or 32Mb.
            have you tried doing memory_get_usag e calls before the problem code? Could
            the program or environment be short on memory anyway?

            Simon.

            "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
            news:1159281310 .958733.46560@e 3g2000cwe.googl egroups.com...
            >
            Simon Stewart wrote:
            >Andy
            >>
            >What size is the image you are loading in megaPIXELS?
            >This is often the limiting factor in PHP GD processing, not the original
            >size in Kb or the final size in Kb.
            >When I ran into this I found that PHP stopped abruptly with an error
            >message.
            >>
            >
            The GIF image is Size: 332 x 287 px = 95284 sq px which is less than
            0.1 sq mpx
            >
            in other words, a tiny image
            >
            Phil
            >
            >-- Simon
            >>
            >>
            >"comp.lang.php " <phillip.s.powe ll@gmail.comwro te in message
            >news:115923430 4.867023.36210@ e3g2000cwe.goog legroups.com...
            >
            Andy Hassall wrote:
            >On 25 Sep 2006 13:41:02 -0700, "comp.lang. php"
            ><phillip.s.pow ell@gmail.com>
            >wrote:
            >>
            >I have an image that's only 100K in size, and I am working with 8mb
            >of
            >memory. If I do this:
            >
            >[PHP]
            >print_r(ceil(( int)ini_get('me mory_limit') * 10 *
            >filesize(actua l_path("$locati onPath/$this->fileName")) / 1000000) .
            >'M'); // PRINTS OUT "2M" for 2mb
            >]/PHP]
            >>
            > Maybe I'm having a dense moment, but what does this value even mean?
            >memory_limit times 10 times size of file then divided by a million?
            >>
            > Is the output actually "2M" ? If you have a 100,000 byte file,
            >doesn't
            >that
            >mean your memory_limit is set to "2" ? (presumably "2M", turning into
            >"2"
            >after
            >the cast to int).
            >>
            >
            Ok I'll make it super simple because I have to explain it so that I
            understand. I want to increase memory because "8M" is 8mb which for
            some images is not enough memory to run imagecreatefrom jpeg, or
            imagecreatefrom gif or imagecreatefrom png.. so I am going to increase it
            from 8MB to something like "130mb" or "130M" if need be for very large
            images. It works every time, even if it's an eventual bad idea.
            >
            However, this image is 87KB in size and if I use the same formula to
            "increase memory", instead I "decrease memory instead"
            >
            >The image itself requires far less than the maximum amount of memory
            >required.
            >
            >However, the moment I do this:
            >
            >[PHP]
            $imagestring =
            >@file_get_cont ents(actual_pat h(realpath("$lo cationPath/" .
            >$this->{$section . '_name'})));
            >[/PHP]
            >
            >It locks up and forces a download of "index.php" (which indicates to
            >me
            >that the memory allotment of 8mb has been used up).
            >>
            > That seems like a guess... what error messages do you get? If you
            >can't
            >see
            >them, change the content-type of the output back to text.
            >
            I see no errors not even in the PHP error log, absolutely nothing is
            logged. Tried changing content-type to text/plain, to no avail, still
            force-downloads index.php
            >
            >>
            >How in the world can a 100KB image that uses only 2mb of 8mb use up
            >8mb
            >of memory on file_get_conten ts() alone? I'm completely confused!
            >>
            > Why would a 100KB image use 2MB? Where did 8MB come from? I'm also
            >confused...
            >
            Sorry but I have no idea whatsoever. Can someone else elaborate?
            >>
            > The usual pitfall is that a 100KB image can easily decompress to
            >several
            >megabytes, and that GD uses even more memory than you might expect.
            >>
            >--
            >Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
            >http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis
            >tool
            >
            >

            Comment

            • comp.lang.php

              #7
              Re: 100KB image that takes up 2mb of memory uses up all 8mb of memory - help!


              Simon Stewart wrote:
              Phil
              >
              yes that's far in pixels smaller than when I had problems
              the 8Mb memory_limit you have for PHP is low. Hosting companies often
              allocate 16 or 32Mb.
              have you tried doing memory_get_usag e calls before the problem code? Could
              the program or environment be short on memory anyway?
              >
              I was suspecting that most places use more than 8mb default memory;
              however, it appears customized as our servers here come "out of the
              box" with RHEL 4 which sets the memory_limit within php.ini to 8mb.
              It's easy to fix that, of course, but just thought I'd share.

              I wrote some code that apparently seems to solve the memory issue:

              [PHP]
              $mem = memory_get_usag e();
              list($width, $height) =
              @getimagesize(a ctual_path(real path("$this->locationPath/$this->fileName"))) ;
              if ($willIncreaseM emory && (int)ini_get('m emory_limit') * 10 *
              @filesize(actua l_path("$this->locationPath/$this->fileName")) $mem) {
              @ini_set('memor y_limit', ceil((int)ini_g et('memory_limi t') * 10 *
              filesize(actual _path("$this->locationPath/$this->fileName")) / 100000)
              .. 'M');
              } elseif ($willIncreaseM emory && (int)$width 0 && (int)$height >
              0) {
              @ini_set('memor y_limit', ceil((int)$widt h * (int)$height * 5 /
              10000) . 'M');
              }
              [/PHP]
              Simon.
              >
              "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
              news:1159281310 .958733.46560@e 3g2000cwe.googl egroups.com...

              Simon Stewart wrote:
              Andy
              >
              What size is the image you are loading in megaPIXELS?
              This is often the limiting factor in PHP GD processing, not the original
              size in Kb or the final size in Kb.
              When I ran into this I found that PHP stopped abruptly with an error
              message.
              >
              The GIF image is Size: 332 x 287 px = 95284 sq px which is less than
              0.1 sq mpx

              in other words, a tiny image

              Phil
              -- Simon
              >
              >
              "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
              news:1159234304 .867023.36210@e 3g2000cwe.googl egroups.com...

              Andy Hassall wrote:
              On 25 Sep 2006 13:41:02 -0700, "comp.lang. php"
              <phillip.s.powe ll@gmail.com>
              wrote:
              >
              I have an image that's only 100K in size, and I am working with 8mb
              of
              memory. If I do this:

              [PHP]
              print_r(ceil((i nt)ini_get('mem ory_limit') * 10 *
              filesize(actual _path("$locatio nPath/$this->fileName")) / 1000000) .
              'M'); // PRINTS OUT "2M" for 2mb
              ]/PHP]
              >
              Maybe I'm having a dense moment, but what does this value even mean?
              memory_limit times 10 times size of file then divided by a million?
              >
              Is the output actually "2M" ? If you have a 100,000 byte file,
              doesn't
              that
              mean your memory_limit is set to "2" ? (presumably "2M", turning into
              "2"
              after
              the cast to int).
              >

              Ok I'll make it super simple because I have to explain it so that I
              understand. I want to increase memory because "8M" is 8mb which for
              some images is not enough memory to run imagecreatefrom jpeg, or
              imagecreatefrom gif or imagecreatefrom png.. so I am going to increase it
              from 8MB to something like "130mb" or "130M" if need be for very large
              images. It works every time, even if it's an eventual bad idea.

              However, this image is 87KB in size and if I use the same formula to
              "increase memory", instead I "decrease memory instead"

              The image itself requires far less than the maximum amount of memory
              required.

              However, the moment I do this:

              [PHP]
              $imagestring =
              @file_get_conte nts(actual_path (realpath("$loc ationPath/" .
              $this->{$section . '_name'})));
              [/PHP]

              It locks up and forces a download of "index.php" (which indicates to
              me
              that the memory allotment of 8mb has been used up).
              >
              That seems like a guess... what error messages do you get? If you
              can't
              see
              them, change the content-type of the output back to text.

              I see no errors not even in the PHP error log, absolutely nothing is
              logged. Tried changing content-type to text/plain, to no avail, still
              force-downloads index.php

              >
              How in the world can a 100KB image that uses only 2mb of 8mb use up
              8mb
              of memory on file_get_conten ts() alone? I'm completely confused!
              >
              Why would a 100KB image use 2MB? Where did 8MB come from? I'm also
              confused...

              Sorry but I have no idea whatsoever. Can someone else elaborate?
              >
              The usual pitfall is that a 100KB image can easily decompress to
              several
              megabytes, and that GD uses even more memory than you might expect.
              >
              --
              Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
              http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis
              tool

              Comment

              Working...