Search code examples
phpgetimagesize

getimagesize() is not working with PNG images having HTTPS


i have this image:

  $imgurl = 'https://www.danmurphys.com.au/media/DM/Product/308x385/913411_0_9999_med_v1_m56577569854513142.png';

I have tried these both codes

  1. $image = @getimagesize($imgurl);
    print_r($image);
    

Gives no result.

See 2nd case below starts with function getRanger

  1. public static function getRanger($url){
        $headers = array(
        "Range: bytes=0-327680"
        );
    
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
    
    $raw    = self::getRanger($imgurl);
    $im     = imagecreatefromstring($raw);
    
    $width  = imagesx($im);
    $height = imagesy($im);
    
    echo $width;
    echo $height;
    

Both gives me empty result. can some of you can help me.

Thanks in advance.


Solution

  • 2nd one is nearly there.

    I am quite sure that calling

    echo curl_error($curl);
    

    after curl's execution will give you: SSL certificate problem: unable to get local issuer certificate

    Basically there are two ways to fix this -

    Proper way

    download cacert.pem file from https://curl.haxx.se/docs/caextract.html, save it somewhere where your script can reach it and add following line before curl_exec(); call

        // Add certification atuhority info
        curl_setopt($curl, CURLOPT_CAINFO, './path/to/cacert.pem'); 
    

    Note, this won't work with self-signed SSL Certificates

    Quick fix

    You might want to make SSL checks 'loose'.

        // disable SSL checks
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    

    Note, this method will work even with self-signed SSL Certificates, but doing so is considered insecure.