Mighty Gorgon
Mighty Gorgon

Reputation: 33

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.

Upvotes: 1

Views: 10761

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 99011

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

Upvotes: 7

AntoineB
AntoineB

Reputation: 4694

You have to turn allow_url_fopen on in your php.ini file in order to allow it to access resources that are not local.

This is specified in the official document of getimagesize in the description of the filename parameter.

Upvotes: 3

Related Questions