]> git.andy128k.dev Git - ipf.git/commitdiff
image: replace color utility
authorAndrey Kutejko <andy128k@gmail.com>
Fri, 13 Sep 2013 13:13:05 +0000 (16:13 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Fri, 13 Sep 2013 13:13:05 +0000 (16:13 +0300)
ipf/image.php

index 539ff81f0a44ffb4fb81f61836120f0d0d89fea8..3c7164e4011db032c3e67ad174485d8950726b8a 100644 (file)
@@ -21,6 +21,9 @@ class IPF_Image
         else
             throw new Exception('Unknown image format '.$path);
 
+        imagealphablending($image, false);
+        imagesavealpha($image, true);
+
         return new IPF_Image($image, $imageInfo[0], $imageInfo[1], $type, $path);
     }
 
@@ -75,9 +78,9 @@ class IPF_Image
 
         if ($type == IMAGETYPE_JPEG)
             imagejpeg($this->image, $filename);
-        else if ($type == IMAGETYPE_GIF)
+        elseif ($type == IMAGETYPE_GIF)
             imagegif($this->image, $filename);
-        else if ($type == IMAGETYPE_PNG)
+        elseif ($type == IMAGETYPE_PNG)
             imagepng($this->image, $filename);
         else
             throw new Exception('Unknown file type.');
@@ -230,5 +233,18 @@ class IPF_Image
         $result->copyScale($this, $x, $y, $w, $h);
         return $result;
     }
+
+    public function replaceColor($from, $to)
+    {
+             $from = $this->color($from);
+             $to = $this->color($to);
+             for ($y = 0; $y < $this->height; ++$y) {
+                       for ($x = 0; $x < $this->width; ++$x) {
+                                 $at = imagecolorat($this->image, $x, $y);
+                                 if ($at == $from)
+                                         imagesetpixel($this->image, $x, $y, $to);
+                       }
+             }
+    }
 }