Creating an image - speed issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Fischer

    Creating an image - speed issues

    Hello,

    I am writing a gallery script and use imagecreatefrom jpeg and fpassthru
    to output images without any change to them (i.e., no thumbnail
    creation etc. - just deliver the image via the php script). However I
    note that the image creation is quite slow - the user can see that the
    image is created line-by-line, from top to bottom.

    Most likely this speed issue comes from PHP, since PHP is simply slower
    than C etc.

    However, if I use a "profession al" script like e.g. phpThumb, I notice
    that the image display is much, much faster - almost as if I had the
    image displayed directly.

    What can I do to make my images faster? If there's nothing (simple)
    that I can do, is there at least a way to make the images as
    progressive JPGs, i.e. they don't build up from top to bottom
    line-by-line, but the image starts as a blurry image and gets sharper
    with time?

    Thanks!
    Alex

  • Andy Hassall

    #2
    Re: Creating an image - speed issues

    On 6 Oct 2006 09:00:14 -0700, "Alexander Fischer" <happysmithers@ hotmail.com>
    wrote:
    >I am writing a gallery script and use imagecreatefrom jpeg and fpassthru
    >to output images without any change to them (i.e., no thumbnail
    >creation etc. - just deliver the image via the php script). However I
    >note that the image creation is quite slow - the user can see that the
    >image is created line-by-line, from top to bottom.
    Arguably that's the image output rather than creation. The GD library doesn't
    stream the image out as it creates it, rather it's all done in memory (which
    may take some time, but the browser won't be getting any image data during this
    time), then dumped out to the browser in one go.

    You'd expect the final output part to be somewhat slower than serving a file
    out directly, but no slower than any other large output from PHP - which can
    still serve out data quite quickly.

    Is all large-ish output from PHP on your server slow? What about compared with
    serving static files from the same server?
    >Most likely this speed issue comes from PHP, since PHP is simply slower
    >than C etc.
    >
    >However, if I use a "profession al" script like e.g. phpThumb, I notice
    >that the image display is much, much faster - almost as if I had the
    >image displayed directly.
    They may well process the image to a temporary location on disk (so it's only
    processed once) and then serve it out directly through the webserver, or just
    readfile() it (although you mentioned you're using fpassthru which is similar
    in purpose).

    Looking up phpThumb on Google, it mentions it has the option to process via
    ImageMagick, which is less integrated with PHP but can give better performance
    for processing photo images (GD is aimed more at generating block graphics
    rather than processing photos).
    >What can I do to make my images faster? If there's nothing (simple)
    >that I can do, is there at least a way to make the images as
    >progressive JPGs, i.e. they don't build up from top to bottom
    >line-by-line, but the image starts as a blurry image and gets sharper
    >with time?
    The GD library has an interlacing option, which (according to the docs anyway)
    for JPEG it actually interprets as using progressive mode - look up
    imageinterlace( ).

    --
    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

    • Jerry Stuckle

      #3
      Re: Creating an image - speed issues

      Alexander Fischer wrote:
      Hello,
      >
      I am writing a gallery script and use imagecreatefrom jpeg and fpassthru
      to output images without any change to them (i.e., no thumbnail
      creation etc. - just deliver the image via the php script). However I
      note that the image creation is quite slow - the user can see that the
      image is created line-by-line, from top to bottom.
      >
      Most likely this speed issue comes from PHP, since PHP is simply slower
      than C etc.
      >
      However, if I use a "profession al" script like e.g. phpThumb, I notice
      that the image display is much, much faster - almost as if I had the
      image displayed directly.
      >
      What can I do to make my images faster? If there's nothing (simple)
      that I can do, is there at least a way to make the images as
      progressive JPGs, i.e. they don't build up from top to bottom
      line-by-line, but the image starts as a blurry image and gets sharper
      with time?
      >
      Thanks!
      Alex
      >
      Alex,

      Are you trying to resize the image in your html? If so, bad idea. This
      will send the entire image file to the browser and display the behavior
      you are experiencing. It also unnecessarily inflates the bandwidth
      you're using.

      "Profession al" scripts create a thumbnail and send it instead. The size
      difference is quite significant for larger pictures. And even for
      smaller images the difference is usually quite noticeable.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Alexander Fischer

        #4
        Re: Creating an image - speed issues

        Jerry,
        thanks for your reply.
        Are you trying to resize the image in your html? If so, bad idea. This
        will send the entire image file to the browser and display the behavior
        you are experiencing. It also unnecessarily inflates the bandwidth
        you're using.
        No, of course not. When I need a thumbnail, I resample the original
        image within PHP. However, I'm having speed issues also and primarily
        when I'm simply showing the original-sized (800x600px) image..

        Alex

        Comment

        • Alexander Fischer

          #5
          Re: Creating an image - speed issues

          Hi Andy!
          You'd expect the final output part to be somewhat slower than serving a file
          out directly, but no slower than any other large output from PHP - which can
          still serve out data quite quickly.
          >
          Is all large-ish output from PHP on your server slow? What about compared with
          serving static files from the same server?
          I don't think that all PHP output is slow. Well, I don't know - can't
          really compare. When I directly serve the image file, i.e. <img
          src="picture.jp g">, it's there in an instant. Same when I use phpThumb
          <img src=phpThumb.ph p?src=picture.j pg>. I'm talking about a 800x600
          image, so no huge sizes there. When I output it with my PHP code, which
          is serving some header data and than fpassthru, it takes about half a
          second for the whole image to appear, and it comes line-by-line, which
          is noticeable and annoying.
          The GD library has an interlacing option, which (according to the docs anyway)
          for JPEG it actually interprets as using progressive mode - look up
          imageinterlace( ).
          Will do. Any other ideas how I could find out whether my server is too
          slow, my code sucks, or whatever else is the problem here?

          Thanks,
          Alex

          Comment

          • Alexander Fischer

            #6
            Re: Creating an image - speed issues


            Alexander Fischer wrote:
            Will do. Any other ideas how I could find out whether my server is too
            slow, my code sucks, or whatever else is the problem here?
            Actually, now that I tried it again: when I output the image directly
            from the server, it has the same - slow - speed the first time it's
            loading. When I reload the page, it's there instantly - probably cached
            somehow. If I really reload everything by hitting Ctrl-F5, it takes
            again the half second or so.

            So obviously the server or the connection is slow in itself. But there
            is the different caching behavior - the static image gets cached, while
            my php-output images get fully reloaded every single time. This is
            especially annoying in thumbnail pages, as there first one sees all the
            images, and then they flicker through as one by one is being visibly
            rebuilt.

            Any suggestions on how I could make sure that also my PHP-created
            images will get cached the same way as static files?

            Thanks,
            Alex

            Comment

            • Jerry Stuckle

              #7
              Re: Creating an image - speed issues

              Alexander Fischer wrote:
              Jerry,
              thanks for your reply.
              >
              >
              >>Are you trying to resize the image in your html? If so, bad idea. This
              >>will send the entire image file to the browser and display the behavior
              >>you are experiencing. It also unnecessarily inflates the bandwidth
              >>you're using.
              >
              >
              No, of course not. When I need a thumbnail, I resample the original
              image within PHP. However, I'm having speed issues also and primarily
              when I'm simply showing the original-sized (800x600px) image..
              >
              Alex
              >
              Displaying the 800x600 can be slow - even on a broadband connection.
              How large is the file?

              And I doubt PHP itself it the problem - unless you're doing something
              really strange.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Alexander Fischer

                #8
                Re: Creating an image - speed issues

                Hi,

                Jerry Stuckle wrote:
                Displaying the 800x600 can be slow - even on a broadband connection.
                ok. Then I can live with that.
                How large is the file?
                Some 100 kb, on average...
                And I doubt PHP itself it the problem - unless you're doing something
                really strange.
                Well, the only thing that really annoys me is the behavior of the page
                in which I display some 30 thumbnails, which I create with this code:

                if($showasthumb ){
                // Content type
                header('Content-type: image/jpeg');
                // Get new dimensions
                list($width, $height) = getimagesize($i mgname);
                if($width>=$hei ght){$new_width = $mwthumbs_width ;$new_height =
                $height * $new_width / $width;}else{$n ew_height =
                $mwthumbs_heigh t;$new_width = $width * $new_height / $height;}
                // Resample
                $image_p = imagecreatetrue color($new_widt h, $new_height);
                imagecopyresamp led($image_p, $im, 0, 0, 0, 0, $new_width,
                $new_height, $width, $height);
                // Output
                imagejpeg($imag e_p, null, 100);
                exit();


                When the thumbs page is loaded for the first time, it takes a few
                moments to load, ok. Then I view an individual image. Then I click back
                in the browser. And now: I instantly get to see the whole page with all
                the images on it - probably from the cache. And then the browser starts
                to reload image after image, rebuilding them line by line, and this
                causes a flickering across the screen which takes around 2-3 seconds.

                Would there be a way - e.g., a header information - to tell the browser
                that these images do not need to be reloaded every single time?

                Thanks,
                Alex

                Comment

                • Andy Hassall

                  #9
                  Re: Creating an image - speed issues

                  On 6 Oct 2006 14:30:37 -0700, "Alexander Fischer" <happysmithers@ hotmail.com>
                  wrote:
                  >Any suggestions on how I could make sure that also my PHP-created
                  >images will get cached the same way as static files?
                  There are various HTTP headers that control caching, such as Cache-Control and
                  Expires:




                  If you've started a PHP session then PHP also sends headers to suppress
                  caching by default; you can override this though:



                  Have a look at the headers being returned for your image (for example with the
                  Live HTTP Headers extension for Firefox, or Fiddler for Internet Explorer).

                  --
                  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...