0)
{
$buf = fread($src, $buflen);
echo($buf);
}
} while (($buflen>0) and (!feof($src)));
return true;
}
/**
* nahrada za readfile()
*
* @param string $filename
*/
function my_readfile($filename)
{
$h = fopen($filename, 'rb');
if(!$h) return false;
fcopy_stdout($h, filesize($filename));
fclose($h);
}
// nasleduje obsah vykradene NoICEho core.fnc lib ...
// zkopcena NoICEho core.image knihovna
# returns proper image size shrinked to the given limits
# $img = image resource
function ciShrinkedSize($sx,$sy,$limitx,$limity)
{
$size = array(0=>$sx,1=>$sy);
if($limitx>=$sx and $limity>=$sy) return $size;
$width = $limitx;
$height = $limity;
$ratio_orig = $size[0]/$size[1];
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$size[0] = ceil($width);
$size[1] = ceil($height);
return $size;
}
# returns proper image size shrinked to the given limits
# $img = image resource
function ciImageSizeShrinked($img,$limitx,$limity)
{
$size = getimagesize($img);
list($size[0],$size[1]) = ciShrinkedSize($size[0],$size[1],$limitx,$limity);
return $size;
}
# this function resizes an image to its dest*$quality size and then resample to its
# dest*1 size, thus resizing large images is twice faster and with very good quality
function ciCopyResampledFast($imgd,$imgs,$destx,$desty,$srcx,$srcy,$dx,$dy,$sx,$sy,$quality=3)
{
if(($dx<=$sx) and ($dy<=$sy))
{
$rdx = ($dx * $quality);
$rdy = ($dy * $quality);
if(($rdx < $sx) and ($rdy < $sy))
{
$r = imagecreatetruecolor($rdx,$rdy);
imagecopyresized($r,$imgs,0,0,$srcx,$srcy,$rdx,$rdy,$sx,$sy);
imagecopyresampled($imgd,$r,$destx,$desty,0,0,$dx,$dy,$rdx,$rdy);
imagedestroy($r);
} else {
imagecopyresampled ( $imgd, $imgs, $destx, $desty, $srcx, $srcy, $dx, $dy, $sx, $sy);
}
} else { // for enlarging we will use normal method
imagecopyresampled ( $imgd, $imgs, $destx, $desty, $srcx, $srcy, $dx, $dy, $sx, $sy);
}
}
function ciResampleFast(&$img,$x,$y)
{
$i = imagecreatetruecolor($x,$y);
ciCopyResampledFast($i, $img, 0, 0, 0, 0, $x, $y, imagesx($img), imagesy($img));
imagedestroy($img);
$img = $i;
}
function ciResampleFastProp(&$img,$x,$y)
{
$s = ciShrinkedSize(imagesx($img),imagesy($img),$x,$y);
ciResampleFast($img,$s[0],$s[1]);
return $s;
}
function http_status_messages()
{
$statuses = array(
200 => "OK",
304 => "Not Modified",
400 => "Bad Request",
403 => "Forbidden",
404 => "Not Found",
500 => "Internal Server Error",
);
return $statuses;
}
/**
* osekana verhe HTTPStatus z core.fnc.php
* odesle hlavicku HTTP/1.1 podle daneho kodu
* @param int $i status identifier
*/
function http_status($i)
{
$statuses = http_status_messages();
if(!isset($statuses[$i]))
{
http_status(500);
exit_with_message(500);
}
header('HTTP/1.1 '.$i.' '.$statuses[$i]);
}
function exit_with_message($i, $body = '')
{
$statuses = http_status_messages();
exit('
'.$i.' - '.$statuses[$i].'
'."\n".$body);
}
function http_exception($i, $body = '')
{
http_status($i);
exit_with_message($i, $body);
}
function err_image($img, $filename)
{
header('Pragma: private');
header('Cache-control: private, must-revalidate');
header("Last-Modified: ".gmdate("D, d M Y H:i:s", time())." GMT");
header('Content-Type: '.Ck_Shared::get_mime('png'));
header('Content-Disposition: inline; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary");
imagepng($img, null, 9, PNG_ALL_FILTERS);
return false;
}
function output_image($img, $ext, $thumbfile)
{
switch(strtolower($ext))
{
case 'png':
imagesavealpha($img, true);
imagepng($img, $thumbfile,9,PNG_ALL_FILTERS);
header('Content-Length: '.filesize($thumbfile));
my_readfile($thumbfile);
break;
case 'gif':
imagetruecolortopalette($img, true, 255);
imagegif($img, $thumbfile);
header('Content-Length: '.filesize($thumbfile));
my_readfile($thumbfile);
break;
case 'jpg':
case 'jpeg':
default:
imagejpeg($img, $thumbfile, 90);
header('Content-Length: '.filesize($thumbfile));
my_readfile($thumbfile);
break;
}
return true;
}
function try_from_cache($thumbfile, $ext)
{
// pokud uz existuje cachnuta verze...
if(file_exists($thumbfile))
{
$gmdate = gmdate("D, d M Y H:i:s",filemtime($thumbfile)).' GMT';
// headers
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && $_SERVER["HTTP_IF_MODIFIED_SINCE"] >= $gmdate)
{
http_status(304);
header("Last-Modified: ".$gmdate);
header('Content-Type: '.Ck_Shared::get_mime($ext));
header('Content-Length: '.filesize($thumbfile));
return true;
}
http_status(200);
// header('Pragma: private');
// header('Cache-control: private, must-revalidate');
header("Last-Modified: ".$gmdate);
header('Content-Type: '.Ck_Shared::get_mime($ext));
header('Content-Length: '.filesize($thumbfile));
header('Content-Disposition: inline; filename="'.basename($thumbfile).'"');
header("Content-Transfer-Encoding: binary");
my_readfile($thumbfile);
return true;
}
return false;
}
/**
* podiva se, jestli neni obrazek v cache pro danou velikost... kdyz neni,
* zmeni velikost obrazku, cachne, vrati do outputu se spravnou hlavickou
* @param string $filename
* @param int $width
* @param int $height
* @return boolean
*/
function resize_user_photo_with_headers($filename, $width, $height, $logo = true)
{
// zdrojova i cilova pripona souboru
// cela cesta ke zdrojovemu souboru
//$realfile = BASE_DIR.(substr(trim($filename), 0, 1) == '/' ? '' : '/' ).$filename;
$filename = trim($filename);
if(ONLINE !== false)
{
$realfile = ONLINE.(substr(trim($filename), 0, 1) == '/' ? '' : '/' ).str_replace(' ', '%20', $filename);
} else {
// $realfile = BASE_DIR.(substr(trim($filename), 0, 1) == '/' ? '' : '/' ).$filename;
$realfile = (substr($filename, 0, 2) == './' || substr($filename, 0, 4) == 'http' ? '' : './' ).$filename;
}
// cela cesta k nahledu
$ext = Ck_Shared::file_ext($filename);
$thumbfile = sprintf('%s-%04x-%04x'.($logo ? '' : '-nl').".%s", Ck_Shared::fixed_crc32($filename), $width, $height, $ext);
// adresar s nahledy je ve dvou urovnich
$fileid = Ck_Shared::fixed_crc32($thumbfile);
$a = str_pad(dechex($fileid / 65536), 2, "0");
$b = str_pad(dechex(($fileid % 65536) / 256), 2, "0");
$thumbdir = BASE_DIR.'/cache/thumbs/'.strtolower($a).'/'.strtolower($b).'/';
if(!file_exists($thumbdir)) { Ck_Shared::mk_dir($thumbdir, 0777); }
$thumbfile = $thumbdir.$thumbfile;
// var_dump($thumbdir);
// var_dump($thumbfile);
// exit();
if(try_from_cache($thumbfile, $ext)) return true;
// chyba pri nahravani? - cili jinak receno necachovat thumb?
$err = false;
// nacteni obrazku
$img = Ck_Shared::load_image($realfile);
if(!$img)
{
$img = Ck_Shared::load_image(ERR_IMAGE);
if(!$img) http_exception(404, 'Image Not Found'); // no failsafe? so fail.
imagealphablending($img, false);
imagesavealpha($img, true);
$err = true;
}
ciResampleFastProp($img, $width, $height);
$img_x = imagesx($img);
$img_y = imagesy($img);
if($logo && !$err && $img_x >= 300 && $img_y >= 300)
{
// logo
$logo = Ck_Shared::load_image(BASE_DIR.'/img/vodo.png');
imagealphablending($logo, true);
imagealphablending($img, true);
imagesavealpha($img, true);
imagesavealpha($logo, true);
// pravy dolni roh
$l_x = imagesx($logo);
$l_y = imagesy($logo);
$s = ciShrinkedSize($l_x, $l_y, $img_x / 3, $img_y / 3);
imagecopyresampled($img, $logo, // dst image, src image
$img_x-$s[0]-3, $img_y-$s[1]-3, // dst x, dst y
0, 0, // src x, src y
$s[0], $s[1], // dst w, dst h
$l_x, $l_y // src w, src h
);
}
if($err) return err_image($img, $realfile);
// headers
// header('Pragma: private');
// header('Cache-control: private, must-revalidate');
header("Last-Modified: ".gmdate("D, d M Y H:i:s", time())." GMT");
header('Content-Type: '.Ck_Shared::get_mime($ext));
header('Content-Disposition: inline; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary");
output_image($img, $ext, $thumbfile);
return true;
}
function resize_youtube_with_headers($filename, $width, $height, $logo = true)
{
$realfile = str_replace(' ', '%20', $filename);
$realfile = 'http://img.youtube.com/vi/'.$realfile.'/0.jpg';
// cela cesta k nahledu
$ext = Ck_Shared::file_ext($filename);
$thumbfile = BASE_DIR.'/cache/thumbs/'.sprintf('%s-%04x-%04x'.($logo ? '' : '-nl').'.%s', md5($filename), $width, $height, $ext);
if(try_from_cache($thumbfile, 'jpg')) return true;
// chyba pri nahravani? - cili jinak receno necachovat thumb?
$err = false;
// nacteni obrazku
$img = Ck_Shared::load_image($realfile);
if(!$img)
{
$img = Ck_Shared::load_image(ERR_IMAGE);
if(!$img) http_exception(404, 'Image Not Found'); // no failsafe? so fail.
imagealphablending($img, false);
imagesavealpha($img, true);
$err = true;
}
ciResampleFastProp($img, $width, $height);
$img_x = imagesx($img);
$img_y = imagesy($img);
if($logo && !$err && $img_x >= 40 && $img_y >= 40)
{
// logo
$logo = Ck_Shared::load_image(BASE_DIR.'/img/logo.png');
imagealphablending($logo, true);
imagealphablending($img, true);
imagesavealpha($img, true);
imagesavealpha($logo, true);
// pravy dolni roh
$s = ciShrinkedSize(imagesx($logo), imagesy($logo), $img_x / 3, $img_y / 3);
imagecopyresampled($img, $logo, // dst image, src image
$img_x-$s[0]-3, $img_y-$s[1]-3, // dst x, dst y
0, 0, // src x, src y
$s[0], $s[1], // dst w, dst h
imagesx($logo), imagesy($logo) // src w, src h
);
imagedestroy($logo);
}
if($err) return err_image($img, $realfile);
// headers
// header('Pragma: private');
// header('Cache-control: private, must-revalidate');
header("Last-Modified: ".gmdate("D, d M Y H:i:s", time())." GMT");
header('Content-Type: image/jpeg');
header('Content-Disposition: inline; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary");
output_image($img, $ext, $thumbfile);
imagedestroy($img);
return true;
}
// let's rock ...
$fname = Ck_Shared::safe_get('f');
$lname = Ck_Shared::safe_get('y');
$width = Ck_Shared::safe_get('w');
$height = Ck_Shared::safe_get('h');
$l = Ck_Shared::safe_get('l');
$l = ($l === null || $l == true ? true : false );
// pokud je neco spatne.. neco chybi...
if((empty($fname) && empty($lname)) || empty($width) || empty($height) || !is_numeric($width) || !is_numeric($height))
{
http_exception(400, 'Some parameters are missing');
}
if(!empty($fname))
{
resize_user_photo_with_headers($fname, $width, $height, $l);
} elseif(!empty($lname))
{
resize_youtube_with_headers($lname, $width, $height, $l);
}