Example #1
0
 public function algorithm($callback)
 {
     if (!$callback instanceof imagealgorithm) {
         throw new ImageException("Callback is no class implementing " . "interface 'imagealgorithm'.");
     }
     if ($callback->option("newimg")) {
         $new = new self();
         $new->createtruecolor($this->width(), $this->height());
         $new->preparecopy($this);
     }
     $callback->setoriginalimage($this);
     if ($callback->option("newimg") && method_exists($callback, "setnewimage")) {
         $callback->setnewimage($new);
     }
     for ($x = 0; $x < $this->width(); $x++) {
         for ($y = 0; $y < $this->height(); $y++) {
             $color = $callback->algorithm($x, $y);
             if (!$callback->option("newimg") || $color == false || $color == self::undefined) {
                 continue;
             }
             if (is_numeric($color)) {
                 $color = min(255, max(0, $color));
                 $new->setpixelrgb($x, $y, array("red" => $color, "green" => $color, "blue" => $color));
                 continue;
             }
             foreach ($color as $key => $val) {
                 $color[$key] = min(255, max(0, $val));
             }
             $new->setpixelrgb($x, $y, $color);
         }
     }
     if ($callback->option("newimg")) {
         $this->sethandle($new->handle());
         $this->setpixelcache($new->getpixelcache());
         $new->close();
     }
     if ($callback->option("return")) {
         return $callback->return();
     }
     return $this;
 }