Professional Web Applications Themes

How to find size (in bytes) of an image using the GD image resource? - PHP Development

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 [url]www.easyavatar.com[/url]...

  1. #1

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

    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 [url]www.easyavatar.com[/url]


    Norman Peelman Guest

  2. #2

    Default Re: How to find size (in bytes) of an image using the GD image resource?


    "Norman Peelman" <npeelmancfl.rr.com> wrote in message
    news:iqoOc.413$Hu2.378tornado.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 [url]www.easyavatar.com[/url]
    >
    >
    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


    Žed Eye Media - Richard Grove Guest

  3. #3

    Default Re: How to find size (in bytes) of an image using the GD image resource?

    On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelmancfl.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 <andyandyh.co.uk> / Space: disk usage ysis tool
    [url]http://www.andyh.co.uk[/url] / [url]http://www.andyhsoftware.co.uk/space[/url]
    Andy Hassall Guest

  4. #4

    Default Re: How to find size (in bytes) of an image using the GD image resource?

    "Andy Hassall" <andyandyh.co.uk> wrote in message
    news:ip3lg0lgo033sd54g7dnqv9crq857e2oo84ax.com...
    > On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelmancfl.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 <andyandyh.co.uk> / Space: disk usage ysis tool
    > [url]http://www.andyh.co.uk[/url] / [url]http://www.andyhsoftware.co.uk/space[/url]
    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 [url]www.easyavatar.com[/url]


    Norman Peelman Guest

  5. #5

    Default Re: How to find size (in bytes) of an image using the GD image resource?

    On Fri, 30 Jul 2004 22:04:36 GMT, "Norman Peelman" <npeelmancfl.rr.com> wrote:
    >"Andy Hassall" <andyandyh.co.uk> wrote in message
    >news:ip3lg0lgo033sd54g7dnqv9crq857e2oo84ax.com.. .
    >> On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelmancfl.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.
    >
    >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 <andyandyh.co.uk> / Space: disk usage ysis tool
    [url]http://www.andyh.co.uk[/url] / [url]http://www.andyhsoftware.co.uk/space[/url]
    Andy Hassall Guest

  6. #6

    Default Re: How to find size (in bytes) of an image using the GD image resource?

    "Andy Hassall" <andyandyh.co.uk> wrote in message
    news:ip3lg0lgo033sd54g7dnqv9crq857e2oo84ax.com...
    > On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelmancfl.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 <andyandyh.co.uk> / Space: disk usage ysis tool
    > [url]http://www.andyh.co.uk[/url] / [url]http://www.andyhsoftware.co.uk/space[/url]
    Andy,

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

    Norm
    --
    Avatar hosting at [url]www.easyavatar.com[/url]


    Norman Peelman Guest

  7. #7

    Default Re: How to find size (in bytes) of an image using the GD image resource?

    "Andy Hassall" <andyandyh.co.uk> wrote in message
    news:e2ilg0ln2jviea4q8kcj0mj9thjagnl4ax.com...
    > On Fri, 30 Jul 2004 22:04:36 GMT, "Norman Peelman" <npeelmancfl.rr.com>
    wrote:
    >
    > >"Andy Hassall" <andyandyh.co.uk> wrote in message
    > >news:ip3lg0lgo033sd54g7dnqv9crq857e2oo84ax.com.. .
    > >> On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman"
    <npeelmancfl.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.
    > >
    > >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 <andyandyh.co.uk> / Space: disk usage ysis tool
    > [url]http://www.andyh.co.uk[/url] / [url]http://www.andyhsoftware.co.uk/space[/url]
    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 [url]www.easyavatar.com[/url]


    Norman Peelman Guest

Similar Threads

  1. How to find Image Region and add image in it?
    By yiyun0918@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 7
    Last Post: August 14th, 05:33 AM
  2. Load JPG into Flash and find out the size of the image
    By vorpal in forum Macromedia Flash Actionscript
    Replies: 3
    Last Post: January 20th, 11:54 AM
  3. getting remote image size with Image::Size & LWP
    By Matthew Galaher in forum PERL Beginners
    Replies: 1
    Last Post: November 13th, 11:27 PM
  4. getting remote image sizes with image::size lwp
    By Matthew Galaher in forum PERL Beginners
    Replies: 1
    Last Post: November 13th, 03:06 PM
  5. Dragging Image onto canvas changes image size
    By Brian Ingle in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 1
    Last Post: August 26th, 03:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139