How to get image size in kb in php?

[PHP 4, PHP 5, PHP 7, PHP 8]

getimagesizeGet the size of an image

Description

getimagesize[string $filename, array &$image_info = null]: array|false

getimagesize[] can also return some more information in image_info parameter.

Caution

This function expects filename to be a valid image file. If a non-image file is supplied, it may be incorrectly detected as an image and the function will return successfully, but the array may contain nonsensical values.

Do not use getimagesize[] to check that a given file is a valid image. Use a purpose-built solution such as the Fileinfo extension instead.

Note: Note that JPC and JP2 are capable of having components with different bit depths. In this case, the value for "bits" is the highest bit depth encountered. Also, JP2 files may contain multiple JPEG 2000 codestreams. In this case, getimagesize[] returns the values for the first codestream it encounters in the root of the file.

Note: The information about icons are retrieved from the icon with the highest bitrate.

Note: GIF images consist of one or more frames, where each frame may only occupy part of the image. The size of the image which is reported by getimagesize[] is the overall size [read from the logical screen descriptor].

Parameters

filename

This parameter specifies the file you wish to retrieve information about. It can reference a local file or [configuration permitting] a remote file using one of the supported streams.

image_info

This optional parameter allows you to extract some extended information from the image file. Currently, this will return the different JPG APP markers as an associative array. Some programs use these APP markers to embed text information in images. A very common one is to embed » IPTC information in the APP13 marker. You can use the iptcparse[] function to parse the binary APP13 marker into something readable.

Note:

The image_info only supports JFIF files.

Return Values

Returns an array with up to 7 elements. Not all image types will include the channels and bits elements.

Index 0 and 1 contains respectively the width and the height of the image.

Note:

Some formats may contain no image or may contain multiple images. In these cases, getimagesize[] might not be able to properly determine the image size. getimagesize[] will return zero for width and height in these cases.

Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.

Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.

mime is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type header:

Example #1 getimagesize[] and MIME types

kazuya

8 years ago

i made function img_resize[$path,$tmp_name,$new_name,$new_width]
this could be useful.

cloned at clonedmadman dot com

14 years ago

Well, I am making a script which will resize the image when uploaded, however, i am making a multi-uploader, so i came across with a problem: an efficient way of getting a pictures height and width and storing them in an array to resize later. This is what i came up with:

info at alex-lawrence dot com

14 years ago

Could be useful [didn´t know where to post it]:

function getImageErrors[ $filename, $type = "", $minWidth = 0, $minHeight = 0, $maxWidth = 0, $maxHeight = 0, $maxFileSize = 0 ]
{
    $errors = array[];
    if [ file_exists[ $filename ] ]
    {
        $ending = substr[ $filename, strpos[ $filename, "." ] ];
        if [ is_array[ $type ] ]
        {
            $isTypeOf = false;
            foreach[ $type as $eachtype ]
            {
                if [ $ending == $eachtype ]
                {
                    $isTypeOf = true;
                }
            }
            if [ ! $isTypeOf ]
            {
                $errors[ 'type' ] = $ending;
            }
        }
        elseif [ $type != "" ]
        {
            if [ $ending != $type ]
            {
                $errors[ 'type' ] = $ending;
            }
        }
        $size = getimagesize[ $filename ];
        if [ $size[ 0 ] < $minWidth ]
        {
            $errors[ 'minWidth' ] = $size[ 0 ];
        }
        if [ $size[ 1 ] < $minHeight ]
        {
            $errors[ 'minHeight' ] = $size[ 1 ];
        }
        if [ [ $maxWidth > $minWidth ] && [ $size[ 0 ] > $maxWidth ] ]
        {
            $errors[ 'maxWidth' ] = $size[ 0 ];
        }
        if [ [ $maxHeight > $minHeight ] && [ $size[ 1 ] > $maxHeight ] ]
        {
            $errors[ 'maxHeight' ] = $size[ 1 ];
        }
        if [ [ $maxFileSize > 0 ] && [ filesize[ $filename ] > $maxFileSize ] ]
        {
            $errors[ 'maxFileSize' ] = filesize[ $filename ];
        }
    }
    else
    {
        $errors[ 'filename' ] = "not existing";
    }
    return [ count[ $errors ] > 0 ? $errors : null ];
}

utilmind

11 years ago

Here is the function which determines whether the PNG image contains alpha or not:



The color type of PNG image is stored at byte offset 25. Possible values of that 25'th byte is:
* 0 - greyscale
* 2 - RGB
* 3 - RGB with palette
* 4 - greyscale + alpha
* 6 - RGB + alpha

Jesus Zamora

11 years ago

Returns a array with 4 elements.
The 0 index is the width of the image in pixels.
The 1 index is the height of the image in pixels.
The 2 index is a flag for the image type:

1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF[orden de bytes intel], 8 = TIFF[orden de bytes motorola], 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.

The 3 index contains ' height="yyy" width="xxx" '

ajreading at classixshop dot com

17 years ago

A simple piece of code i wrote to proportionally resize an image to a max height and width then display it

freecorvette at gmail dot com

4 years ago

For some images, using getimagesize[] without the second parameter will return the correct info, but when you add the second parameter it will return false. This is most likely a bug [and it has been reported as such], but meanwhile, if you encounter this problem, a workaround is to use exif_read_data[].

Coodiss at w3bbix dot net

17 years ago

Heres a easy way to scale images to the that they are in
*this is broken up so anyone can understand it :]

diablx at hotmail dot com

18 years ago

I'm sorry for they other scripts, but I made one mistake about the image resizing... here is a working script !




  

Chủ Đề