PHP getimagesize Upload image length and width detection code _php tutorial

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 as well as the file type and a height/width text string that can be used in markup in normal HTML files.
If the image specified by filename cannot be accessed or is not a valid image, GetImageSize () returns false and produces an 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 encountered. Additionally, the JP2 file may contain multiple JPEG 2000 code streams, in which case getimagesize () returns the value of the first code stream encountered in the top level of this file.
Note: This function does not require the GD image library.
Returns an array with 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 a marker 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 content "height=" yyy "width=" xxx ", which can be used directly with the IMG tag.
Example #1 getimagesize (file)
Copy CodeThe code is as follows:
List ($width, $height, $type, $attr) = getimagesize ("img/flag.jpg");
echo "";
?>

URL support is added by PHP 4.0.5.
Example #2 getimagesize (URL)
Copy CodeThe 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.gif");
?>

For JPG images, more than two 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 per color.
from PHP 4.3.0, bits and channels exist for other image types as well. But these values can confuse people. For example, GIF always uses 3 channel for each pixel, but for animated GIFs, the number of bits per pixel cannot be computed by 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 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 conforms to the MIME type of the image. This information can be used to send the correct information in the HTTP content-type header message:
Example #3 getimagesize () and MIME types
Copy CodeThe 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 some extended information from the image file. Currently, this will return a different JPG APP identifier with an associative array. Some programs use these APP identifiers to embed text information in an image. One very common is the iptc»http://www.iptc.org/information embedded in the APP13 identity. You can use the Iptcparse () function to parse a binary APP13 identity into readable information.
Example #4 getimagesize () return IPTC
Copy CodeThe code is as follows:
$size = getimagesize ("Testimg.jpg", & $info);
if (Isset ($info ["APP13"])) {
$IPTC = Iptcparse ($info ["APP13"]);
Var_dump ($IPTC);
}
?>


PHP has a picture of the GD library getimagesize () function.
There is a function to get basic information about a picture.
GetImageSize ()
$img =getimagesize (' picture source ');
width = $img [0];
Height = $img [1];
The format is = $img [2];
It would be easier if you wanted to be simple.
Copy CodeThe code is as follows:
$picpath = ' http://www.jb51.net/images/logo.gif ';
$array = getimagesize ($picpath);
Print_r ($array);
echo ' picture width is '. $array [0];
echo ' picture height is '. $array [1];
echo ' picture format is '. $array [2];

Another code that uses getimagesize to display thumbnails
Copy CodeThe code is as follows:
function Show_thumbnail ($file)
{
$max = +//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;
}

http://www.bkjia.com/PHPjc/321755.html www.bkjia.com true http://www.bkjia.com/PHPjc/321755.html techarticle getimagesize-Gets the image size description of the Height/width text string in the array getimagesize (string $filename [, array lt;img tag. If you cannot access the image specified by the filename ...

  • 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.