PHP getimagesize detects the length and width of uploaded images

  • 2020-03-31 20:46:19
  • OfStack

Getimagesize - gets the imagesize
instructions
Array getimagesize (string $filename [, array &$imageinfo])
The getimagesize() function will determine the size of any GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, XBM, or WBMP image file and return the size of the image as well as the file type and one that can be used in normal HTML files < IMG> Height /width text string in the tag.
If the image specified by filename cannot be accessed or is not a valid image, getimagesize() returns FALSE and generates an E_WARNING error.
Note: support for JPC, JP2, JPX, JB2, XBM, and WBMP is available since PHP 4.3.2. Support for SWC has been available since PHP 4.3.0. Support for TIFF was added in PHP 4.2.0.
Note: JPEG 2000 support was added to 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, a 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 the file.
Note: this function does not require the GD image library.
Returns an array with four cells. Index 0 contains pixel values for image width and index 1 contains pixel values for image height. Index 2 is an image type marker: 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 new IMAGETYPE constant in PHP 4.3.0. The index 3 is a text string with the content "height="yyy" width=" XXX" ", which can be used directly on the IMG tag.
Example #1 getimagesize (file)
 
<?php 
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg"); 
echo "<img src="img/flag.jpg" $attr>"; 
?> 

URL support was added in PHP 4.0.5.
Example #2 getimagesize (URL)
 
<?php 
$size = getimagesize("//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, two more indexes are returned: channels and bits. Channels has a value of 3 for RGB images and 4 for CMYK images. Bits are bits of each color.
Since PHP 4.3.0, bits and channels exist for other image types as well. But these values can be confusing. For example, gifs always use three channels per pixel, but for animated gifs the number of bits per pixel cannot be calculated from the global color table.
Some formats may not contain images or may contain multiple images. In this case, getimagesize() may not be used to determine the exact size of the image. At this point getimagesize() returns zero for the width and height.
Starting with 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:
Example #3 getimagesize() and MIME types
 
<?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 extension information to be extracted from the image file. For now, this will return different JPG APP identifiers as an associative array. Some programs use these APP identifiers to embed text information in images. A very common one is the IPTC » embedded in the APP13 identity. Information at http://www.iptc.org/. The iptcparse() function can be used to parse the binary APP13 identifier into readable information.
Example #4 getimagesize() returns IPTC
 
<?php 
$size = getimagesize("testimg.jpg", &$info); 
if (isset($info["APP13"])) { 
$iptc = iptcparse($info["APP13"]); 
var_dump($iptc); 
} 
?> 


PHP has an image GD library called getimagesize ().
There is a function to get the basic information of the picture.
Getimagesize ()
$img=getimagesize(' image source ');
Width is = $img [0];
Height is = $img [1];
Format for = $img [2];
It could be simpler if you want
 
$picpath = '//www.jb51.net/images/logo.gif'; 
$array = getimagesize($picpath); 
print_r( $array ); 
echo ' The width of the picture is '.$array[0]; 
echo ' The height of the picture is '.$array[1]; 
echo ' The picture format is '.$array[2]; 

// another code that displays thumbnails using getimagesize
 
function show_thumbnail($file) 
{ 
$max = 200 // Max. thumbnail width and height 
$size = getimagesize($file); 
if ( $size[0] <= $max && $size[1] <= $max ) 
{ 
$ret = '<img src="'.$file.'" '.$size[3].' border="0">'; 
} 
else 
{ 
$k = ( $size[0] >= $size[1] ) ? $size[0] / $max : $size[1] / $max; 
$ret = '<a href="javascript The tutorial :;" onClick="window.open('image.php?img='; 
$ret .= $file.'','','width='.$size[0]; 
$ret .= ',height='.$size[1].'')">'; 
$ret .= '<img src="'.$file.'" width="'.floor($size[0]/$k).'" height="'.floor($size[1]/$k).'" border="0" alt="View full-size image"></a>'; 
} 
return $ret; 
} 

Related articles: