| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 ------------------------------------------------------------------- 4 Easy Reflections v3 by Richard Davey, Core PHP (rich@corephp.co.uk) 5 Released 13th March 2007 6 Includes code submissions from Monte Ohrt (monte@ohrt.com) 7 ------------------------------------------------------------------- 8 You are free to use this in any product, or on any web site. 9 I'd appreciate it if you email and tell me where you use it, thanks. 10 Latest builds at: http://reflection.corephp.co.uk 11 ------------------------------------------------------------------- 12 13 This script accepts the following $_GET parameters: 14 15 img required The source image to reflect 16 height optional Height of the reflection (% or pixel value) 17 fade_start optional Start the alpha fade from whch value? (% value) 18 fade_end optional End the alpha fade from whch value? (% value) 19 cache optional Save reflection image to the cache? (boolean) 20 tint optional Tint the reflection with this colour (hex) 21 */ 22 23 // PHP Version sanity check 24 if (version_compare('4.3.2', phpversion()) == 1) 25 { 26 echo 'This version of PHP is not fully supported. You need 4.3.2 or above.'; 27 exit(); 28 } 29 30 // GD check 31 if (extension_loaded('gd') == false && !dl('gd.so')) 32 { 33 echo 'You are missing the GD extension for PHP, sorry but I cannot continue.'; 34 exit(); 35 } 36 37 // GD Version check 38 $gd_info = gd_info(); 39 40 if ($gd_info['PNG Support'] == false) 41 { 42 echo 'This version of the GD extension cannot output PNG images.'; 43 exit(); 44 } 45 46 if (ereg_replace('[[:alpha:][:space:]()]+', '', $gd_info['GD Version']) < '2.0.1') 47 { 48 echo 'GD library is too old. Version 2.0.1 or later is required, and 2.0.28 is strongly recommended.'; 49 exit(); 50 } 51 52 // Our allowed query string parameters 53 54 // To cache or not to cache? that is the question 55 if (isset($_GET['cache'])) 56 { 57 58 if ((int) $_GET['cache'] == 1) 59 { 60 61 $cache = true; 62 } 63 else 64 { 65 $cache = false; 66 } 67 } 68 else 69 { 70 $cache = true; 71 } 72 73 // img (the image to reflect) 74 if (isset($_GET['img'])) 75 { 76 $source_image = $_GET['img']; 77 $source_image = str_replace('://','',$source_image); 78 //$source_image = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $source_image; 79 80 if (file_exists($source_image)) 81 { 82 if ($cache) 83 { 84 $cache_dir = dirname($source_image); 85 $cache_base = basename($source_image); 86 $cache_file = 'refl_' . md5($_SERVER['REQUEST_URI']) . '_' . $cache_base; 87 $cache_path = $cache_dir . DIRECTORY_SEPARATOR . $cache_file; 88 89 if (file_exists($cache_path) && filemtime($cache_path) >= filemtime($source_image)) 90 { 91 // Use cached image 92 $image_info = getimagesize($cache_path); 93 header("Content-type: " . $image_info['mime']); 94 readfile($cache_path); 95 exit(); 96 } 97 } 98 } 99 else 100 { 101 echo 'Cannot find or read source image'; 102 exit(); 103 } 104 } 105 else 106 { 107 echo 'No source image to reflect supplied'; 108 exit(); 109 } 110 111 // tint (the colour used for the tint, defaults to white if not given) 112 113 if (isset($_GET['tint']) == false) 114 { 115 $red = 127; 116 $green = 127; 117 $blue = 127; 118 119 120 121 } 122 else 123 { 124 // Extract the hex colour 125 $hex_bgc = $_GET['tint']; 126 127 // Does it start with a hash? If so then strip it 128 $hex_bgc = str_replace('#', '', $hex_bgc); 129 130 switch (strlen($hex_bgc)) 131 { 132 case 6: 133 $red = hexdec(substr($hex_bgc, 0, 2)); 134 $green = hexdec(substr($hex_bgc, 2, 2)); 135 $blue = hexdec(substr($hex_bgc, 4, 2)); 136 break; 137 138 case 3: 139 $red = substr($hex_bgc, 0, 1); 140 $green = substr($hex_bgc, 1, 1); 141 $blue = substr($hex_bgc, 2, 1); 142 $red = hexdec($red . $red); 143 $green = hexdec($green . $green); 144 $blue = hexdec($blue . $blue); 145 break; 146 147 default: 148 // Wrong values passed, default to white 149 $red = 127; 150 $green = 127; 151 $blue = 127; 152 } 153 } 154 155 // height (how tall should the reflection be?) 156 if (isset($_GET['height'])) 157 { 158 $output_height = $_GET['height']; 159 160 // Have they given us a percentage? 161 if (substr($output_height, -1) == '%') 162 { 163 // Yes, remove the % sign 164 $output_height = (int) substr($output_height, 0, -1); 165 166 // Gotta love auto type casting ;) 167 if ($output_height == 100) 168 { 169 $output_height = "0.99"; 170 } 171 elseif ($output_height < 10) 172 { 173 $output_height = "0.0$output_height"; 174 } 175 else 176 { 177 $output_height = "0.$output_height"; 178 } 179 } 180 else 181 { 182 $output_height = (int) $output_height; 183 } 184 } 185 else 186 { 187 // No height was given, so default to 50% of the source images height 188 $output_height = 0.50; 189 } 190 191 if (isset($_GET['fade_start'])) 192 { 193 if (strpos($_GET['fade_start'], '%') !== false) 194 { 195 $alpha_start = str_replace('%', '', $_GET['fade_start']); 196 $alpha_start = (int) (127 * $alpha_start / 100); 197 } 198 else 199 { 200 $alpha_start = (int) $_GET['fade_start']; 201 202 if ($alpha_start < 1 || $alpha_start > 127) 203 { 204 $alpha_start = 80; 205 } 206 } 207 } 208 else 209 { 210 $alpha_start = 80; 211 } 212 213 if (isset($_GET['fade_end'])) 214 { 215 if (strpos($_GET['fade_end'], '%') !== false) 216 { 217 $alpha_end = str_replace('%', '', $_GET['fade_end']); 218 $alpha_end = (int) (127 * $alpha_end / 100); 219 } 220 else 221 { 222 $alpha_end = (int) $_GET['fade_end']; 223 224 if ($alpha_end < 1 || $alpha_end > 0) 225 { 226 $alpha_end = 0; 227 } 228 } 229 } 230 else 231 { 232 $alpha_end = 0; 233 } 234 235 /* 236 ---------------------------------------------------------------- 237 Ok, let's do it ... 238 ---------------------------------------------------------------- 239 */ 240 241 // How big is the image? 242 $image_details = getimagesize($source_image); 243 244 if ($image_details === false) 245 { 246 echo 'Not a valid image supplied, or this script does not have permissions to access it.'; 247 exit(); 248 } 249 else 250 { 251 $width = $image_details[0]; 252 $height = $image_details[1]; 253 $type = $image_details[2]; 254 $mime = $image_details['mime']; 255 } 256 257 // Calculate the height of the output image 258 if ($output_height < 1) 259 { 260 // The output height is a percentage 261 $new_height = $height * $output_height; 262 } 263 else 264 { 265 // The output height is a fixed pixel value 266 $new_height = $output_height; 267 } 268 269 // Detect the source image format - only GIF, JPEG and PNG are supported. If you need more, extend this yourself. 270 switch ($type) 271 { 272 case 1: 273 // GIF 274 $source = imagecreatefromgif($source_image); 275 break; 276 277 case 2: 278 // JPG 279 $source = imagecreatefromjpeg($source_image); 280 break; 281 282 case 3: 283 // PNG 284 $source = imagecreatefrompng($source_image); 285 break; 286 287 default: 288 echo 'Unsupported image file format.'; 289 exit(); 290 } 291 292 /* 293 ---------------------------------------------------------------- 294 Build the reflection image 295 ---------------------------------------------------------------- 296 */ 297 298 299 300 301 // We'll store the final reflection in $output. $buffer is for internal use. 302 $output = imagecreatetruecolor($width, $new_height); 303 $buffer = imagecreatetruecolor($width, $new_height); 304 305 // Save any alpha data that might have existed in the source image and disable blending 306 imagesavealpha($source, true); 307 308 imagesavealpha($output, true); 309 imagealphablending($output, false); 310 311 imagesavealpha($buffer, true); 312 imagealphablending($buffer, false); 313 314 // Copy the bottom-most part of the source image into the output 315 imagecopy($output, $source, 0, 0, 0, $height - $new_height, $width, $new_height); 316 317 // Rotate and flip it (strip flip method) 318 for ($y = 0; $y < $new_height; $y++) 319 { 320 imagecopy($buffer, $output, 0, $y, 0, $new_height - $y - 1, $width, 1); 321 } 322 323 $output = $buffer; 324 325 /* 326 ---------------------------------------------------------------- 327 Apply the fade effect 328 ---------------------------------------------------------------- 329 */ 330 331 // This is quite simple really. There are 127 available levels of alpha, so we just 332 // step-through the reflected image, drawing a box over the top, with a set alpha level. 333 // The end result? A cool fade. 334 335 // There are a maximum of 127 alpha fade steps we can use, so work out the alpha step rate 336 337 $alpha_length = abs($alpha_start - $alpha_end); 338 339 imagelayereffect($output, IMG_EFFECT_OVERLAY); 340 341 for ($y = 0; $y <= $new_height; $y++) 342 { 343 // Get % of reflection height 344 $pct = $y / $new_height; 345 346 // Get % of alpha 347 if ($alpha_start > $alpha_end) 348 { 349 $alpha = (int) ($alpha_start - ($pct * $alpha_length)); 350 } 351 else 352 { 353 $alpha = (int) ($alpha_start + ($pct * $alpha_length)); 354 } 355 356 // Rejig it because of the way in which the image effect overlay works 357 $final_alpha = 127 - $alpha; 358 359 //imagefilledrectangle($output, 0, $y, $width, $y, imagecolorallocatealpha($output, 127, 127, 127, $final_alpha)); 360 imagefilledrectangle($output, 0, $y, $width, $y, imagecolorallocatealpha($output, $red, $green, $blue, $final_alpha)); 361 } 362 363 /* 364 ---------------------------------------------------------------- 365 HACK - Build the reflection image by combining the source 366 image AND the reflection in one new image! 367 ---------------------------------------------------------------- 368 */ 369 $finaloutput = imagecreatetruecolor($width, $height+$new_height); 370 imagesavealpha($finaloutput, true); 371 372 $trans_colour = imagecolorallocatealpha($finaloutput, 0, 0, 0, 127); 373 imagefill($finaloutput, 0, 0, $trans_colour); 374 375 imagecopy($finaloutput, $source, 0, 0, 0, 0, $width, $height); 376 imagecopy($finaloutput, $output, 0, $height, 0, 0, $width, $new_height); 377 $output = $finaloutput; 378 379 /* 380 ---------------------------------------------------------------- 381 Output our final PNG 382 ---------------------------------------------------------------- 383 */ 384 if (headers_sent()) 385 { 386 echo 'Headers already sent, I cannot display an image now. Have you got an extra line-feed in this file somewhere?'; 387 exit(); 388 } 389 else 390 { 391 // PNG 392 header("Content-type: image/png"); 393 imagepng($output); 394 395 // Save cached file 396 if ($cache) 397 { 398 imagepng($output, $cache_path); 399 } 400 401 imagedestroy($output); 402 exit(); 403 } 404 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |