Moving from getimagesize() to CURL

Okay, I was trying to incorporate an avatar-validating script I wrote for phpBB on a different site (non-DH), and then discovered that the fopen() disabling affected me unexpectedly (I was using getimagesize()), so I went and adapted some curl scripts to match the functionality I needed.

Right now, I only really need to do dimension-checking, but since my original script did both, I figured I may as well learn how to adapt the whole thing.

Now, changing this script seems to make my profile mod changes take a lot longer (like 10 seconds), and then I get errors returned which seem to indicate that the remote images aren’t being successfully accessed.

Warning: imagecreatefromstring(): Data is not in a recognized format. in
Warning: imagesx(): supplied argument is not a valid Image resource in
Warning: imagesy(): supplied argument is not a valid Image resource in

I’ll spare the headers-already-started errors. Here’s the relevant curl code I wrote:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $user_avatar_remoteurl);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$okay = curl_exec($ch);
echo $okay;
$head = ob_get_contents();
echo $head;
$regex = ‘/Content-Length:\s([0-9].+?)\s/’;
$count = preg_match($regex, $head, $matches);
$size = $matches[1];
$size = 0;

curl_setopt($ch, CURLOPT_URL, $user_avatar_remoteurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$file_contents = curl_exec($ch);

$r_avatar_data = ImageCreateFromString($file_contents);

$size = 0;
$width = ImageSX($r_avatar_data);
$height = ImageSY($r_avatar_data);

$r_avatar_filesize = (is_numeric($size) ? intval($size) : false);

I assume the 10 second delay or so is from the script trying to access the remote image and timing out (since timeout is set for 5, and the access happens twice). Anyone have any suggestions on what I’m doing wrong here? I don’t actually need to retain the binary data for the image, but I was using that so that I could pump it into ImageCreateFromString to get dimensions.

It should also be noted that $okay and $head (which I echo in the first half of the script - just for testing) are empty.

Well, at least nobody came in and gave me a smarmy answer like this guy got:

Anyone have any thoughts? I think a week+ of inactivity merits a bump.

I just had the same problem, the following code works for me:

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $filename);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);

$new_image = ImageCreateFromString($file_contents);
imagejpeg($new_image, “temp.jpg”,100);

// Get new dimensions
$myfilesize = getimagesize(“temp.jpg”);
$width_orig = $myfilesize[0];
$height_orig = $myfilesize[1];

Works great. I only need dimension-checking right now, but if I need filesize checking, I’m sure I can figure it out at that point.

Thanks a lot!

didn’t work for me or my script.

by the way I have searched for hours on every wiki forum
search engine.

getfilesize() for curl.

here’s the nasty bit of code.

if(!($min_width = getimagesize($file_name))) {
$file_name = “./logo/$logoname[0].$logoname[1]”;
$min_width = getimagesize($file_name);

if ($width != “” AND $width <= $min_width[0]) $width_out = “width=$width”; else $width_out = “”;

It works for me now (years later :-)), after having changed the next lines of code:

  • In the first part, I added ‘curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);’, removing recurring options from the second part.
  • I didn’t use the connection-timeout-option.

Thank your for your question. It helped me!