Home > Backend Development > PHP Problem > How to calculate image size function in php

How to calculate image size function in php

王林
Release: 2023-02-24 17:40:01
Original
3874 people have browsed it

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 )
Copy after login

Example:

<?php
$array = getimagesize("images/flower_1.jpg");
print_r($array);
?>
Copy after login

The browser displays as follows:

Array
(
    [0] => 350
    [1] => 318
    [2] => 2
    [3] => width="350" height="318"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)
Copy after login

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 HTML 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 )
Copy after login

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 )
Copy after login

Example:

<?php
$img = imagecreatefromjpeg("images/flower_1.jpg");
echo "图像宽度:",imagesx( $img ),"<br />";
echo "图像高度:",imagesy( $img );
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template