View source
<?php
if (strpos(base64_decode($_GET['imgp']), "://") !== false) {
header("HTTP/1.0 404 Not Found");
exit;
}
drupalize();
function drupalize() {
while (!@stat('./includes/bootstrap.inc')) {
chdir('..');
}
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
}
$GLOBALS['devel_shutdown'] = FALSE;
$bgcachexpire = 3600 * 24 * 3;
if (variable_get('brilliant_gallery_cache', 'd') == 'f') {
$my_data = resizeimage_wrapper_filecache();
}
else {
$my_data = resizeimage_wrapper_dbcache();
}
header($my_data[0]);
echo base64_decode($my_data[1]);
exit;
function resizeimage_wrapper_filecache() {
global $bgcachexpire;
$bgcacheid = 'bg_' . md5($_GET['imgp'] . $_GET['imgw'] . $_GET['imgh']);
$cachedfile = file_directory_temp() . '/' . $bgcacheid;
$lastchanged = file_exists($cachedfile) ? filemtime($cachedfile) : false;
if ($lastchanged === false or time() - $lastchanged > $bgcachexpire) {
$my_data = resizeimage($_GET['imgp'], $_GET['imgw'], $_GET['imgh']);
$fh = fopen($cachedfile, "w+");
fwrite($fh, $my_data);
fclose($fh);
$my_data = unserialize($my_data);
}
else {
$my_data = unserialize(file_get_contents($cachedfile));
}
return $my_data;
}
function resizeimage_wrapper_dbcache($reset = FALSE) {
global $bgcachexpire;
global $user;
$bgcacheid = 'bg_' . md5($_GET['imgp'] . $_GET['imgw'] . $_GET['imgh']);
static $my_data;
if (!isset($my_data) || $reset) {
if (!$reset && ($cache = cache_get($bgcacheid)) && !empty($cache->data)) {
$my_data = unserialize($cache->data);
}
else {
$my_data = resizeimage($_GET['imgp'], $_GET['imgw'], $_GET['imgh']);
cache_set($bgcacheid, 'cache', $my_data, time() + $bgcachexpire);
$my_data = unserialize($my_data);
}
}
return $my_data;
}
function resizeimage($imgp, $imgw, $imgh) {
$imagepath = base64_decode($imgp);
$suffix = strtolower(substr($imagepath, -4));
$imgsize = @getimagesize($imagepath);
$head = "Content-type: {$imgsize['mime']}";
if ($suffix == ".gif") {
$img = imagecreatefromgif($imagepath);
if (!$img) {
brokenimage("Error loading GIF");
}
}
else {
if ($suffix == ".jpg" or $suffix == "jpeg") {
$img = imagecreatefromjpeg($imagepath);
if (!$img) {
brokenimage("Error loading JPG");
}
}
else {
if ($suffix == ".png") {
$img = imagecreatefrompng($imagepath);
if (!$img) {
brokenimage("Error loading PNG");
}
}
}
}
$src_h = ImageSY($img);
$src_w = ImageSX($img);
$dst_img = imagecreatetruecolor($imgw, $imgh);
imagecopyresampled($dst_img, $img, 0, 0, 0, 0, $imgw, $imgh, $src_w, $src_h);
$img = $dst_img;
imageinterlace($img, 1);
imagecolortransparent($img);
ob_start();
if ($suffix == ".gif") {
Imagegif($img);
}
else {
if ($suffix == ".jpg" or $suffix == ".jpeg") {
Imagejpeg($img, '', 80);
}
else {
if ($suffix == ".png") {
Imagepng($img);
}
}
}
$result = ob_get_clean();
$result = serialize(array(
$head,
base64_encode($result),
));
return $result;
}
function brokenimage($msg) {
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 0, 0, 0);
$tc = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, $msg, $tc);
imagejpeg($im);
exit;
}