Php detects the length and width of the uploaded image-PHP source code

Source: Internet
Author: User
Ec (2); * The arraygetimagesize (string $ filename [, array & amp; $ imageinfo]) getimagesize () function will determine any given image size file, the returned text string along with the file type and height is the size used in a normal HTMLIMG label and corresponding HTTP content type. Thegetimagesize () functionw script ec (2); script

/*
Array getimagesize (string $ filename [, array & $ imageinfo])

The getimagesize () function will determine any given image size file, the returned text string along with the file type and height/width is the size used in a normal html img tag and the corresponding HTTP content type.
The getimagesize () function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal html I www.111cn.net MG tag and the correspondant HTTP content type

And getimagesize () can also return more information about some imageinfo parameters.

Note: Warning for teenagers and JP2 have different deep components. In this case, the value of BITs is the greatest bit depth. In addition, the JPEG file on JP2 may contain multiple 2000 codestreams. In this case, the value of the first bitstream returned with getimagesize () is in contact with the root of the file.

Note: For information retrieval icons, refer to the highest bit rate icons.
*/

List ($ width, $ height) = getimagesize ($ image );
$ New_dimensions = resize_dimensions (300,400, $ width, $ height );

// Calculates restricted dimensions with a maximum of $ goal_width by $ goal_height
Function resize_dimensions ($ goal_width, $ goal_height, $ width, $ height ){
$ Return = array ('width' => $ width, 'height' => $ height );

// If the ratio> goal ratio and the width> goal width resize down to goal width
If ($ width/$ height> $ goal_width/$ goal_height & $ width> $ goal_width ){
$ Return ['width'] = $ goal_width;
$ Return ['height'] = $ goal_width/$ width * $ height;
}
// Otherwise, if the height> goal, resize down to goal height
Else if ($ height> $ goal_height ){
$ Return ['width'] = $ goal_height/$ height * $ width;
$ Return ['height'] = $ goal_height;
}

Return $ return;
}

/*
The above functions are used

Php has an image GD library getimagesize () function.
A function is used to obtain the basic information of an image.
Getimagesize ()
$ Img = getimagesize ('image source ');
The width is = $ img [0];
Height = $ img [1];
Format: = $ img [2];

It can be simpler if you want

*/
$ Picpath = 'ww.111cn.net.gif ';
$ Array = getimagesize ($ picpath );
Print_r ($ array );

Echo 'image width is '. $ array [0];
Echo 'image height is '. $ array [1];
Echo 'image format is '. $ array [2];

Method 4
// Renumber
$ My_image = array_values(getimagesize('test.jpg '));
// Use list on new array
List ($ width, $ height, $ type, $ attr) = $ my_image;

// View new array
Print_r ($ my_image );

// Spit out content
Echo 'attribute: '. $ attr .'
';
Echo 'width: '. $ Width .'
';

// The image width and height are displayed.

// Another code that uses getimagesize to display thumbnails
Function show_thumbnail ($ file)
{
$ Max = 200 // 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;
}

Related Article

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.