473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how can I tell if a remote file is an image

I've written a link aliasing script that allows users to rewrite urls
on their web pages. The crux of the script is the following:

if(getimagesize($redirect_to) == 0)
header("Location: $redirect_to");
else
include("$redirect_to");

Which uses getimagesize() to determine if the link specified in
$redirect_to points to an image (i.e img src) in which case it should
include the image in the page, or if the link points to another
website (i.e. a href), in which case I use the header method to
redirect to that page when the link alias is called.

So far this script has been working great, but I recently had a
customer run it under php4.2.2 and the getimagesize is taking approx.
160 seconds to execute.

Can anyone think of a different way to tell if a remote file is an
image? And also, has anyone else experienced this kind of behavior
with getimagesize when running php4.2.2?

Assistance is really appreciated.

Thanks,

Esoos
Jul 17 '05 #1
13 2577
Try to look on extension of $redirect_to. If it will image extension (.jpg,
..gif, .png, etc), so that is picture, otherwise other file type

"ebobnar" <eb*******@yahoo.com> ???????/???????? ? ???????? ?????????:
news:c9**************************@posting.google.c om...
I've written a link aliasing script that allows users to rewrite urls
on their web pages. The crux of the script is the following:

if(getimagesize($redirect_to) == 0)
header("Location: $redirect_to");
else
include("$redirect_to");

Which uses getimagesize() to determine if the link specified in
$redirect_to points to an image (i.e img src) in which case it should
include the image in the page, or if the link points to another
website (i.e. a href), in which case I use the header method to
redirect to that page when the link alias is called.

So far this script has been working great, but I recently had a
customer run it under php4.2.2 and the getimagesize is taking approx.
160 seconds to execute.

Can anyone think of a different way to tell if a remote file is an
image? And also, has anyone else experienced this kind of behavior
with getimagesize when running php4.2.2?

Assistance is really appreciated.

Thanks,

Esoos

Jul 17 '05 #2
Please learn to post correctly...

[ http://tk.digiserv.net/tofu.txt ]
On Fri, 02 Apr 2004 12:03:30 +0300, Dennis Biletsky wrote:
Try to look on extension of $redirect_to. If it will image extension (.jpg,
.gif, .png, etc), so that is picture, otherwise other file type

LOL!

This is m$ "thinking".. and most certainly _not_ how it _really_ works.

fopen() / fread() / fclose() would work a _lot_ more effectively than this
proposed solution.. although I cna only guess that this is pretty much
what getimagesize() does too.. although it may be faster to jkust read the
header compared to whatever other tests getimagesize() performs too.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #3

"ebobnar" <eb*******@yahoo.com> ???????/???????? ? ???????? ?????????:
news:c9**************************@posting.google.c om...
I've written a link aliasing script that allows users to rewrite urls
on their web pages. The crux of the script is the following:

if(getimagesize($redirect_to) == 0)
header("Location: $redirect_to");
else
include("$redirect_to");

Which uses getimagesize() to determine if the link specified in
$redirect_to points to an image (i.e img src) in which case it should
include the image in the page, or if the link points to another
website (i.e. a href), in which case I use the header method to
redirect to that page when the link alias is called.

So far this script has been working great, but I recently had a
customer run it under php4.2.2 and the getimagesize is taking approx.
160 seconds to execute.

Can anyone think of a different way to tell if a remote file is an
image? And also, has anyone else experienced this kind of behavior
with getimagesize when running php4.2.2?

Assistance is really appreciated.

Thanks,

Esoos


Perhaps, this function will work better.
http://www.php.net/manual/en/functio...-imagetype.php
Jul 17 '05 #4
On Fri, 02 Apr 2004 16:50:07 +0300, Dennis Biletsky wrote:
[ snip ]

Can anyone think of a different way to tell if a remote file is an
image? And also, has anyone else experienced this kind of behavior
with getimagesize when running php4.2.2?

Assistance is really appreciated.

Thanks,

Esoos


Perhaps, this function will work better.
http://www.php.net/manual/en/functio...-imagetype.php

If the image hasn't been optiomised (ie: had the thumbnail (exif image)
removed).

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #5
"Ian.H" <ia*@WINDOZEdigiserv.net> ???????/???????? ? ???????? ?????????:
news:pa****************************@bubbleboy.digi serv.net...
Please learn to post correctly...

