function fix_image($img_url) { //First download it $fn = tempnam(sys_get_temp_dir(), 'NOT'); file_put_contents($fn, raw_get($img_url)); list($w, $h) = getimagesize($fn); if (filesize($fn) > FILESIZE_LIMIT || $w > IMAGE_DIM_LIMIT || $h > IMAGE_DIM_LIMIT) { $img_url = IMAGE_FIXER . '?img=' . rawurlencode($img_url); } unlink($fn); return $img_url; }
$context = stream_context_create(array('http' => array('method' => 'GET', 'header' => implode("\r\n", array('Accept-Charset: utf-8;q=0.7,*;q=0.7'))))); //Fetch raw output $content = file_get_contents($url, FILE_TEXT, $context); foreach ($http_response_header as $value) { //If the response encoding is gzip, ungzip the content if (stristr($value, 'gzip') !== false) { $content = gzdecode($content); break; } } return $content; } //Fetch the content of image $url = $_GET['img']; $fn = tempnam(sys_get_temp_dir(), 'NOT'); file_put_contents($fn, raw_get($url)); $img = imagecreatefromjpeg($fn); $w = imagesx($img); $h = imagesy($img); $ratio = 1.0; //Check image dimension if ($w > IMAGE_DIM_LIMIT) { $ratio = min($ratio, IMAGE_DIM_LIMIT / (double) $w); } if ($h > IMAGE_DIM_LIMIT) { $ratio = min($ratio, IMAGE_DIM_LIMIT / (double) $h); } unlink($fn); $w = (int) floor($ratio * $w); $h = (int) floor($ratio * $h); $img = imagescale($img, $w, $h);