FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Checking if file is an image
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Checking if file is an image [message #170857] Sat, 04 December 2010 01:37 Go to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
I have a section that shows an uploaded image. I was doing this:

list($width, $height, $type, $attr) = @getimagesize("/path/to/
$image");

if (!$width) {
$image = "noimage.gif";
$width = "75";
$height = "95";
}

I understand that error suppression has a bit of a performance hit,
though, so I'm curious if either of these would be better:

if (is_readable("/path/to/$image"))
list($width, $height, $type, $attr) = @getimagesize("/path/to/
$image");

if (getimagesize("/path/to/$image"))
list($width, $height, $type, $attr) = @getimagesize("/path/to/
$image");


Either would still be followed with "if (!$width)...". I know that
neither are fool-proof because they don't check that someone didn't
rename an .xls or .exe to .jpg, but I can probably control that better
on the upload side than checking here on every page load. Here, I'm
more concerned with speed, and not showing any error messages for
faulty images.

TIA,

Jason
Re: Checking if file is an image [message #170859 is a reply to message #170857] Sat, 04 December 2010 02:06 Go to previous messageGo to next message
Magno is currently offline  Magno
Messages: 49
Registered: October 2010
Karma: 0
Member
On 12/03/2010 10:37 PM, jwcarlton wrote:
> I have a section that shows an uploaded image. I was doing this:
>
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");
>
> if (!$width) {
> $image = "noimage.gif";
> $width = "75";
> $height = "95";
> }
>
> I understand that error suppression has a bit of a performance hit,
> though, so I'm curious if either of these would be better:
>
> if (is_readable("/path/to/$image"))
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");
>
> if (getimagesize("/path/to/$image"))
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");
>
>
> Either would still be followed with "if (!$width)...". I know that
> neither are fool-proof because they don't check that someone didn't
> rename an .xls or .exe to .jpg, but I can probably control that better
> on the upload side than checking here on every page load. Here, I'm
> more concerned with speed, and not showing any error messages for
> faulty images.
>
> TIA,
>
> Jason

¿Why don’t you just better use ob_start(); and buffer any possible error
output?
If no error output, then render the image normally.
If there is error, let yourself know by any way and then exit();

To check if there is any error, I think that a single.-
if (strlen($ob = ob_get_clean())) would do.
Re: Checking if file is an image [message #170861 is a reply to message #170859] Sat, 04 December 2010 02:38 Go to previous messageGo to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
On Dec 3, 9:06 pm, Magno <marbar...@gmail.com> wrote:
> On 12/03/2010 10:37 PM, jwcarlton wrote:
>
>
>
>> I have a section that shows an uploaded image. I was doing this:
>
>> list($width, $height, $type, $attr) = @getimagesize("/path/to/
>> $image");
>
>> if (!$width) {
>>    $image = "noimage.gif";
>>    $width = "75";
>>    $height = "95";
>> }
>
>> I understand that error suppression has a bit of a performance hit,
>> though, so I'm curious if either of these would be better:
>
>> if (is_readable("/path/to/$image"))
>>    list($width, $height, $type, $attr) = @getimagesize("/path/to/
>> $image");
>
>> if (getimagesize("/path/to/$image"))
>>    list($width, $height, $type, $attr) = @getimagesize("/path/to/
>> $image");
>
>> Either would still be followed with "if (!$width)...". I know that
>> neither are fool-proof because they don't check that someone didn't
>> rename an .xls or .exe to .jpg, but I can probably control that better
>> on the upload side than checking here on every page load. Here, I'm
>> more concerned with speed, and not showing any error messages for
>> faulty images.
>
>> TIA,
>
>> Jason
>
> ¿Why don’t you just better use ob_start(); and buffer any possible error
> output?
> If no error output, then render the image normally.
> If there is error, let yourself know by any way and then exit();
>
> To check if there is any error, I think that a single.-
> if (strlen($ob = ob_get_clean())) would do.

That's smart, thanks.

J
Re: Checking if file is an image [message #170863 is a reply to message #170857] Sat, 04 December 2010 03:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/3/2010 8:37 PM, jwcarlton wrote:
> I have a section that shows an uploaded image. I was doing this:
>
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");
>
> if (!$width) {
> $image = "noimage.gif";
> $width = "75";
> $height = "95";
> }
>
> I understand that error suppression has a bit of a performance hit,
> though, so I'm curious if either of these would be better:
>
> if (is_readable("/path/to/$image"))
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");
>
> if (getimagesize("/path/to/$image"))
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");
>
>
> Either would still be followed with "if (!$width)...". I know that
> neither are fool-proof because they don't check that someone didn't
> rename an .xls or .exe to .jpg, but I can probably control that better
> on the upload side than checking here on every page load. Here, I'm
> more concerned with speed, and not showing any error messages for
> faulty images.
>
> TIA,
>
> Jason

You're prematurely optimizing (again). Unless you're getting 10K
hits/second on your server, you won't notice the difference.

Write your code in a way that is understandable and maintainable. Once
you get the site up (and ONLY then), see if you have a performance
problem. If you do (there's 99.99% chance you won't), find the cause of
the problem and fix it.

But right now you're spending a lot of time worrying about something
that almost assuredly won't be a problem.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Checking if file is an image [message #170869 is a reply to message #170859] Sat, 04 December 2010 11:39 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Magno)

> ¿Why don’t you just better use ob_start(); and buffer any possible error
> output?

Ugly.

> If no error output, then render the image normally.
> If there is error, let yourself know by any way and then exit();
>
> To check if there is any error, I think that a single.-
> if (strlen($ob = ob_get_clean())) would do.

You could also use your own error handler and handle (almost) all PHP
errors and warnings as exceptions. But this seems to be a bit overkill
in this case.

Micha
Re: Checking if file is an image [message #170870 is a reply to message #170857] Sat, 04 December 2010 11:39 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(jwcarlton)

> I have a section that shows an uploaded image. I was doing this:
>
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");
>
> if (!$width) {
> $image = "noimage.gif";
> $width = "75";
> $height = "95";
> }
>
> I understand that error suppression has a bit of a performance hit,

Not really, it's just ugly.

> though, so I'm curious if either of these would be better:
>
> if (is_readable("/path/to/$image"))
> list($width, $height, $type, $attr) = @getimagesize("/path/to/
> $image");

if (is_readable("/path/to/$image"))
list($width, $height, $type, $attr) = getimagesize("/path/to/
$image");

> Either would still be followed with "if (!$width)...". I know that
> neither are fool-proof because they don't check that someone didn't
> rename an .xls or .exe to .jpg

They do, because non-image files won't have valid 'width' and 'height'
values.

> but I can probably control that better
> on the upload side than checking here on every page load.

The check that an uploaded file is a valid image would still have to be
done with getimagesize(), because you can't rely on filename and content
type.

> Here, I'm
> more concerned with speed

Do you have a real performance issue? Most likely not.

> and not showing any error messages for
> faulty images.

Simply disable errors on the production system (not on the development
system, though) and check the return values of getimagesize().

Micha
Re: Checking if file is an image [message #170877 is a reply to message #170869] Sat, 04 December 2010 15:10 Go to previous messageGo to next message
Magno is currently offline  Magno
Messages: 49
Registered: October 2010
Karma: 0
Member
On 12/04/2010 08:39 AM, Michael Fesser wrote:
> .oO(Magno)
>
>> ¿Why don’t you just better use ob_start(); and buffer any possible error
>> output?
>
> Ugly.

Nonsense.

>> If no error output, then render the image normally.
>> If there is error, let yourself know by any way and then exit();
>>
>> To check if there is any error, I think that a single.-
>> if (strlen($ob = ob_get_clean())) would do.
>
> You could also use your own error handler and handle (almost) all PHP
> errors and warnings as exceptions. But this seems to be a bit overkill
> in this case.

A mess.

> Micha
Re: Checking if file is an image [message #170882 is a reply to message #170870] Sat, 04 December 2010 19:19 Go to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
>
> Do you have a real performance issue? Most likely not.
>
>
Indeed. I have fou,d my performance issues were in fact never PHP.

Its fast enough for most purposes.

The only things I have noted are that scalin huge iamges takes a real
amount of CPU. But that's the GD library.

And that shoving lots of data down a slow onnection is pants.

An that ill formed SQL queries without decent indexing is also pants.

And finally that IE6 is utter pants at searching a arge DOM for element
names and IDS.

There's a lot about PHP that *is* pants, but speed is not one of the issues.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Efficiency of a lot of variables
Next Topic: web solutions for global presence
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 05:01:50 GMT 2024

Total time taken to generate the page: 0.03238 seconds