| 1 |
<?php
|
| 2 |
/* $Id: image.php,v 1.14 2008/03/13 18:54:31 tjfulopp Exp $ */
|
| 3 |
|
| 4 |
if ( strpos( base64_decode( $_GET['imgp'] ), "://" ) !== false ) { # Fixing a possible URL injection problem. Using ':' was not enough because Windows paths contain it as well.
|
| 5 |
header("HTTP/1.0 404 Not Found");
|
| 6 |
exit();
|
| 7 |
}
|
| 8 |
|
| 9 |
drupalize();
|
| 10 |
function drupalize() {
|
| 11 |
while (!@stat('./includes/bootstrap.inc')) {
|
| 12 |
chdir ('..');
|
| 13 |
}
|
| 14 |
require_once './includes/bootstrap.inc';
|
| 15 |
#drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
|
| 16 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|
| 17 |
#drupal_cron_run();
|
| 18 |
}
|
| 19 |
$GLOBALS['devel_shutdown'] = FALSE; # Crucial - to suppress Devel (if installed and enabled) output appearing in the generated XML!
|
| 20 |
|
| 21 |
$bgcachexpire = 3600*24*3; # Cache expiration time.
|
| 22 |
#$bgcachexpire = 3; # Cache expiration time.
|
| 23 |
|
| 24 |
#chdir ('../../../../');
|
| 25 |
#require_once './includes/bootstrap.inc';
|
| 26 |
#require_once '../../../../../includes/bootstrap.inc';
|
| 27 |
#drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
|
| 28 |
|
| 29 |
#if ( $_SERVER['SERVER_ADDR'] == '64.13.192.90' ) {
|
| 30 |
if ( variable_get('brilliant_gallery_cache', 'd') == 'f' ) {
|
| 31 |
#echo '.....................' . $_SERVER['SERVER_ADDR'];
|
| 32 |
$my_data = resizeimage_wrapper_filecache();
|
| 33 |
} else {
|
| 34 |
$my_data = resizeimage_wrapper_dbcache();
|
| 35 |
}
|
| 36 |
|
| 37 |
#echo '....'. sess_read('vacilando');
|
| 38 |
header($my_data[0]);
|
| 39 |
#echo $my_data[0] . '<br>';
|
| 40 |
echo base64_decode($my_data[1]);
|
| 41 |
exit(); # IMPORTANT - otherwise some process after BG adds strings and breaks the image!
|
| 42 |
|
| 43 |
function resizeimage_wrapper_filecache() {
|
| 44 |
global $bgcachexpire;
|
| 45 |
$bgcacheid = 'bg_' . md5($_GET['imgp'] . $_GET['imgw'] . $_GET['imgh']);
|
| 46 |
#echo $bgcacheid;
|
| 47 |
#echo '. 0.... ';
|
| 48 |
# Tested that both relative (eg sites/all/files/cache) and absolute (eg /home/data/tmp) tmp path settings work OK here.
|
| 49 |
$cachedfile = file_directory_temp() . '/' . $bgcacheid;
|
| 50 |
#$cachedfile = realpath(file_directory_temp() . '/' . $bgcacheid);
|
| 51 |
#echo file_directory_temp() . '/' . $bgcacheid;
|
| 52 |
#echo " .... ";
|
| 53 |
#echo $cachedfile;
|
| 54 |
$lastchanged = (file_exists($cachedfile) ? filemtime( $cachedfile ) : false); # See http://drupal.org/node/194923
|
| 55 |
if ( $lastchanged === false or ( time() - $lastchanged > ( $bgcachexpire ) ) ) {
|
| 56 |
#echo '. 1.... ';
|
| 57 |
# Cache file does not exist or is too old.
|
| 58 |
$my_data = resizeimage ( $_GET['imgp'], $_GET['imgw'], $_GET['imgh'] );
|
| 59 |
# Now put $my_data to cache!
|
| 60 |
$fh = fopen( $cachedfile, "w+" );
|
| 61 |
fwrite( $fh, $my_data );
|
| 62 |
fclose( $fh );
|
| 63 |
#test
|
| 64 |
/*
|
| 65 |
$my_data_t = unserialize( $my_data );
|
| 66 |
$fh = fopen( $cachedfile . '_2', "w+" );
|
| 67 |
fwrite( $fh, $my_data_t[1] );
|
| 68 |
fclose( $fh );
|
| 69 |
*/
|
| 70 |
$my_data = unserialize( $my_data );
|
| 71 |
} else {
|
| 72 |
#echo '. 2.... ';
|
| 73 |
# Cache file exists.
|
| 74 |
$my_data = unserialize( file_get_contents( $cachedfile ) );
|
| 75 |
}
|
| 76 |
return $my_data;
|
| 77 |
}
|
| 78 |
|
| 79 |
function resizeimage_wrapper_dbcache($reset = FALSE) {
|
| 80 |
global $bgcachexpire;
|
| 81 |
global $user;
|
| 82 |
$userId = $user->uid;
|
| 83 |
$bgcacheid = 'bg_' . md5($_GET['imgp'] . $_GET['imgw'] . $_GET['imgh']);
|
| 84 |
#echo $bgcacheid;
|
| 85 |
static $my_data;
|
| 86 |
#echo '0.... ';
|
| 87 |
if (!isset($my_data) || $reset) {
|
| 88 |
if (!$reset && ($cache = cache_get($bgcacheid)) && !empty($cache->data)) {
|
| 89 |
#$my_data = $cache->data; echo '-1.... ' . $my_data;
|
| 90 |
$my_data = unserialize( $cache->data );
|
| 91 |
} else {
|
| 92 |
// Do your expensive calculations here, and populate $my_data
|
| 93 |
// with the correct stuff..
|
| 94 |
$my_data = resizeimage ( $_GET['imgp'], $_GET['imgw'], $_GET['imgh'] );
|
| 95 |
#echo ' -2.... ' . $bgcachexpire . ' // ' . $my_data;
|
| 96 |
cache_set($bgcacheid, 'cache', $my_data, time() + $bgcachexpire); # For some reason I could not use: mysql_escape_string($my_data)
|
| 97 |
$my_data = unserialize( $my_data );
|
| 98 |
}
|
| 99 |
}
|
| 100 |
return $my_data;
|
| 101 |
}
|
| 102 |
|
| 103 |
function resizeimage( $imgp, $imgw, $imgh ) {
|
| 104 |
$imagepath = base64_decode( $imgp );
|
| 105 |
#echo '.... ' . base64_decode( $imgp );
|
| 106 |
#flush();die(' stop!');
|
| 107 |
$suffix = strtolower(substr($imagepath, -4)); # Thanks to Michał Albrecht!
|
| 108 |
$imgsize = @getimagesize($imagepath);
|
| 109 |
$head = "Content-type: {$imgsize['mime']}"; # http://be.php.net/getimagesize
|
| 110 |
if ( $suffix == ".gif" ) {
|
| 111 |
#$head = "Content-type: image/gif";
|
| 112 |
$img = imagecreatefromgif( $imagepath );
|
| 113 |
if (!$img) { brokenimage("Error loading GIF"); }
|
| 114 |
} else if ( $suffix == ".jpg" or $suffix == "jpeg" ) { # Thanks to Michał Albrecht!
|
| 115 |
#$head = "Content-type: image/jpeg";
|
| 116 |
$img = imagecreatefromjpeg( $imagepath );
|
| 117 |
if (!$img) { brokenimage("Error loading JPG"); }
|
| 118 |
} else if ( $suffix == ".png" ) {
|
| 119 |
#$head = "Content-type: image/png";
|
| 120 |
$img = imagecreatefrompng( $imagepath );
|
| 121 |
if (!$img) { brokenimage("Error loading PNG"); }
|
| 122 |
}
|
| 123 |
# Resize the image
|
| 124 |
$src_h = ImageSY( $img );
|
| 125 |
$src_w = ImageSX( $img );
|
| 126 |
$dst_img = imagecreatetruecolor( $imgw, $imgh );
|
| 127 |
imagecopyresampled( $dst_img, $img, 0, 0, 0, 0, $imgw, $imgh, $src_w, $src_h );
|
| 128 |
$img = $dst_img;
|
| 129 |
imageinterlace ( $img, 1 );
|
| 130 |
imagecolortransparent ( $img );
|
| 131 |
ob_start();
|
| 132 |
if ( $suffix == ".gif" ) {
|
| 133 |
Imagegif($img);
|
| 134 |
} else if ( $suffix == ".jpg" or $suffix == ".jpeg" ) {
|
| 135 |
Imagejpeg($img,'',80);
|
| 136 |
} else if ( $suffix == ".png" ) {
|
| 137 |
Imagepng($img);
|
| 138 |
}
|
| 139 |
$result = ob_get_clean();
|
| 140 |
#ImageDestroy($img);
|
| 141 |
$result = serialize(array($head, base64_encode( $result )));
|
| 142 |
return $result;
|
| 143 |
}
|
| 144 |
|
| 145 |
?>
|
| 146 |
|