For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > July 2004 > How to find size (in bytes) of an image using the GD image resource?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to find size (in bytes) of an image using the GD image resource?
Norman Peelman

2004-07-30, 8:55 am

I would like to be able to determine the size (in bytes) of a file and
re-adjust before output. Is this possible?

Norm

--
Avatar hosting at www.easyavatar.com


Žed Eye Media - Richard Grove

2004-07-30, 3:55 pm


"Norman Peelman" <npeelman@cfl.rr.com> wrote in message
news:iqoOc.413$Hu2.378@tornado.tampabay.rr.com...
> I would like to be able to determine the size (in bytes) of a file and
> re-adjust before output. Is this possible?
>
> Norm
>
> --
> Avatar hosting at www.easyavatar.com
>
>


No need to use GD, just use this:


function my_filesize($file) {
if(!is_file("./".$file)) return "0 KB";
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte

$size = filesize($file);

if($size < $kb) {
return $size." B";
}
else if($size < $mb) {
return round($size/$kb,2)." KB";
}
else if($size < $gb) {
return round($size/$mb,2)." MB";
}
else if($size < $tb) {
return round($size/$gb,2)." GB";
}
else {
return round($size/$tb,2)." TB";
}
}


Regards
Richard Grove


Andy Hassall

2004-07-30, 3:55 pm

On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelman@cfl.rr.com> wrote:

> I would like to be able to determine the size (in bytes) of a file and
>re-adjust before output. Is this possible?


If you mean determine the size of the file it _would_ create, you can't
without encoding it to somewhere, either memory or a temporary file.

You could capture output (ob_start()) etc., use imagepng/jpeg/whatever, and
save the output buffer to a variable and use strlen to work out the size.

Or save to a file anyway, and use filesize(), and regenerate it if you don't
like the size.

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Norman Peelman

2004-07-30, 8:55 pm

"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:ip3lg0lgo033sd54g7dnqv9crq857e2oo8@
4ax.com...
> On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelman@cfl.rr.com>

wrote:
>
>
> If you mean determine the size of the file it _would_ create, you can't
> without encoding it to somewhere, either memory or a temporary file.
>
> You could capture output (ob_start()) etc., use imagepng/jpeg/whatever,

and
> save the output buffer to a variable and use strlen to work out the size.
>
> Or save to a file anyway, and use filesize(), and regenerate it if you

don't
> like the size.
>
> --
> Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
> http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


Thanks Andy, you understood what I meant... my mistake I should have said
'image' instead of 'file'. I'll look into the ob_* functions but wich do
you think would be faster (or easier)?

Norm
--
Avatar hosting at www.easyavatar.com


Andy Hassall

2004-07-30, 8:55 pm

On Fri, 30 Jul 2004 22:04:36 GMT, "Norman Peelman" <npeelman@cfl.rr.com> wrote:

>"Andy Hassall" <andy@andyh.co.uk> wrote in message
> news:ip3lg0lgo033sd54g7dnqv9crq857e2oo8@
4ax.com...
>wrote:
>
>Thanks Andy, you understood what I meant... my mistake I should have said
>'image' instead of 'file'. I'll look into the ob_* functions but wich do
>you think would be faster (or easier)?


I would guess ob_* would be faster, writing to a file would be easier,
although I wouldn't have thought there a massive difference between the two.

If you're writing out to a file in the end anyway, and more often than not you
get reasonable settings the first time, I'd probably tend towards writing
straight to a file.

If you're outputting to the browser rather than saving to disk, I'd tend
towards the ob_* solution.

As ever, the only answer to what's best is It Depends (tm).

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Norman Peelman

2004-07-30, 8:55 pm

"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:ip3lg0lgo033sd54g7dnqv9crq857e2oo8@
4ax.com...
> On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelman@cfl.rr.com>

wrote:
>
>
> If you mean determine the size of the file it _would_ create, you can't
> without encoding it to somewhere, either memory or a temporary file.
>
> You could capture output (ob_start()) etc., use imagepng/jpeg/whatever,

and
> save the output buffer to a variable and use strlen to work out the size.
>
> Or save to a file anyway, and use filesize(), and regenerate it if you

don't
> like the size.
>
> --
> Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
> http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


Andy,

Got it figured out with ob_start(), ob_get_length(), and ob_end_flush().
Thanks for the help!

Norm
--
Avatar hosting at www.easyavatar.com


Norman Peelman

2004-07-31, 8:55 am

"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:e2ilg0ln2jcumviea4q8kcj0mj9thjagnl@
4ax.com...
> On Fri, 30 Jul 2004 22:04:36 GMT, "Norman Peelman" <npeelman@cfl.rr.com>

wrote:
>
<npeelman@cfl.rr.com>[color=darkred]
and[color=darkred]
can't[color=darkred]
imagepng/jpeg/whatever,[color=darkred]
size.[color=darkred]
>
> I would guess ob_* would be faster, writing to a file would be easier,
> although I wouldn't have thought there a massive difference between the

two.
>
> If you're writing out to a file in the end anyway, and more often than

not you
> get reasonable settings the first time, I'd probably tend towards writing
> straight to a file.
>
> If you're outputting to the browser rather than saving to disk, I'd tend
> towards the ob_* solution.
>
> As ever, the only answer to what's best is It Depends (tm).
>
> --
> Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
> http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


I'm coming from a database (blob field) and going straight to browser
(client) output and I didn't want to have to go to the filesystem. The ob_*
funcs worked like a charm.

--
Avatar hosting at www.easyavatar.com


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com