0

我发现以下功能搜索 SO

function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

这应该可以保存图像,但我如何检查图像,dimensions首先是为了验证它是否有效?sizefiletype

4

2 回答 2

2

像这样的东西?

$raw = curl_exec($ch);
if ($raw === FALSE) {
   die("Fetch failed");
}

// get size
$size = strlen($raw);

// get dimensions
$img = imagecreatefromstring($raw);
$x = imagesx($img);
$y = imagesy($img);

// get mime type
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->buffer($raw); // e.g. image/jpeg
于 2012-06-09T05:39:48.980 回答
0

在触发 curl 之前,您可以使用直接读取图像

$url = 'http://exmaple.com/img/some.png';
$imgInfo = getimagesize($url);
print_r($imgInfo);
于 2012-06-09T05:58:15.377 回答