PHP如何使用getimagesizefromstring()函数?示例

2021年3月29日18:30:03 发表评论 823 次浏览

getimagesizefromstring()function是PHP中的内置函数, 用于从字符串获取图像的大小。此函数接受图像数据作为字符串参数, 并确定图像大小, 然后返回尺寸以及文件类型和图像的高度/宽度。

语法如下:

array getimagesizefromstring( $imagedata, &$imageinfo )

参数:此函数接受上述和以下所述的两个参数:

  • $文件名:这是一个强制参数, 它接受图像数据作为字符串。
  • $ imageinfo:它是一个可选参数, 允许从图像文件中提取一些扩展信息, 例如将不同的JPG APP标记作为关联数组。

返回值:此函数返回尺寸以及文件类型和高度/宽度文本字符串。

下面的程序说明了getimagesizefromstring()PHP中的功能:

图片:

PHP如何使用getimagesizefromstring()函数?示例

范例1:

<?php
  
$img = 
'https://media.lsbin.org/wp-content/uploads/lsbin-25.png' ;
   
// Open image as a string
$data = file_get_contents ( $img );
   
// getimagesizefromstring function accepts image data as string
$info = getimagesizefromstring( $data );
  
// Display the image content
var_dump( $info );
?>

输出如下:

array(6) { 
    [0]=> int(667) 
    [1]=> int(184) 
    [2]=> int(3) 
    [3]=> string(24) "width="667" height="184"" 
    ["bits"]=> int(8) 
    ["mime"]=> string(9) "image/png" 
}

范例2:

<?php
$img = 
'https://media.lsbin.org/wp-content/uploads/lsbin-25.png' ;
   
// Open image as a string
$data = file_get_contents ( $img );
  
// getimagesizefromstring function accepts image data as string
list( $width , $height , $type , $attr ) = getimagesizefromstring( $data );
   
// Displaying dimensions of the image 
echo "Width of image: " . $width . "<br>" ; 
     
echo "Height of image: " . $height . "<br>" ; 
     
echo "Image type: " . $type . "<br>" ; 
     
echo "Image attribute: " . $attr ; 
?>

输出如下:

Width of image: 667
Height of image: 184
Image type: 3
Image attribute: width="667" height="184"

参考: http://php.net/manual/en/function.getimagesizefromstring.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: