Phpgtimagesize the length and width of the uploaded image detection code

Source: Internet
Author: User
Getimagesize-get image size getimagesize-get Image size
Description
Array getimagesize (string $ filename [, array & $ imageinfo])
The getimagesize () function will determine any GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, the size of the XBM or WBMP image file, the size of the returned image, the file type, and a height/width text string that can be used in a common HTML file.
If you cannot access the image specified by filename or the image is not valid, getimagesize () returns FALSE and generates an E_WARNING error.
Note: Support for JPC, JP2, JPX, JB2, XBM, and WBMP is available from PHP 4.3.2. SWC support is available from PHP 4.3.0. The support for TIFF is added in PHP 4.2.0.
Note: JPEG 2000 is added in PHP 4.3.2. Note that JPC and JP2 can have different color depth components. In this case, the value of "bits" is the highest bit depth encountered. In addition, the JP2 file may contain multiple JPEG 2000 code streams. in this case, getimagesize () returns the value of the first code stream encountered at the top layer of the file.
Note: This function does not require the GD Image Library.
Returns an array with four units. Index 0 contains the pixel value of the image width, and index 1 contains the pixel value of the image height. Index 2 is the mark of the image type: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF (intel byte order), 8 = TIFF (motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. These tags correspond to the newly added IMAGETYPE constant in PHP 4.3.0. Index 3 is a text string with the content "height =" yyy "width =" xxx "and can be directly used for IMG marking.
Example #1 getimagesize (file)

The code is as follows:


List ($ width, $ height, $ type, $ attr) = getimagesize ("img/flag.jpg ");
Echo "";
?>


The URL can be added in PHP 4.0.5.
Example #2 getimagesize (URL)

The code is as follows:


$ Size = getimagesize ("http://www.jb51.net/images/logo.gif ");
// If the file name has space in it, encode it properly
$ Size = getimagesize ("http://www.example.com/gifs/lo go.gifs ");
?>


For JPG images, two more indexes are returned: channels and bits. Channels has a value of 3 for the RGB image and a value of 4 for the CMYK image. Bits is the number of bits in each color.
Since PHP 4.3.0, bits and channels also exist for other image types. However, these values may confuse people. For example, GIF always uses three channels for each pixel, but for animated GIF, the bits of each pixel cannot be calculated from the global color table.
Some formats may not contain images or contain multiple images. In this case, getimagesize () may not be used to accurately determine the image size. In this case, getimagesize () returns zero as the width and height.
Since PHP 4.3.0, getimagesize () also returns the additional parameter mime, which conforms to the MIME type of the image. This information can be used to send the correct information in the HTTP Content-type header:
Example #3 getimagesize () and MIME type

The code is as follows:


$ Size = getimagesize ($ filename );
$ Fp = fopen ($ filename, "rb ");
If ($ size & $ fp ){
Header ("Content-type: {$ size ['Mime ']}");
Fpassthru ($ fp );
Exit;
} Else {
// Error
}
?>


The optional imageinfo parameter allows you to extract extended information from an image file. Currently, this will return different jpg app IDs as an associated array. Some programs use these APP identifiers to embed text information in the image. A very common one is the APP13 identifier embedded in IPTC» http://www.iptc.org/information. You can use the iptcparse () function to parse the binary APP13 identifier into readable information.
Example #4 getimagesize () returns IPTC

The code is as follows:


$ Size = getimagesize ("testimg.jpg", & $ info );
If (isset ($ info ["APP13"]) {
$ Iptc = iptcparse ($ info ["APP13"]);
Var_dump ($ iptc );
}
?>



Php has an image GD Library getimagesize () function.
A function is used to obtain the basic information of an image.
Getimagesize ()
$ Img = getimagesize ('image source ');
The width is = $ img [0];
Height = $ img [1];
Format: = $ img [2];
It can be simpler if you want

The code is as follows:


$ Picpath = 'http: // www.jb51.net/images/logo.gif ';
$ Array = getimagesize ($ picpath );
Print_r ($ array );
Echo 'image width is '. $ array [0];
Echo 'image height is '. $ array [1];
Echo 'image format is '. $ array [2];


// Another code that uses getimagesize to display thumbnails

The code is as follows:


Function show_thumbnail ($ file)
{
$ Max = 200 // Max. thumbnail width and height
$ Size = getimagesize ($ file );
If ($ size [0] <= $ max & $ size [1] <= $ max)
{
$ Ret = '';
}
Else
{
$ K = ($ size [0] >=$ size [1])? $ Size [0]/$ max: $ size [1]/$ max;
$ Ret = '$ ret. = $ file. '','', 'width ='. $ size [0];
$ Ret. = ', height ='. $ size [1]. '')"> ';
$ Ret. = '';
}
Return $ ret;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.