I am retrieving facebook album images from facebook.I have calculating the image size using the php function getimagesize.This function is working fine when the url is in http mode.When the facebook return the image url with https the getimagesize giving error.How i can calculate the imagesize of images with https extension using getimage size
-
You should be able to just strip the `s` out of your image url. – luastoned Dec 15 '11 at 10:17
-
Did you try to replace https with http mannualy? I think it will work. – Narek Dec 15 '11 at 10:18
-
Also: post the error message being output. – leepowers Dec 15 '11 at 10:21
-
@powers1 `Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?` – luastoned Dec 15 '11 at 10:24
-
I replaced https with http.But i think it is not a good method to do.is their any other method to do this.... – Warrior Dec 15 '11 at 10:34
-
You would have to recompile PHP with OpenSSL – luastoned Dec 15 '11 at 10:49
4 Answers
You do not have the OpenSSL extension installed in your PHP instance, so the https://
wrapper is not available.
Note: HTTPS is only supported when the openssl extension is enabled.
And:
To use PHP's OpenSSL support you must also compile PHP --with-openssl[=DIR] .
You will need to recompile PHP with the OpenSSL extension.
Alternatively, as suggested by others, you can replace the https://
with http://
, which for Facebook images should work just as well - indeed, it may be quicker, and will certainly be more bandwidth efficient.
I would do that like this:
$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);
}
Another point about that is that passing $url
directly in to getimagesize()
is probably not what you want to be doing. It is unlikely that the only thing you would be doing with the image is getting it's size, you will probably be displaying it on your page or otherwise manipulating it, and if this were the case your will end up downloading it more than once.
You should probably download it to a temporary directory, then work on a local copy of it.

- 87,921
- 11
- 154
- 174
-
I have OpenSSL as seen in my `phpinfo` output: `OpenSSL support enabled OpenSSL Library Version OpenSSL 1.0.2g 1 Mar 2016 OpenSSL Header Version OpenSSL 1.0.2g 1 Mar 2016 Openssl default config /usr/lib/ssl/openssl.cnf` but the `https` prevents the `getimagesize()` from opening the image. – Stephane Feb 10 '17 at 12:27
-
1@Stephane there could be several reasons for this. Turn error reporting up to the maximum (`error_reporting(-1); ini_set('display_errors', '1');`) and inspect any error messages for clues as to why this is not working. Most likely is a certificate error. – DaveRandom Feb 10 '17 at 13:16
/**
* 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);
Output
array (size=4)
'width' => int 640
'height' => int 480
'mime' => string 'image/jpeg' (length=10)
'resource' => resource(6, gd)

- 171
- 2
- 6
I tested this and it worked fine.
$url = "https://path/to/image.jpg";
$url = str_replace("https://", "http://", $url);
$size = getimagesize($url);

- 663
- 3
- 4