Home > Article > Backend Development > How to calculate image size function in php
getimagesize() function is used to obtain image size, type and other information.
imagesx() function is used to get the width of the image.
imagesy() function is used to get the height of the image.
The getimagesize() function is used to obtain the image size and related information. It returns an array if successful. If it fails, it returns FALSE and generates an E_WARNING level error message.
Syntax:
array getimagesize( string filename )
Example:
<?php $array = getimagesize("images/flower_1.jpg"); print_r($array); ?>
The browser displays as follows:
Array ( [0] => 350 [1] => 318 [2] => 2 [3] => width="350" height="318" [bits] => 8 [channels] => 3 [mime] => image/jpeg )
Return result description:
1. Index 0 gives the pixel value of the image width
2. Index 1 gives the pixel value of the image height
3. Index 2 gives the type of image, and returns a number, where 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
4, index 3 gives a string of width and height, which can be directly used for HTMLdc0870658837139040642baa5555a380 tag
5. Index bits gives the number of bits for each color of the image, binary format
6. Index channels gives the channel value of the image, the default for RGB images is 3
7 . Index mime gives the MIME information of the image. This information can be used to send the correct information in the HTTP Content-type header information, such as:
header("Content-type: image/jpeg") ;
imagesx() function is used to obtain the width of the image, the unit is pixels, and the return value is an integer.
Syntax:
int imagesx( resource image )
The parameters are image resources returned by functions such as imagecreatetruecolor(), imagecreatefromjpeg() and other functions.
imagesy() function is used to obtain the height of the image. The syntax and usage are the same as imagesx() .
Syntax:
int imagesy( resource image )
Example:
<?php $img = imagecreatefromjpeg("images/flower_1.jpg"); echo "图像宽度:",imagesx( $img ),"<br />"; echo "图像高度:",imagesy( $img ); ?>
Browser output:
Image width: 350
Image height: 318
Recommended tutorial: PHP video tutorial
The above is the detailed content of How to calculate image size function in php. For more information, please follow other related articles on the PHP Chinese website!