[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/plugins/imageflow/ -> reflect2.php (source)

   1  <?php
   2      /*

   3          ----------------------------------------------------------------

   4          Easy Reflections by Richard Davey, Core PHP (rich@corephp.co.uk)

   5          v2 - 2nd March 2007

   6          Updates include changes by Monte Ohrt (monte@ohrt.com)

   7          ----------------------------------------------------------------

   8          You are free to use this in any product, or on any web site.

   9          Latest builds at: http://reflection.corephp.co.uk

  10          ----------------------------------------------------------------

  11          

  12          This script accepts the following $_GET parameters:

  13          

  14          img                required    The source image (to reflect)

  15          height            optional    Height of the reflection (% or pixel value)

  16          bgc                optional    Background colour to fade into, default = #000000

  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          jpeg            optional    Output will be JPEG at 'param' quality (default 80)

  20          cache           optional    Save reflection image to the cache? (boolean)

  21      */
  22      
  23      // Replace special chars to be HTML-Code

  24  	function stringToHTML($string)
  25      {
  26          $array_search = array('é', 'è', 'ë', 'ê', 'à', 'ä', 'Ä', 'â', 'ù', 'ü', 'Ü', 'û', 'ö', 'Ö', 'ô', 'ï', 'î');
  27          $array_replace = array('&eacute;', '&egrave;',    '&euml;', '&ecirc;', '&agrave;', '&auml;', '&Auml;', '&acirc;', '&ugrave;', '&uuml;', '&Uuml;', '&ucirc;', '&ouml;', '&Ouml;', '&ocirc;', '&iuml;', '&icirc;');
  28          $string_return = str_replace($array_search, $array_replace, $string);
  29          return $string_return;
  30      }
  31  
  32      //    PHP Version sanity check

  33      if (version_compare('4.3.2', phpversion()) == 1)
  34      {
  35          echo 'This version of PHP is not fully supported. You need 4.3.2 or above.';
  36          exit();
  37      }
  38      
  39      //    GD check

  40      if (extension_loaded('gd') == false && !dl('gd.so'))
  41      {
  42          echo 'You are missing the GD extension for PHP, sorry but I cannot continue.';
  43          exit();
  44      }
  45      
  46      //    Our allowed query string parameters

  47  
  48      //  To cache or not to cache? that is the question

  49      if (isset($_GET['cache']))
  50      {
  51          if ((int) $_GET['cache'] == 1)
  52          {
  53              $cache = true;
  54          }
  55          else
  56          {
  57              $cache = false;
  58          }
  59      }
  60      else
  61      {
  62          $cache = true;
  63      }
  64  
  65      //    img (the image to reflect)

  66      if (isset($_GET['img']))
  67      {
  68          $source_image = $_GET['img'];
  69  
  70          //$source_image = utf8_decode($source_image);

  71  
  72          $source_image = str_replace('://','',$source_image);
  73          //$source_image = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $source_image;

  74          
  75          if (file_exists($source_image))
  76          {
  77              if ($cache)
  78              {
  79                  $cache_dir = dirname($source_image);
  80                  $cache_base = basename($source_image);
  81                  $cache_file = 'refl_' . md5($_SERVER['REQUEST_URI']) . '_' . $cache_base;
  82                  $cache_path = $cache_dir . DIRECTORY_SEPARATOR . $cache_file;
  83  
  84                  if (file_exists($cache_path) && filemtime($cache_path) >= filemtime($source_image))
  85                  {
  86                      // Use cached image

  87                      $image_info = getimagesize($cache_path);
  88                      header("Content-type: " . $image_info['mime']);
  89                      readfile($cache_path);
  90                      exit();
  91                  }
  92              }
  93          }
  94          else
  95          {
  96            echo 'Cannot find or read source image';
  97            exit();
  98          }
  99      }
 100      else
 101      {
 102          echo 'No source image to reflect supplied';
 103          exit();
 104      }
 105  
 106      //    bgc (the background colour used, defaults to black if not given)

 107      if (isset($_GET['bgc']) == false)
 108      {
 109          $red = 0;
 110          $green = 0;
 111          $blue = 0;
 112      }
 113      else
 114      {
 115          //    Extract the hex colour

 116          $hex_bgc = $_GET['bgc'];
 117          
 118          //    Does it start with a hash? If so then strip it

 119          $hex_bgc = str_replace('#', '', $hex_bgc);
 120          
 121          switch (strlen($hex_bgc))
 122          {
 123              case 6:
 124                  $red = hexdec(substr($hex_bgc, 0, 2));
 125                  $green = hexdec(substr($hex_bgc, 2, 2));
 126                  $blue = hexdec(substr($hex_bgc, 4, 2));
 127                  break;
 128                  
 129              case 3:
 130                  $red = substr($hex_bgc, 0, 1);
 131                  $green = substr($hex_bgc, 1, 1);
 132                  $blue = substr($hex_bgc, 2, 1);
 133                  $red = hexdec($red . $red);
 134                  $green = hexdec($green . $green);
 135                  $blue = hexdec($blue . $blue);
 136                  break;
 137                  
 138              default:
 139                  //    Wrong values passed, default to black

 140                  $red = 0;
 141                  $green = 0;
 142                  $blue = 0;
 143          }
 144      }
 145      
 146      //    height (how tall should the reflection be?)

 147      if (isset($_GET['height']))
 148      {
 149          $output_height = $_GET['height'];
 150          
 151          //    Have they given us a percentage?

 152          if (substr($output_height, -1) == '%')
 153          {
 154              //    Yes, remove the % sign

 155              $output_height = (int) substr($output_height, 0, -1);
 156  
 157              //    Gotta love auto type casting ;)

 158              if ($output_height < 10)
 159              {
 160                  $output_height = "0.0$output_height";
 161              }
 162              else
 163              {
 164                  $output_height = "0.$output_height";
 165              }
 166          }
 167          else
 168          {
 169              $output_height = (int) $output_height;
 170          }
 171      }
 172      else
 173      {
 174          //    No height was given, so default to 50% of the source images height

 175          $output_height = 0.50;
 176      }
 177      
 178      if (isset($_GET['fade_start']))
 179      {
 180          if (strpos($_GET['fade_start'], '%') !== false)
 181          {
 182              $alpha_start = str_replace('%', '', $_GET['fade_start']);
 183              $alpha_start = (int) (127 * $alpha_start / 100);
 184          }
 185          else
 186          {
 187              $alpha_start = (int) $_GET['fade_start'];
 188          
 189              if ($alpha_start < 1 || $alpha_start > 127)
 190              {
 191                  $alpha_start = 80;
 192              }
 193          }
 194      }
 195      else
 196      {
 197          $alpha_start = 80;
 198      }
 199  
 200      if (isset($_GET['fade_end']))
 201      {
 202          if (strpos($_GET['fade_end'], '%') !== false)
 203          {
 204              $alpha_end = str_replace('%', '', $_GET['fade_end']);
 205              $alpha_end = (int) (127 * $alpha_end / 100);
 206          }
 207          else
 208          {
 209              $alpha_end = (int) $_GET['fade_end'];
 210          
 211              if ($alpha_end < 1 || $alpha_end > 0)
 212              {
 213                  $alpha_end = 0;
 214              }
 215          }
 216      }
 217      else
 218      {
 219          $alpha_end = 0;
 220      }
 221  
 222      /*

 223          ----------------------------------------------------------------

 224          Ok, let's do it ...

 225          ----------------------------------------------------------------

 226      */
 227      
 228      //    How big is the image?

 229      $image_details = getimagesize($source_image);
 230      
 231      if ($image_details === false)
 232      {
 233          echo 'Not a valid image supplied, or this script does not have permissions to access it.';
 234          exit();
 235      }
 236      else
 237      {
 238          $width = $image_details[0];
 239          $height = $image_details[1];
 240          $type = $image_details[2];
 241          $mime = $image_details['mime'];
 242      }
 243      
 244      //    Calculate the height of the output image

 245      if ($output_height < 1)
 246      {
 247          //    The output height is a percentage

 248          $new_height = $height * $output_height;
 249      }
 250      else
 251      {
 252          //    The output height is a fixed pixel value

 253          $new_height = $output_height;
 254      }
 255  
 256      //    Detect the source image format - only GIF, JPEG and PNG are supported. If you need more, extend this yourself.

 257      switch ($type)
 258      {
 259          case 1:
 260              //    GIF

 261              $source = imagecreatefromgif($source_image);
 262              break;
 263              
 264          case 2:
 265              //    JPG

 266              $source = imagecreatefromjpeg($source_image);
 267              break;
 268              
 269          case 3:
 270              //    PNG

 271              $source = imagecreatefrompng($source_image);
 272              break;
 273              
 274          default:
 275              echo 'Unsupported image file format.';
 276              exit();
 277      }
 278  
 279  
 280      /*

 281          ----------------------------------------------------------------

 282          Build the reflection image

 283          ----------------------------------------------------------------

 284      */
 285  
 286      //    We'll store the final reflection in $output. $buffer is for internal use.

 287      $output = imagecreatetruecolor($width, $new_height);
 288      $buffer = imagecreatetruecolor($width, $new_height);
 289  
 290      //    Copy the bottom-most part of the source image into the output

 291      imagecopy($output, $source, 0, 0, 0, $height - $new_height, $width, $new_height);
 292      
 293      //    Rotate and flip it (strip flip method)

 294      for ($y = 0; $y < $new_height; $y++)
 295      {
 296         imagecopy($buffer, $output, 0, $y, 0, $new_height - $y - 1, $width, 1);
 297      }
 298  
 299      $output = $buffer;
 300      
 301      /*

 302          ----------------------------------------------------------------

 303          Apply the fade effect

 304          ----------------------------------------------------------------

 305      */
 306      
 307      //    This is quite simple really. There are 127 available levels of alpha, so we just

 308      //    step-through the reflected image, drawing a box over the top, with a set alpha level.

 309      //    The end result? A cool fade into the background colour given.

 310  
 311      //    There are a maximum of 127 alpha fade steps we can use, so work out the alpha step rate

 312  
 313      $alpha_length = abs($alpha_start - $alpha_end);
 314  
 315      for ($y = 0; $y <= $new_height; $y++)
 316      {
 317          //  Get % of reflection height

 318          $pct = $y / $new_height;
 319  
 320          //  Get % of alpha

 321          if ($alpha_start > $alpha_end)
 322          {
 323              $alpha = (int) ($alpha_start - ($pct * $alpha_length));
 324          }
 325          else
 326          {
 327              $alpha = (int) ($alpha_start + ($pct * $alpha_length));
 328          }
 329  
 330          imagefilledrectangle($output, 0, $y, $width, $y, imagecolorallocatealpha($output, $red, $green, $blue, $alpha));
 331          
 332      }
 333  
 334          
 335      /*

 336          ----------------------------------------------------------------

 337          HACK - Build the reflection image by combining the source 

 338          image AND the reflection in one new image!

 339          ----------------------------------------------------------------

 340      */
 341          $finaloutput = imagecreatetruecolor($width, $height+$new_height);
 342          imagecopy($finaloutput, $source, 0, 0, 0, 0, $width, $height);
 343          imagecopy($finaloutput, $output, 0, $height, 0, 0, $width, $new_height);
 344          $output = $finaloutput;
 345  
 346      /*

 347          ----------------------------------------------------------------

 348          Output our final PNG

 349          ----------------------------------------------------------------

 350      */
 351  
 352      if (headers_sent())
 353      {
 354          echo 'Headers already sent, I cannot display an image now. Have you got an extra line-feed in this file somewhere?';
 355          exit();
 356      }
 357      else
 358      {
 359          //    If you'd rather output a JPEG instead of a PNG then pass the parameter 'jpeg' (no value needed) on the querystring

 360  
 361          if (isset($_GET['png']))
 362          {
 363              //    PNG

 364              header("Content-type: image/png");
 365              imagepng($output);
 366  
 367              // Save cached file

 368              if ($cache)
 369              {
 370                  imagepng($output, $cache_path);
 371              }
 372              
 373          }
 374          else
 375          {
 376              if(!isset( $_GET['jpeg']))  $_GET['jpeg'] = 90;
 377              
 378              $quality = (int) $_GET['jpeg'];
 379              
 380              if ($quality < 1 || $quality > 100)
 381              {
 382                  $quality = 90;
 383              }
 384              
 385              //    JPEG (the final parameter = the quality, 0 = terrible, 100 = pixel perfect)

 386              header("Content-type: image/jpeg");
 387              imagejpeg($output, '', $quality);
 388  
 389              // Save cached file

 390              if ($cache)
 391              {
 392                  imagejpeg($output, $cache_path, $quality);
 393              }
 394              
 395             
 396          }
 397  
 398          imagedestroy($output);
 399          exit();
 400      }
 401  ?>


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7