[ http://tk.digiserv.net/tofu.txt ]
On Fri, 02 Apr 2004 12:03:30 +0300, Dennis Biletsky wrote:
Try to look on extension of $redirect_to. If it will image extension (.jpg, .gif, .png, etc), so that is picture, otherwise other file type

LOL!

This is m$ "thinking".. and most certainly _not_ how it _really_ works.

fopen() / fread() / fclose() would work a _lot_ more effectively than this
proposed solution.. although I cna only guess that this is pretty much
what getimagesize() does too.. although it may be faster to jkust read the
header compared to whatever other tests getimagesize() performs too.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/


fread() is way that works in any case, but you will need analyze file
signature to identify what type of that file and you should now how to do
that. You should know file structure of various pictures' formats.
Jul 17 '05 #6
On Fri, 02 Apr 2004 17:45:05 +0300, Dennis Biletsky wrote:
"Ian.H" <ia*@WINDOZEdigiserv.net> ???????/???????? ? ???????? ?????????:
news:pa****************************@bubbleboy.digi serv.net...
Please learn to post correctly...

[ http://tk.digiserv.net/tofu.txt ]
On Fri, 02 Apr 2004 12:03:30 +0300, Dennis Biletsky wrote:
> Try to look on extension of $redirect_to. If it will image extension (.jpg, > .gif, .png, etc), so that is picture, otherwise other file type

LOL!

This is m$ "thinking".. and most certainly _not_ how it _really_ works.

fopen() / fread() / fclose() would work a _lot_ more effectively than this
proposed solution.. although I cna only guess that this is pretty much
what getimagesize() does too.. although it may be faster to jkust read the
header compared to whatever other tests getimagesize() performs too.


fread() is way that works in any case, but you will need analyze file
signature to identify what type of that file and you should now how to do
that. You should know file structure of various pictures' formats.

Like.. the first 4 bytes of the file (in most cases)? Don't really need to
know much about the actual structure of the rest of the data.. just the
header.

$fp = @fopen('foo.jpg'm 'rb');
if ($fp) {
$fh = @fread($fp, 4);
@fclose($fp);
}

switch ($fh) {
'JPEG': /* File is a JPEG image */; break;
'GIF.': /* File is a GIF image */; break;
[...]
default: /* File == unknown / invalid type */
}

This example is _very_ crude and I haven't double checked to see exactly
how many bytes of the header would need to be read in (I think it
differs between 2 and 4.. but maybe wrong).. but should work just fine and
possible quicker than getimagesize().. although I haven't tested any of
this =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #7

"Ian.H" <ia*@WINDOZEdigiserv.net> ???????/???????? ? ???????? ?????????:
news:pa****************************@bubbleboy.digi serv.net...
On Fri, 02 Apr 2004 17:45:05 +0300, Dennis Biletsky wrote:
"Ian.H" <ia*@WINDOZEdigiserv.net> ???????/???????? ? ???????? ?????????:
news:pa****************************@bubbleboy.digi serv.net...
Please learn to post correctly...

[ http://tk.digiserv.net/tofu.txt ]
On Fri, 02 Apr 2004 12:03:30 +0300, Dennis Biletsky wrote:

> Try to look on extension of $redirect_to. If it will image extension

(.jpg,
> .gif, .png, etc), so that is picture, otherwise other file type
LOL!

This is m$ "thinking".. and most certainly _not_ how it _really_ works.

fopen() / fread() / fclose() would work a _lot_ more effectively than this proposed solution.. although I cna only guess that this is pretty much
what getimagesize() does too.. although it may be faster to jkust read the header compared to whatever other tests getimagesize() performs too.
fread() is way that works in any case, but you will need analyze file
signature to identify what type of that file and you should now how to

do that. You should know file structure of various pictures' formats.

Like.. the first 4 bytes of the file (in most cases)? Don't really need to
know much about the actual structure of the rest of the data.. just the
header.

$fp = @fopen('foo.jpg'm 'rb');
if ($fp) {
$fh = @fread($fp, 4);
@fclose($fp);
}

switch ($fh) {
'JPEG': /* File is a JPEG image */; break;
'GIF.': /* File is a GIF image */; break;
[...]
default: /* File == unknown / invalid type */
}

This example is _very_ crude and I haven't double checked to see exactly
how many bytes of the header would need to be read in (I think it
differs between 2 and 4.. but maybe wrong).. but should work just fine and
possible quicker than getimagesize().. although I haven't tested any of
this =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/


Yes, in common, first several bytes, but you should analyze it. By, the way,
JPEG some time has JFIF signature :). Anyway, I agree that your version will
work in any case.

Regards,
Dennis
Jul 17 '05 #8
ebobnar wrote:
Can anyone think of a different way to tell if a remote file is an
image? And also, has anyone else experienced this kind of behavior
with getimagesize when running php4.2.2?


If you're willing to trust the remote server to correctly report the
Content-Type, it'd be a lot more efficient than retrieving the whole thing
just to check it out.

Perhaps something along the general lines of:

$fh = fsockopen($host,80);
if ($fh)
{
fputs($fh,"HEAD $url HTTP/1.0\n\n");
$resp = fgets($fh,2048);
if (preg_match("/Content-Type:(.+)/",$resp,$match))
{
print "Content type is : " . $match[1];
}
fclose($fh);
}

Of course you'd have to parse the URL they've entered to get the hostname
and path correct, but that shouldn't be too hard.

Cheers

Dave P
--
David Precious
http://www.preshweb.co.uk/

Jul 17 '05 #9
On Fri, 02 Apr 2004 14:00:25 GMT, "Ian.H" <ia*@WINDOZEdigiserv.net> wrote:
On Fri, 02 Apr 2004 16:50:07 +0300, Dennis Biletsky wrote:
Can anyone think of a different way to tell if a remote file is an
image? And also, has anyone else experienced this kind of behavior
with getimagesize when running php4.2.2?


Perhaps, this function will work better.
http://www.php.net/manual/en/functio...-imagetype.php

If the image hasn't been optiomised (ie: had the thumbnail (exif image)
removed).


Actually, despite being a part of the EXIF extension, this function doesn't
actually have anything to do with EXIF. If you look at the source, it's just a
wrapper that calls back into php_getimagetype in standard/image.c, which does a
read of the first few bytes pretty much as you posted elsewhere in this thread.

The php_getimagetype C function is also used by the PHP function getimagesize.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #10
Thank you to everyone who replied to my question.

I haven't solved it yet. Apparently exif_imagetype only works on php
= php 4.3.0, and I am working with php 4.2.2. I tried the reading the

header for image identifiers, like so:

$fp = @fopen("$redirect_to", 'rb');
if ($fp) {
$fh = @fread($fp, 12);
@fclose($fp);
}

if (preg_match("/GIF87|GIF88|GIF89|JFIF|hsi1|MM|II|IIN1|PNG|BM|P1|P 4|P2|P5|P3|P6/i",
$fh)) {
include("$redirect_to");
} else {
header("Location: $redirect_to");
}

This improved things somewhat, execution time dropped from about 160
sec to about 80 sec, but still takes much too long to be practical.

I appreciate everyones feedback, and I am continuing to look for a way
to solve this problem.

Thanks,

Esoos
Jul 17 '05 #11
"David Precious" <pi******@preshweb.co.uk> wrote in message
news:10****************@iris.uk.clara.net...
If you're willing to trust the remote server to correctly report the
Content-Type, it'd be a lot more efficient than retrieving the whole thing
just to check it out.


getimagesize() at most reads 12 bytes from the stream, so I don't think file
size is an issue here. The problem, it appears, is that the PHP isn't timing
out quickly when the server doesn't respond or if the domain name is
invalid.

Reduce default_socket_time so that getimagesize fails quickly, instead of
160 seconds that this customer has set:

$dst = ini_set('default_socket_timeout', 5);
getimagesize($redirect_to);
ini_set('default_socket_timeout', $dst);
Jul 17 '05 #12
"ebobnar" <eb*******@yahoo.com> wrote in message
news:c9**************************@posting.google.c om...
I've written a link aliasing script that allows users to rewrite urls
on their web pages. The crux of the script is the following:

if(getimagesize($redirect_to) == 0)
header("Location: $redirect_to");
else
include("$redirect_to");


BTW: You've actually written a script that allows users to take over your
server. Use readfile(), not include(), to print the content of a remote
page.
Jul 17 '05 #13
Thanks again to everyone who responded.

I have tried everyones suggestions, but still have not been completely
sucessful.
If you're willing to trust the remote server to correctly report the
Content-Type, it'd be a lot more efficient than retrieving the whole thingjust to check it out.
I wrote a function, similar to the one in this post:

function display_content_type($hostname, $file) {

$ip = gethostbyname($hostname);
$fp = fsockopen($ip, 80, &$errno, &$errstr);

if ($fp)
{
fputs($fp, "GET " . $file . " HTTP/1.0\r\n" .
"Host: $hostname\r\n" .
"Accept: text/html\r\n" .
"Accept: text/plain\r\n\r\n" );

$src = '';
while (!feof ($fp)) {
$src .= fgets($fp, 4096);
if (preg_match("/Content-Type:(.+)/", $src, $match))
{
print "Content type is : " . $match[1];
fclose ($fp);
exit;
}
}
}
}

This works well with when called with arguments like:

display_content_type("http://www.yahoo.com", "/index.html");

but fails completely when called like:

display_content_type("http://www.qksrv.net",
"/click-1469122-7154067");

Also, runs extremely slow under php 4.2.2.
getimagesize() at most reads 12 bytes from the stream, so I don't think filesize is an issue here. The problem, it appears, is that the PHP isn't timingout quickly when the server doesn't respond or if the domain name is
invalid.


I profiled the code very closely, and was able to tell that it was
precisely the getimagesize() function that was taking 180 seconds to
complete. I don't think that the program is failing to retrieve the
images, because if I wait 180 seconds for getimagesize() to complete,
then the images are downloaded and displayed normally.

I also tried just using:

header("Location: $redirect_to");

for both images and web pages, and it works pretty well, but fails
when the link to the image is redirected to another site (I imagine
for server load balancing, but I digress...)

I am thinking that the problem is php 4.2.2. It seems that any file
manipulation functions that access remote files run really slow. I'm
not sure if this is the case but that's my guess.

The question now stands: if I just use the header("Location:
$redirect_to");
statement, is there a way to determine if the link is being redirected
(that would run quickly on php 4.2.2?) Does php have a function that
does this?

I would like to do something like the following pseudocode:

header("Location: $redirect_to");
if ($status_code == "3.x.x") {
header("Location: $redirect_to_whateverpage_is specified in the
redirect header");
}
also, thanks to Chung Leong for the security heads up.
Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rene' Nielsen | last post by:
Context: Running Windows 2003 Server on an intranet. A web is configured with an anonymous access account that is a domain account that has been granted the desired access to a file on another...
6
by: Philip Lee | last post by:
Dear all, How can I access files in remote file server through my ASP.NET application only, but deny all other users? I have added <identity impersonate="true" userName="test"...
2
by: Andrea Moro | last post by:
I need to get a remote file via http protocol, get it in some variable and use with some unzipping tool to get the content. So the problem I've is two. First how to read a remote file. I found...
3
by: Vjay77 | last post by:
I posted this question, but I pressed 'post' and it disappeared. So once again: Problem: I need to go to lets say www.site.com/page.html Imagine that this html code is 6 mb long. I need to...
2
by: Adrian | last post by:
Hi I have some local pages (on PC harddisk) that act as a user interface and a set of JS files that controls it all. I now want to be able to access a remote web server to read a txt file and use...
2
by: awdomain | last post by:
Hi, I want to know how to include remote file in an asp file? Anybody woh know about it.Please let me tell. Thanks http://www.newbestweb.com/
4
by: Ryderch | last post by:
Greetings folks! I need to open a file located on a remote machine running DOS (ouch!) from a client running Vista. The network connection is based on NetBEUI. Test1 Code running on the...
2
by: Martin Zugec | last post by:
Heya, googling\liveing around, but I found no results - any ideas how to retrieve timestamp of remote file (http\ftp)??? Idea is to build auto update utility that could (based on timestamps)...
2
by: houghi | last post by:
I want to save a remote file. The Remote file is created by google maps and has the name maps.klm What I intend to do is the following 1) Make a route, like http://tinyurl.com/5oknap on the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.