PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/_squelettes_/IENSP/iensp-x_vPP_ImageFlow/javascript/ImageFlow/reflect3.php

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