Moving from getimagesize() to CURL
Posted by: Cutriss
Posted on: 2005-10-23 11:03:00
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:
ob_start();
$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;
ob_end_clean();
$regex = '/Content-Length:s([0-9].+?)s/';
$count = preg_match($regex, $head, $matches);
if(isset($matches[1]))
{
$size = $matches[1];
}
else
{
$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);
curl_close($ch);
$r_avatar_data = ImageCreateFromString($file_contents);
if(!$r_avatar_data)
$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.