PHP detects the length and width of uploaded images

Source: Internet
Author: User

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

The getimagesize () function determines any given image size of the file and returns the size of the text string with the file type and height/width used in a normal HTML img tag and corresponding HTTP content type.
The getimagesize () function would determine the size of any given image file and return the dimensions along with the file Type and a height/width text string to is used inside a normal HTML I www.111cn.net MG tag and the correspondant HTTP cont ENT type

and getimagesize () can also return some more information about the Imageinfo parameters.

Note: Please note that JPC and JP2 are capable of having different bit depth components. In this case, the value of the "bit" is the highest bit depth of difficulty. In addition, the JPEG file on the JP2 may contain multiple codestreams. In this case, and getimagesize () returns the value of the first stream of code in the root contact of the file.

Note: About the data retrieval icon from the highest bit rate icon.
*/

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 function is that we use

PHP has a picture GD library getimagesize () function.
One function is to get the basic information of a picture.
GetImageSize ()
$img =getimagesize (' picture source ');
width = $img [0];
Height for = $img [1];
The format is = $img [2];

If you want simple words can be simpler

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

echo ' picture width of '. $array [0];
echo ' picture height '. $array [1];
echo ' picture format is '. $array [2];

Method Four
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. ' <br/> ';
Echo ' Width: '. $width. ' <br/> ';

There will be the width and height of the picture.

Another code that uses getimagesize to display thumbnails
function Show_thumbnail ($file)
{
$max =/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 = ' <a href= ' JavaScript tutorial:; "onclick=" window.open (' image.php?img= ';
$ret. = $file. ', ', ' width= '. $size [0];
$ret. = ', height= '. $size [1]. " > ';
$ret. = ' </a>";
}

return $ret;
}

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.