getimagesize()
是一个 PHP 函数,用于获取图像尺寸信息。要在网络请求中使用它,您需要首先使用 cURL 或 file_get_contents() 等函数获取图像的原始数据,然后将其传递给 getimagesize()
函数。以下是两种方法的示例:
方法 1:使用 cURL
function get_image_size_from_url($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $data = https://www.yisu.com/ask/curl_exec($ch);'https://example.com/image.jpg'; $image_size = get_image_size_from_url($url); if ($image_size !== false) { list($width, $height) = $image_size; echo "Image width: $width, height: $height"; } else { echo "Failed to get image size."; }
方法 2:使用 file_get_contents()
function get_image_size_from_url($url) { $data = https://www.yisu.com/ask/file_get_contents($url);'https://example.com/image.jpg'; $image_size = get_image_size_from_url($url); if ($image_size !== false) { list($width, $height) = $image_size; echo "Image width: $width, height: $height"; } else { echo "Failed to get image size."; }
请注意,这两种方法都需要您的 PHP 设置允许从外部 URL 下载文件。如果您的 PHP 设置不允许这样做,您可能需要修改配置文件以允许这些操作。