PHP Conference Japan 2024

getimagesizefromstring

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

getimagesizefromstring從字串取得影像大小

描述

getimagesizefromstring(字串 $string, 陣列 &$image_info = null): 陣列|false

getimagesize() 相同,不同之處在於 getimagesizefromstring() 接受字串而不是檔名作為第一個參數。

有關此函式運作方式的詳細資訊,請參閱 getimagesize() 文件。

參數

string (字串)

影像資料,以字串形式呈現。

image_info

請參閱 getimagesize()

返回值

請參閱 getimagesize()

範例

範例 #1 getimagesizefromstring() 範例

<?php
$img
= '/path/to/test.png';

// 以檔案方式開啟
$size_info1 = getimagesize($img);

// 或以字串方式開啟
$data = file_get_contents($img);
$size_info2 = getimagesizefromstring($data);
?>

另請參閱

新增註釋

使用者貢獻的註釋 2 則註釋

24
imageman
10 年前
適用於 5.4 以下版本的 getimagesizefromstring 函式

<?php
if (!function_exists('getimagesizefromstring')) {
function
getimagesizefromstring($string_data)
{
$uri = 'data://application/octet-stream;base64,' . base64_encode($string_data);
return
getimagesize($uri);
}
}
?>
5
sarah at anigel dot net
10 年前
針對 imageman 提供的 5.4 以下版本解決方案,需要啟用 allow_url_fopen 才能使用 data wrapper。
To Top