我正在尝试确定图像的元数据。此代码适用于我遇到的每个有效图像引用,直到现在。这是我正在使用的代码:
<?php
$url = "http://206.196.103.100/Vendor%20Images/ADI36462.jpg";
try {
list($content, $img_info) = captureImageData($url);
print("Success!!\n");
var_dump($img_info);
} catch (Exception $e) {
print("Failure!!\n" . (string)$e);
}
function captureImageData($filepath) {
print('"' . $filepath . '" detected as a remote file' . "\n");
$local_file = '/tmp/img_' . uniqid();
try {
// harvest the data into a temp file
$cmd = 'wget -O "' . $local_file . '" "' . $filepath . '"';
print('Running cmd: ' . $cmd . "\n");
`$cmd`;
} catch (Exception $e) {
// output the wget headers
unlink($local_file);
throw new Exception('Unable to read file: '.$filepath);
}
$content = file_get_contents($local_file);
if ($content === false) {
unlink($local_file);
throw new Exception('Unable to read file: '.$filepath);
}
$img_info = getimagesize($local_file);
if ($img_info === false) {
unlink($local_file);
throw new Exception('File "' . $filepath . '" did not return an image');
}
unlink($local_file);
return array($content, $img_info);
}
在进入getimagesize()调用之前,所有内容都处理得很好,并返回false。这种情况是不是原因,还是我应该做其他检查?