Show external images - best/fastest way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Show external images - best/fastest way?

Post by Guldstrand »

I'm working on developing a Webcam portal for Scandinavia, and would need some help.

There are about 300 webcams portal, (we have been given permission to display them).
Given that several webcam operators/owners do not want everyone to be able to see the direct link to their cameras, I had to "hide" the links using a php-file.

I collect and read all external images via a php file, and this makes the images load very slowly.
Is there an ultimate way to speed this up?

Below are parts of the code that I use to view the images.

Code: Select all

$bild = "link-to-external-image";

header ("Content-type: image/jpeg");

$string = "mydomain.com";
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefromjpeg($bild);
$x = imagesx($im) - $width -5;
$y = imagesy($im) - $height -5;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 255, 255, 255);
imagestring ($im, $font, $x, $y,  $string, $textColor);
imagejpeg($im);
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Show external images - best/fastest way?

Post by tr0gd0rr »

If you are going to create a copy of the image you can use copy() as long as remote url fopen is enabled. If you do need to use gd for some reason, cache the resulting image instead of creating it every time.

But maybe the simplest solution is to just send a 302 or 303 header:

Code: Select all

header('HTTP/1.1 303 See other'); // If you omit this line, a 302 "Moved temporarily" will automatically be sent instead
header("Location: $bild");
die();
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Re: Show external images - best/fastest way?

Post by Guldstrand »

Thanks for your reply.
I've allready "replaced" the GD-function, with readfile(), and that made the images load a LITTLE faster.

Since i'm working with webcam images, that changes every 10 seconds (as min.), cache won't do it.
Any other tips on how i can speed it up even more? The images is still loading pretty slow. :(
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Show external images - best/fastest way?

Post by tr0gd0rr »

At that point maybe start looking at the server load? Maybe you need a beefier server.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Show external images - best/fastest way?

Post by flying_circus »

Perhaps I dont completely understand what you are trying to do, but can you use curl and save the image to the local drive, then host the images from your server?

What kind of internet connection do the cameras have? If the link is saturated with requests, it will move slowly.
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Re: Show external images - best/fastest way?

Post by Guldstrand »

flying_circus wrote:Perhaps I dont completely understand what you are trying to do, but can you use curl and save the image to the local drive, then host the images from your server?

What kind of internet connection do the cameras have? If the link is saturated with requests, it will move slowly.
I have no idea what each camera has for speed/bandwidth.
And i can't downloaded all the images to my server, every 10 seconds. This means that the server will have to work even more. :(
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Show external images - best/fastest way?

Post by Weiry »

Guldstrand wrote:I have no idea what each camera has for speed/bandwidth.
And i can't downloaded all the images to my server, every 10 seconds. This means that the server will have to work even more. :(
That is generally how things works when you have a service which is constantly updating. At some point you have to make a choice about how often you want them to update. Besides... servers are there to work, if they aren't doing something, they are wasting time and resources.
flying_circus wrote:Perhaps I dont completely understand what you are trying to do, but can you use curl and save the image to the local drive, then host the images from your server?
flying_circus has a very good point, This would be the easiest solution would be to simply download the images if possible.

As a suggestion, i wouldn't be storing more than a single image from a given camera at any time. This means that you don't have to worry about storage and if your always using the same file name for storing the images, your pages could automatically refresh to show the updated image.
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Re: Show external images - best/fastest way?

Post by Guldstrand »

Hard drive space is no problem.
Since I also want to be able to show a live streams on the page (the main purpose of a Webcam portal), there is little sense to save the images on my server.

But I really appreciate all the tips and ideas.
Post Reply