]> git.wh0rd.org - tt-rss.git/commitdiff
try to improve color guessing algorithm a bit
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Mon, 15 Apr 2013 13:01:51 +0000 (17:01 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Mon, 15 Apr 2013 13:01:51 +0000 (17:01 +0400)
include/colors.php
include/functions.php
include/rssfuncs.php

index d1e970728a272c152d9b8da746b615948775b6d3..19c8915177473b3cdc007f02d29a6e5014d1f75d 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+require_once "lib/floIcon.php";
+
 function _resolve_htmlcolor($color) {
        $htmlcolors = array ("aliceblue" => "#f0f8ff",
                "antiquewhite" => "#faebd7",
@@ -278,4 +280,62 @@ function hsl2rgb($arr) {
     return array($r, $g, $B);
 }
 
+       function colorPalette($imageFile, $numColors, $granularity = 5) {
+          $granularity = max(1, abs((int)$granularity));
+          $colors = array();
+
+               $size = @getimagesize($imageFile);
+
+               if (strtolower($size['mime']) == 'image/vnd.microsoft.icon') {
+                       $ico = new floIcon();
+                       @$ico->readICO($imageFile);
+
+                       if(count($ico->images)==0)
+                               return null;
+                       else
+                               $img = @$ico->images[count($ico->images)-1]->getImageResource();
+
+               } else {
+                  $img = @imagecreatefromstring(file_get_contents($imageFile));
+               }
+
+               if (!$img) return false;
+
+          for($x = 0; $x < $size[0]; $x += $granularity) {
+             for($y = 0; $y < $size[1]; $y += $granularity) {
+                $thisColor = imagecolorat($img, $x, $y);
+                $rgb = imagecolorsforindex($img, $thisColor);
+                $red = round(round(($rgb['red'] / 0x33)) * 0x33);
+                $green = round(round(($rgb['green'] / 0x33)) * 0x33);
+                $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
+                $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
+                if(array_key_exists($thisRGB, $colors)) {
+                   $colors[$thisRGB]++;
+                } else{
+                   $colors[$thisRGB] = 1;
+                }
+             }
+               }
+
+          arsort($colors);
+          return array_slice(array_keys($colors), 0, $numColors);
+       }
+
+       function calculate_avg_color($iconFile) {
+               $palette = colorPalette($iconFile, 4, 4);
+
+               if (is_array($palette)) {
+                       foreach ($palette as $p) {
+                               $hsl = rgb2hsl(_color_unpack("#$p"));
+
+                               if ($hsl[1] > 0.25 && $hsl[2] > 0.25 &&
+                                       !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[1] < 0.01) &&
+                                       !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[2] > 0.99)) {
+
+                                       return _color_pack(hsl2rgb($hsl));
+                               }
+                       }
+               }
+               return false;
+       }
 ?>
index 6372db1fd8e95d5d8dadb85879f2f10f47c99b34..306014d96e272f302eef5924df9bd26104945b51 100644 (file)
                }
        }
 
-       function calculate_avg_color($iconFile) {
-
-               require_once "lib/floIcon.php";
-
-               $imgInfo = @getimagesize($iconFile);
-
-               if(strtolower($imgInfo['mime'])=='image/vnd.microsoft.icon') {
-                       $ico = new floIcon();
-                       @$ico->readICO($iconFile);
-                       //TODO: error logging
-                       if(count($ico->images)==0)
-                               return null;
-                       else {
-                               $image = @$ico->images[count($ico->images)-1]->getImageResource();
-                       }
-                       $type = "ico";
-                       }
-               elseif(strtolower($imgInfo['mime'])=='image/png') {
-            $image = imagecreatefrompng($iconFile);
-                       $type = 'png';
-               }
-               elseif(strtolower($imgInfo['mime'])=='image/jpeg') {
-                       $image = imagecreatefromjpeg($iconFile);
-                       $type = 'jpg';
-               }
-               elseif(strtolower($imgInfo['mime'])=='image/gif') {
-                       $image = imagecreatefromgif($iconFile);
-                       $type = 'gif';
-               }
-               //TODO: error logging
-               if (is_null($image))
-                       return null;
-               $width = imagesx($image);
-               $height = imagesy($image);
-               $pixel = imagecreatetruecolor(1, 1);
-               imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
-               $rgb = imagecolorat($pixel, 0, 0);
-               $color = imagecolorsforindex($pixel, $rgb);
-               return $color;
-       }
-
        function print_select($id, $default, $values, $attributes = "") {
                print "<select name=\"$id\" id=\"$id\" $attributes>";
                foreach ($values as $v) {
index 2e522175ecffd3e9ce0b08f804d1de93481103f0..0d55dce36bf513f7a3737626bb4a03e0dcbd518e 100644 (file)
                                $favicon_file = ICONS_DIR . "/$feed.ico";
 
                                if (file_exists($favicon_file)) {
-                                           $favicon_color = calculate_avg_color($favicon_file);
-
                                                 require_once "colors.php";
 
-                                                if (is_array($favicon_color))
-                                                               $tmp = array($favicon_color['red'],
-                                                                       $favicon_color['green'],
-                                                                       $favicon_color['blue']);
+                                                $favicon_color = db_escape_string($link,
+                                                        calculate_avg_color($favicon_file));
 
-                                                                $favicon_colorstring = ",favicon_avg_color = '" .
-                                                               _color_pack($tmp) . "'";
-                }
+                                                $favicon_colorstring = ",favicon_avg_color = '".$favicon_color."'";
+                               }
 
                                db_query($link, "UPDATE ttrss_feeds SET favicon_last_checked = NOW() $favicon_colorstring
                                        WHERE id = '$feed'");