PHP getimagesize Upload image length and width detection code _php tips

Source: Internet
Author: User
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,XBM or WBMP The size of the image file and returns the size of the image and the file type and a height/width text string that can be used in the tag in the normal HTML file.
If the image specified by filename cannot be accessed or is not a valid image, GetImageSize () returns false and generates a e_warning level error.
Note: Support for JPC,JP2,JPX,JB2,XBM and WBMP is available from PHP 4.3.2. Support for SWC is available from PHP 4.3.0. The support for TIFF is added by PHP 4.2.0.
Note:jpeg 2000 support is added by 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 that is encountered. In addition, the JP2 file may contain more than one JPEG 2000 code flow, in which case getimagesize () returns the value of the first code flow encountered in the top level of the file.
Note: The GD Image Library is not required for this function.
Returns an array of four cells. 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 tag 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 constants of PHP 4.3.0. Index 3 is a text string with the contents "height=" yyy "width=" xxx, which can be used directly for the IMG tag.
Example #1 getimagesize (file)
Copy Code code as follows:

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

URL support is added by PHP 4.0.5.
Example #2 getimagesize (URL)
Copy Code code as follows:

<?php
$size = getimagesize ("Yun_qi_img/logo.gif");
If the file name has space in it, encode it properly
$size = getimagesize ("Http://www.example.com/gifs/lo go.gif");
?>

For JPG images, two more indexes are returned: channels and bits. Channels for RGB images with a value of 3, the value for CMYK images is 4. BITS is the number of digits per color.
Bits and channels also exist for other image types since PHP 4.3.0. But these values can confuse people. For example, GIF always uses 3 channel for each pixel, but for an animated GIF, the number of bits per pixel cannot be computed 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 measure the size of the image. At this point getimagesize () returns 0 as the width and height.
from PHP 4.3.0, getimagesize () also returns an additional parameter mime that matches the MIME type of the image. This information can be used to send the correct information in the HTTP Content-type header information:
Example #3 getimagesize () and MIME types
Copy Code code as follows:

<?php
$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 some extended information to be extracted from the image file. Currently, this will return a different JPG APP identity with an associative array. Some programs use these APP identities to embed textual information in an image. A very common is the iptc»http://www.iptc.org/information embedded in the APP13 identity. You can use the Iptcparse () function to resolve binary APP13 identities to readable information.
Example #4 getimagesize () return to IPTC
Copy Code code as follows:

<?php
$size = getimagesize ("Testimg.jpg", & $info);
if (Isset ($info ["APP13"])) {
$IPTC = Iptcparse ($info ["APP13"]);
Var_dump ($IPTC);
}
?>


PHP has a picture GD library getimagesize () function.
One function is to get the basic information of a picture.
GetImageSize ()
$img =getimagesize (' picture source ');
width = $img [0];
Height for = $img [1];
The format is = $img [2];
If you want simple words can be simpler
Copy Code code as follows:

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

Another code that uses getimagesize to display thumbnails
Copy Code code as follows:

function Show_thumbnail ($file)
{
$max =//max. Thumbnail Widt H 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 = ' <a href= ' JavaScript tutorial:; "onclick=" window.open (' image.php?img= ';
$ret. = $file. ', ', ' width= '. $size [0];
$ret. = ', height= '. $size [1]. " > ';
$ret. = ' </a>";
}
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.