Skip to content

Instantly share code, notes, and snippets.

@gooof
Last active December 18, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gooof/f02271d07462ea91a816 to your computer and use it in GitHub Desktop.
Save gooof/f02271d07462ea91a816 to your computer and use it in GitHub Desktop.
phpBB getimagesize + get remote image headers (max 48kb) for getimagesize() + 60min positive temp cache + cURL + fsockopen + fopen
<?php
/* Todo:
* + avoid timeouts with an countdown?
* + get width/height with imagecreatefromstring
*/
echo '<hr>Test 1 with my curl + getimagesize + fopen fallback: <br>';
$start = microtime(true);
function phpbb_getimagesize_fn($data, $readonly = false)
{
static $chk = '';
if ($readonly)
{
$get = $chk;
$chk = '';
return $get;
}
$chk .= $data;
}
function phpbb_getimagesize($url)
{
static $limit = 49152; // bytes
//static $avoid_timeout = time(); // avoid timeouts with an countdown
//static $limit_func_hits = 0; // function hit limit
//if ($limit_func_hits++ >= 100) { return 'UNABLE_GET_IMAGE_SIZE'; }
if (function_exists('curl_init'))
{
// php 5.3+ only
// use function writefn($ch, $chunk) { ... } for earlier versions
$writefn = function($ch, $chunk) {
static $limit = 49152; // bytes
static $curl_data = '';
$len = strlen($curl_data) + strlen($chunk);
if ($len >= $limit)
{
//$curl_data .= substr($chunk, 0, $limit - strlen($curl_data));
phpbb_getimagesize_fn($chunk);
$curl_data = '';
return -1;
}
$curl_data .= $chunk;
phpbb_getimagesize_fn($chunk);
return strlen($chunk);
};
$headers = array(
'Range: bytes=0-' . $limit,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); // CURLOPT_CONNECTTIMEOUT_MS = 1200
curl_setopt($ch, CURLOPT_TIMEOUT, 2); // CURLOPT_TIMEOUT_MS = 1200
curl_setopt($ch, CURLOPT_RANGE, '0-' . $limit);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
$result = curl_exec($ch);
curl_close($ch);
// save temp file
$chunk = phpbb_getimagesize_fn('', true);
$fp = fopen('./store/testimgdata.jpg', 'wb');
fwrite($fp, $chunk);
fclose($fp);
$is = getimagesize('./store/testimgdata.jpg');
unset($curl_data);
}
elseif (function_exists('fsockopen'))
{
// ... ??
}
else
{
//global $phpbb_root_path;
//$tmpfilename = tempnam($phpbb_root_path . 'store/', unique_id() . '-');
$tmpfilename = './store/testimgdata.jpg';
$fp = fopen($url, 'rb');
// TODO: stream_set_timeout($fp, 4); // timeout
if (!$fp) return false;
$tmpfile = fopen($tmpfilename, 'wb');
$size = 0;
while ($size < $limit && !feof($fp))
{
$content = fread($fp, 8192);
$size += 8192;
fwrite($tmpfile, $content);
}
echo '<b>' . $size . '</b> ';
fclose($fp);
fclose($tmpfile);
$is = getimagesize($tmpfilename);
//unlink($tmpfilename);
}
// get size
echo "size: ";
print_r($is);
}
phpbb_getimagesize('http://digilicious.org/gallery/worldmap/Worldmap_short_names_large.png');
echo '<hr>';
phpbb_getimagesize('http://www.esa.int/images/image2-492-20110120-8974-HighresImage-02-PhobosFlyby.jpg');
echo '<hr>';
phpbb_getimagesize('http://iambrony.jsmart.web.id/mlp/gif/212381__safe_animated_scootaloo_vector_artist-deadparrot22.gif_001.gif');
echo '<hr>FALSE with 32kb:<br />';
phpbb_getimagesize('http://kingsfordhomestead.com.au/hi_res/120312_KHS_Images_1074-Edit.jpg');
echo '<hr>';
phpbb_getimagesize('http://eoimages.gsfc.nasa.gov/images/imagerecords/79000/79383/14_goe_2012259_lrg.jpg');
$stop = round(microtime(true) - $start, 5);
echo "<br> ({$stop}s)";
unset($start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment