/**
  * Tests ImageResource->setResource()
  */
 public function testSetResourceFail()
 {
     try {
         $this->res->setResource(false);
         $this->fail('Expected an exception');
     } catch (ImageResourceException $ex) {
     }
 }
 public function applyFilter(ImageResource $aResource)
 {
     $x = $aResource->getX();
     $y = $aResource->getY();
     $resource = $aResource->getResource();
     // Create a new image
     $ip = imagecreatetruecolor($x, $y);
     // Loop through the whole height of the image
     for ($i = 0; $i < $y; $i++) {
         // Loop through the whole width of the image
         for ($j = 0; $j < $x; $j++) {
             // Get the pixel color ( $this->img->x - 1 needs to be there because of the imagestart @ 0,0 ;) )
             $c = imagecolorat($resource, $x - 1 - $j, $i);
             // Get the rgb values from color $c
             $r = $c >> 16 & 0xff;
             $g = $c >> 8 & 0xff;
             $b = $c & 0xff;
             // Set the color
             $clr = imagecolorallocate($ip, $r, $g, $b);
             // Set the pixel color in the new image
             imagesetpixel($ip, $j, $i, $clr);
         }
     }
     $aResource->setResource(imagecreatetruecolor($x, $y));
     imagecopy($aResource->getResource(), $ip, 0, 0, 0, 0, $x, $y);
 }
 /**
  * Applies the filter to the image resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     $function = 'imageflip';
     if (function_exists($function)) {
         $function($aResource->getResource(), $this->getMode());
     } else {
         $width = $aResource->getX();
         $height = $aResource->getY();
         $dest = imagecreatetruecolor($width, $height);
         imagealphablending($dest, false);
         imagesavealpha($dest, true);
         switch ($this->getMode()) {
             case self::FLIP_VERTICALLY:
                 $this->flipVertically($dest, $aResource->getResource(), $height, $width);
                 break;
             case self::FLIP_HORIZONTALLY:
                 $this->flipHorizontally($dest, $aResource->getResource(), $height, $width);
                 break;
             case self::FLIP_BOTH:
                 $this->flipVertically($dest, $aResource->getResource(), $height, $width);
                 $copy = imagecreatetruecolor($width, $height);
                 imagecopy($copy, $dest, 0, 0, 0, 0, $width, $height);
                 $this->flipHorizontally($dest, $copy, $height, $width);
                 break;
         }
         $aResource->setResource($dest);
     }
 }
 /**
  * Tests ImageResource->setResource()
  */
 public function testSetResourceFail()
 {
     try {
         $this->res->setResource(false);
         $this->fail('Expected an exception');
     } catch (ImageResourceException $ex) {
         $this->assertNotNull($ex->getMessage());
     }
 }
 /**
  * Applies the filter to the resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     $width = $aResource->getWidth();
     $height = $aResource->getHeight();
     $lineRes = imagecreatetruecolor($width, 1);
     $bgc = imagecolorallocatealpha($lineRes, $this->backgroundColor->getRed(), $this->backgroundColor->getGreen(), $this->backgroundColor->getBlue(), $this->backgroundColor->getAlpha());
     // Background color
     imagefilledrectangle($lineRes, 0, 0, $width, 1, $bgc);
     $rotated = $aResource->cloneResource();
     $rotateFilter = new ImageFilterRotate(180, $this->backgroundColor);
     $rotateFilter->applyFilter($rotated);
     $bg = imagecreatetruecolor($width, $this->height);
     imagecopyresampled($bg, $rotated->getResource(), 0, 0, 0, 0, $width, $height, $width, $height);
     $im = $bg;
     $bg = imagecreatetruecolor($width, $this->height);
     for ($x = 0; $x < $width; $x++) {
         imagecopy($bg, $im, $x, 0, $width - $x, 0, 1, $this->height);
     }
     $im = $bg;
     $in = 100 / $this->height;
     for ($i = 0; $i <= $this->height; $i++) {
         imagecopymerge($im, $lineRes, 0, $i, 0, 0, $width, 1, $this->startOpacity);
         if ($this->startOpacity < 100) {
             $this->startOpacity += $in;
         }
     }
     imagecopymerge($im, $lineRes, 0, 0, 0, 0, $width, $this->divLineHeight, 100);
     // Divider
     if ($this->includeOriginal) {
         // we need to include the original
         $mergeWidth = imagesx($im) + $aResource->getWidth();
         $mergeHeight = imagesy($im) + $aResource->getHeight();
         $imMerge = imagecreatetruecolor($width, $mergeHeight);
         imagecopy($imMerge, $aResource->getResource(), 0, 0, 0, 0, $width, $height);
         imagecopy($imMerge, $im, 0, $aResource->getHeight(), 0, 0, $width, $height);
         $aResource->setResource($imMerge);
     } else {
         $aResource->setResource($im);
     }
 }
 /**
  * Applies the filter to the image resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     if ($this->angle == 0 || $this->angle == 360) {
         return;
     }
     imageantialias($aResource->getResource(), true);
     $aResource->setResource(imagerotate($aResource->getResource(), $this->angle, $this->bgColor->getColorIndex(), $this->bgColor->getAlpha()));
     $new_imgres = imagecreatetruecolor($aResource->getX(), $aResource->getY());
     $success = imagecopy($new_imgres, $aResource->getResource(), 0, 0, 0, 0, $aResource->getX(), $aResource->getY());
     if (!$success) {
         throw new FilterException(self::$filterType);
     }
     imagedestroy($new_imgres);
 }
 /**
  * Applies the filter to the resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     $dest = $aResource->getResource();
     if (imageistruecolor($dest)) {
         imagetruecolortopalette($dest, false, 256);
     }
     foreach ($this->search as $search) {
         $searchRgb = new Color($search);
         $index = imagecolorclosest($aResource->getResource(), $searchRgb->getRed(), $searchRgb->getGreen(), $searchRgb->getBlue());
         // get White COlor
         imagecolorset($aResource->getResource(), $index, $this->replace->getRed(), $this->replace->getGreen(), $this->replace->getBlue());
         // SET NEW COLOR
     }
     $aResource->setResource($dest);
 }
 /**
  * Applies the filter to the resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     $orig_time_limit = ini_get('max_execution_time');
     set_time_limit(180);
     // creating the image
     $starting_img = $aResource->getResource();
     // this will be the final image, same width and height of the original
     $final = imagecreatetruecolor($aResource->getX(), $aResource->getY());
     // looping through ALL pixels!!
     for ($x = 1; $x < $aResource->getX() - 1; $x++) {
         for ($y = 1; $y < $aResource->getY() - 1; $y++) {
             // getting gray value of all surrounding pixels
             $pixel_up = $this->getLuminance(imagecolorat($starting_img, $x, $y - 1));
             $pixel_down = $this->getLuminance(imagecolorat($starting_img, $x, $y + 1));
             $pixel_left = $this->getLuminance(imagecolorat($starting_img, $x - 1, $y));
             $pixel_right = $this->getLuminance(imagecolorat($starting_img, $x + 1, $y));
             $pixel_up_left = $this->getLuminance(imagecolorat($starting_img, $x - 1, $y - 1));
             $pixel_up_right = $this->getLuminance(imagecolorat($starting_img, $x + 1, $y - 1));
             $pixel_down_left = $this->getLuminance(imagecolorat($starting_img, $x - 1, $y + 1));
             $pixel_down_right = $this->getLuminance(imagecolorat($starting_img, $x + 1, $y + 1));
             // appliying convolution mask
             $conv_x = $pixel_up_right + $pixel_right * 2 + $pixel_down_right - ($pixel_up_left + $pixel_left * 2 + $pixel_down_left);
             $conv_y = $pixel_up_left + $pixel_up * 2 + $pixel_up_right - ($pixel_down_left + $pixel_down * 2 + $pixel_down_right);
             // calculating the distance
             $gray = (int) sqrt($conv_x * $conv_x + $conv_y + $conv_y);
             // inverting the distance not to get the negative image
             $gray = 255 - $gray;
             // adjusting distance if it's greater than 255 or less than zero (out of color range)
             if ($gray > 255) {
                 $gray = 255;
             }
             if ($gray < 0) {
                 $gray = 0;
             }
             // creation of the new gray
             $new_gray = imagecolorallocate($final, $gray, $gray, $gray);
             // adding the gray pixel to the new image
             imagesetpixel($final, $x, $y, $new_gray);
         }
     }
     imagedestroy($starting_img);
     $aResource->setResource($final);
     set_time_limit($orig_time_limit);
 }
 /**
  * Applies the filter to the resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     //comic effect
     $width = $aResource->getX();
     $height = $aResource->getY();
     $imgK = imagecreatetruecolor($width, $height);
     $lowR = $this->lowcolor->getRed();
     $lowG = $this->lowcolor->getGreen();
     $lowB = $this->lowcolor->getBlue();
     $highR = $this->highcolor->getRed();
     $highG = $this->highcolor->getGreen();
     $highB = $this->highcolor->getBlue();
     for ($y = 0; $y < $height; $y++) {
         for ($x = 0; $x < $width; $x++) {
             $rgb = imagecolorat($aResource->getResource(), $x, $y);
             $r = $rgb >> 16 & 0xff;
             $g = $rgb >> 8 & 0xff;
             $b = $rgb & 0xff;
             $bw = $r + $g + $b > 300 ? imagecolorallocate($imgK, $lowR, $lowG, $lowB) : imagecolorallocate($imgK, $highR, $highG, $highB);
             imagesetpixel($imgK, $x, $y, $bw);
         }
     }
     $aResource->setResource($imgK);
 }
 /**
  * Set the resource identifier (BE *VERY* CAREFULL with this method)
  *
  * @param $aResource resource
  */
 public function setResource($aResource)
 {
     parent::setResource($aResource);
 }