我正在从facebook检索facebook相册图像。我使用php函数计算图像大小。当url在http mode.When中时,getimagesize.This函数工作得很好。facebook使用https返回图像url。mode.When大小给定error.How我可以使用getimage大小计算带有https扩展名的图像的图像大小。
发布于 2011-12-15 18:30:32
您的实例中没有安装OpenSSL扩展,因此https://包装器不可用。
From the manual
注意:只有在启用openssl扩展时才支持HTTPS。
And
要使用PHP的OpenSSL支持,还必须使用-openssl=DIR编译PHP。
您将需要使用OpenSSL扩展重新编译PHP。
或者,正如其他人所建议的那样,你可以用http://代替https://,这对于Facebook的图片来说应该也是一样的-事实上,它可能更快,而且肯定会更有效地利用带宽。
我会这样做:
$url = 'https://facebook.com/path/to/image.jpg';
$url = trim($url); // Get rid of any accidental whitespace
$parsed = parse_url($url); // analyse the URL
if (isset($parsed['scheme']) && strtolower($parsed['scheme']) == 'https') {
// If it is https, change it to http
$url = 'http://'.substr($url,8);
}关于这一点的另一点是,将$url直接传递给getimagesize()可能不是您想要做的事情。您对图像所做的唯一事情不太可能是获取它的大小,您可能会在页面上显示它或以其他方式操作它,如果是这样的话,您最终将多次下载它。
您可能应该将其下载到临时目录,然后在其本地副本上工作。
发布于 2011-12-15 18:21:47
$newlink = str_replace('https://', 'http://', $oldlink);
我想这可能有助于将https://剥离为http://
发布于 2021-09-30 20:33:59
/**
* Get Image Size Alternative
*
* @param string $url
* @param string $referer
* @return array
*/
function getImage( $url, $referer = '' ) {
$default = array('width' => 0, 'height' => 0, 'mime' => NULL, 'resource' => NULL);
// set headers
$headers = array( 'Range: bytes=0-131072' );
if ( !empty( $referer ) ) { array_push( $headers, 'Referer: ' . $referer ); }
// set curl config
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec( $ch );
$http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
$curl_errno = curl_errno( $ch );
curl_close( $ch );
// valid stauts
if ( $http_status >= 400) {
return $default;
}
// set stream config
stream_context_set_default( [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
$mime = (!empty(get_headers($url, 1)['Content-Type'])) ? get_headers($url, 1)['Content-Type'] : false;
$mime = (is_array($mime) && $mime) ? end($mime) : $mime;
// valid image types
if(!$mime || !preg_match('/png|jpe?g|gif/i',$mime)){
return false;
}
$image_resource = imagecreatefromstring( $data );
if(!$image_resource){
return $default;
}
return ['width' => imagesx($image_resource), 'height' => imagesy($image_resource), 'mime' => $mime, 'resource' => $image_resource];
}
// Set image url
$url = 'http://pudim.com.br/pudim.jpg';
$data = getImage( $url );
// get image resource
var_dump($data);
// for tests
#header ('Content-Type: image/jpeg');
#imagejpeg($data['resource'], null, 78);输出
array (size=4)
'width' => int 640
'height' => int 480
'mime' => string 'image/jpeg' (length=10)
'resource' => resource(6, gd)https://stackoverflow.com/questions/8518403
复制相似问题