There is a function in PHP that can obtain information such as the width and height of an image. It is the getimagesize() function. This article will give you a brief introduction and a small example for your reference.
getimagesize() function array getimagesize ( string $filename [, array &$imageinfo ] ) Returns an array with four cells. Index 0 contains the pixel value of the image width, and index 1 contains the pixel value of the image height. Index 2 is the tag of the image type: 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. These tags correspond to the new IMAGETYPE constant added in PHP 4.3.0. Index 3 is a text string with the content "height="yyy" width="xxx"" and can be used directly in the IMG tag. Example: <?php $images_array = array("http://img.jbxue.com/img/logo.gif"); foreach($images_array as $image) { list($width, $height, $type, $attr) = getimagesize($image); $new_height = (int)(192 / $width * $height); echo '<li><img src="'.$image.'" style="max-width:90%" height="'.$new_height.'" / alt="PHP function introduction to obtain image width and height information" >'; } ?> Copy after login |