By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,759 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,759 IT Pros & Developers. It's quick & easy.

Creating an image - speed issues

P: n/a
Hello,

I am writing a gallery script and use imagecreatefromjpeg 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 "professional" 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

Oct 6 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 6 Oct 2006 09:00:14 -0700, "Alexander Fischer" <ha***********@hotmail.com>
wrote:
>I am writing a gallery script and use imagecreatefromjpeg 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 "professional" 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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 6 '06 #2

P: n/a
Alexander Fischer wrote:
Hello,

I am writing a gallery script and use imagecreatefromjpeg 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 "professional" 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.

"Professional" 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.
js*******@attglobal.net
==================
Oct 6 '06 #3

P: n/a
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

Oct 6 '06 #4

P: n/a
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.jpg">, it's there in an instant. Same when I use phpThumb
<img src=phpThumb.php?src=picture.jpg>. 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

Oct 6 '06 #5

P: n/a

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

Oct 6 '06 #6

P: n/a
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.
js*******@attglobal.net
==================
Oct 7 '06 #7

P: n/a
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($imgname);
if($width>=$height){$new_width = $mwthumbs_width;$new_height =
$height * $new_width / $width;}else{$new_height =
$mwthumbs_height;$new_width = $width * $new_height / $height;}
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $im, 0, 0, 0, 0, $new_width,
$new_height, $width, $height);
// Output
imagejpeg($image_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

Oct 7 '06 #8

P: n/a
On 6 Oct 2006 14:30:37 -0700, "Alexander Fischer" <ha***********@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:

http://www.w3.org/Protocols/rfc2616/...4.html#sec14.9
http://www.w3.org/Protocols/rfc2616/....html#sec14.21

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

http://uk2.php.net/session_cache_limiter

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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 7 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.