Search code examples
phpfopenmod-securitygetimagesizesafe-mode

PHP getimagesize not working on external links


I have some issues on my server in running getimagesize on remote url images.

For example if I run this code on my local server it works fine and returns OK:

<?php
$file = 'http://inspiring-photography.com/wp-content/uploads/2012/04/Culla-bay-rock-formations-Isle-of-Uist.jpg';
$pic_size = getimagesize($file);
if (empty($pic_size))
{
    die('FALSE');
}
die('OK');

?>

But if I run the same code on my server I cannot make it work properly. Can you help in determining which settings shall I ask to enable?

I think some of these may be involved:

  1. mod_security
  2. safe_mode
  3. allow_url_fopen

Can you help me in determining the right configuration to get this solved?

Thank you very much in advance.


Solution

  • allow_url_fopen is off. Either enable it on your php.ini, or use curl to get the image:

    function getImg($url){
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
    
    $url = "http://inspiring-photography.com/wp-content/uploads/2012/04/Culla-bay-rock-formations-Isle-of-Uist.jpg";
    $raw = getImg($url);
    $im = imagecreatefromstring($raw);
    $width = imagesx($im);
    $height = imagesy($im);
    echo $width." x ".$height;
    

    SRC