Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

CPHaxer

By: a guest on Apr 5th, 2009  |  syntax: None  |  size: 4.22 KB  |  views: 3,417  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. This PHP-Script checks a Picture of a Sky and tells you how Cloudy it is.
  2.  
  3. The best thing is, if you enter the Path to a Webcam Picture, which is often updated and where its Webcam is showing [ONLY] the Sky.
  4.  
  5. BTW: At the Blue_Color Array you can enter the RGB-Values of a Pixel of your Sky. Its optional, but if you enter it, the Analisations will be more prectise.
  6.  
  7. The Percession Variable controls which Pixels get scanned.
  8. If you have large Pictures, enter a high Number there.
  9.  
  10. <?php
  11.  
  12.   //........................................................
  13.   //: Projectname: Cloudinator
  14.   //: Description:
  15.   //:   Cloudinator is a PHP Script that analyses
  16.   //:   how cloudy the Sky is.
  17.   //:   It takes an PNG Picture and scans its Greyness.
  18.   //:   After it scanned, it takes the Message ($words)
  19.   //:   And displays it; together with the PercentNumber
  20.   //:   And a link to the Picture.
  21.   //: Author:
  22.   //:   Alexander Rath
  23.   //:   A 13-Years old guy from Germany.
  24.   //: Idea by:
  25.   //:   I dont really know who gave me dat idea, but it was
  26.   //:   here on php.net
  27.   //: @PHP-Staff:
  28.   //:   Wouldn't a PHP-Forum great?
  29.   //:   Maybe you could make one on php.net ^_^
  30.   //:
  31.   //: ~Please let the Copyright Line stay! Thx :)
  32.  
  33.   //Configuration starts .: HERE :.
  34.   $image_path = "Sky.png";
  35.   $percission = 100;
  36.   $blue_color = array("red" => 62, "green" => 120, "blue" => 168);
  37.   //Configuration ends .: HERE :.
  38.  
  39.   $image  = imagecreatefrompng($image_path);
  40.   $size   = getimagesize($image_path);
  41.  
  42.   $width  = $size[0];
  43.   $height = $size[1];
  44.  
  45.   $words  = array(0 => "The Sky is Cloudless",
  46.                   5 => "The Sky is almost Cloudless",
  47.                   10 => "The Sky is a little bit Cloudy",
  48.                   20 => "The Sky is quite Cloudy",
  49.                   30 => "There are some Clouds in the Sky",
  50.                   40 => "The Sky is Cloudy",
  51.                   50 => "The Sky contains many Clouds",
  52.                   60 => "The Sky is very Cloudy",
  53.                   70 => "The Sky is very very Cloudy",
  54.                   80 => "The Sky is almost full of Clouds",
  55.                   90 => "You cant even see the Sky anymore");
  56.  
  57.   $cloudy = 0;
  58.   $blue   = colorGreyness($blue_color);
  59.  
  60.   unset($size);
  61.  
  62.   for($x = 0; $x < $width; $x += $percission) {
  63.     for($y = 0; $y < $height; $y += $percission) {
  64.       $cloudy += colorGreyness(getColor($image, $x, $y)) - $blue;
  65.     }
  66.   }
  67.  
  68.   $cloudy = ($cloudy / (($x * $y) / ($percission * $percission))) * 450;
  69.  
  70.   $output = array();
  71.   $output["Message"] = getWord($words, $cloudy);
  72.   $output["Percent"] = $cloudy;
  73.  
  74.   function getColor($func_image, $func_x, $func_y) {
  75.     $func_color = ImageColorAt($func_image, $func_x, $func_y);
  76.     return ImageColorsForIndex($func_image, $func_color);
  77.   }
  78.  
  79.   function getDifference($func_A, $func_B) {
  80.     if($func_A > $func_B) return $func_A - $func_B;
  81.     return $func_B - $func_A;
  82.   }
  83.  
  84.   function colorGreyness($func_color) {
  85.     $func_grey        = ($func_color["red"] + $func_color["green"] + $func_color["blue"]) / 3;
  86.    
  87.     $func_difference  = 0;
  88.     $func_difference += getDifference($func_color["red"], $func_grey);
  89.     $func_difference += getDifference($func_color["green"], $func_grey);
  90.     $func_difference += getDifference($func_color["blue"], $func_grey);
  91.    
  92.     $func_greyness    = (510 - $func_difference) / 510;
  93.    
  94.     return $func_greyness;
  95.   }
  96.  
  97.   function getWord($func_words, $func_number) {
  98.     $func_return = "No Message Found";
  99.    
  100.     foreach($func_words as $func_min => $func_word) {
  101.       if($func_min <= $func_number) $func_return = $func_word;
  102.     }
  103.    
  104.     return $func_return;
  105.   }
  106.  
  107. ?>
  108. <html>
  109.   <head>
  110.     <title>Cloudinator :: Result</title>
  111.   </head>
  112.   <body>
  113.     <font face="Tahoma">
  114.       <small>
  115.         <div style="width:200px;">
  116.           <b><?php echo $output["Message"]; ?></b><br>
  117.           <div align="right"><?php echo "<small>The Sky is " . $output["Percent"] . "% Cloudy</small>"; ?></div><br>
  118.           If you want to see the Picture of the Sky, click <a href="<?php echo $image_path; ?>">here</a>.<br><br>
  119.           <small><i>&copy;Copyright 2009 <b>Alexander Rath</b></i></small>
  120.         </div>
  121.       </small>
  122.     </font>
  123.   </body>
  124. </html>