Php obtains attributes such as the image length, width, and size.

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. When we use PHP to develop a user center, the user's portrait upload function will certainly be available, or when developing other interaction functions to upload images, we need to know whether the images uploaded by users meet our requirements. Here we will share with you a simple way to obtain image attributes.

Welcome to the Linux community forum and interact with 2 million technical staff> when we are using PHP to develop a user center, users will certainly upload their portraits, or when developing other interaction functions to upload images, we need to know whether the images uploaded by users meet our requirements. Here we will share with you a simple way to obtain image attributes.

Welcome to the Linux community forum and interact with 2 million technicians>

When we use PHP to develop a user center, the user's portrait upload function will certainly be available, or when other interaction functions need to upload images, we all need to know whether the images uploaded by users meet our requirements. Here we will share with you a simple PHP user-defined function for retrieving image attributes, hoping to help you, this function was also written by a netizen on the Internet. It has been tested by UncleToo and has good results and is easy to use.

Code:

  

/**

* Obtain image information

*

* @ Param string $ image file name

*

* @ Return mixed

*/

Function getImageInfo ($ image ){

$ ImageInfo = getimagesize ($ image );

If ($ imageInfo! = FALSE ){

$ ImageType = strtolower (substr (image_type_to_extension ($ imageInfo [2]), 1 ));

$ ImageSize = filesize ($ image );

$ Info = array (

"Width" => $ imageInfo [0],

"Height" => $ imageInfo [1],

"Type" => $ imageType,

"Size" => $ imageSize,

"Mime" => $ imageInfo ['mime ']

);

Return $ info;

} Else {

Return FALSE;

}

}

?>

Call example:

1

2

3

4

  

$ Info = getImageInfo('123.jpg ');

Var_dump ($ info );

?>

Output result:

Array (5 ){

["Width"] =>

Int (614)

["Height"] =>

Int (850)

["Type"] =>

String (4) "jpeg"

["Size"] =>

Int (597066)

["Mime"] =>

String (10) "image/jpeg"

}

The output information is the same as the information displayed in the image below.

Image Information:

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.