/*
Apply effects on an image

Parameters
resource $resource	: image resource
integer $level		: level

Return
resource
517885
public function apply($resource){
		// Extract arguments
		@list(,$level)=func_get_args();
		$level=abs((int)$level);
		if(!$level){
		    $level=5;
		}
		// Get resolution
		$width=imagesx($resource);
		$height=imagesy($resource);
		if(!$width || !$height){
			throw new Exception("An error was encountered while getting image resolution");
		}
		// Apply effect
		$x=0;
		do{
			$y=0;
			do{
				// Get current pixel color
				list($r,$g,$b,$a)=$this->_getColorAt($resource,$x,$y);
				// Generate noise
				$z=rand(-$level,$level);
				// Define color
				$r+=$z;
				$g+=$z;
				$b+=$z;
				if($r<0)		$r=0;
				elseif($r>255)	$r=255;
				if($g<0)		$g=0;
				elseif($g>255)	$g=255;
				if($b<0)		$b=0;
				elseif($b>255)	$b=255;
				// Define new pixel
				imagesetpixel($resource,$x,$y,imagecolorallocatealpha($resource,$r,$g,$b,$a));
			}
			while(++$y<$height);
		}
		while(++$x<$width);
		return $resource;
	}
744529
public function apply($resource){
		// Verify support
		if(!function_exists('imagefilter')){
			throw new Exception("It seems your PHP version is not compiled with the bundled version of the GD library");
		}
		// Extract arguments
		@list(,$method)=func_get_args();
		// Apply effect
		switch($method){
			case self::GAUSSIAN:
				if(!imagefilter($resource,IMG_FILTER_GAUSSIAN_BLUR)){
					throw new Exception("GAUSSIAN_BLUR filter failed");
				}
				break;
			case self::SELECTIVE:
			default:
				if(!imagefilter($resource,IMG_FILTER_SELECTIVE_BLUR)){
					throw new Exception("SELECTIVE_BLUR filter failed");
				}
		}
		return $resource;
	}
771133
public static function load($color, $resource)
    {
        if ($color === 'transparent') {

            return imagecolorallocatealpha($resource, 0, 0, 0, 127);
        }

        $gdpixel = new GDPixel($color);

        return imagecolorallocate($resource, $gdpixel->channelR, $gdpixel->channelG, $gdpixel->channelB);
    }
981250
protected function applyWatermarkGD($resource, Picture $picture) {

        // SOURCE IMAGE DIMENSIONS
        $pictureWidth  = imagesx($resource);
        $pictureHeight = imagesy($resource);

        // WATERMARK DIMENSIONS
        $watermarkPicture = new Picture($this->watermark, NULL, Picture::WORKER_GD);

        $opacity = new Opacity($this->opacity);
        $opacity->apply($watermarkPicture);

        if($this->size)
        {
            $resize = new Resize(
                $pictureWidth / 100 * $this->size,
                $pictureHeight / 100 * $this->size,
                Resize::MODE_FIT
            );

            $resize->apply($watermarkPicture);
        }

        $watermark = $watermarkPicture->getResource($watermarkPicture::WORKER_GD);

        imagealphablending($watermark, TRUE);

        $watermarkWidth  = imagesx($watermark);
        $watermarkHeight = imagesx($watermark);

        // CALCULATE WATERMARK POSITION
        switch($this->position)
        {
            case 'repeat':

                for($w = 0; $w < $pictureWidth; $w += $watermarkWidth + $this->space)
                {
                    for($h = 0; $h < $pictureHeight; $h += $watermarkHeight + $this->space)
                    {
                        imagecopy($resource, $watermark, $w, $h, 0, 0, $watermarkWidth, $watermarkHeight);
                    }
                }

                return $resource;

            case 'center':

                $positionX = ($pictureWidth - $watermarkWidth) / 2 - $this->offsetX;
                $positionY = ($pictureHeight - $watermarkHeight) / 2 - $this->offsetY;
                break;

            case 'topRight':

                $positionX = $pictureWidth - $watermarkWidth - $this->offsetX;
                $positionY = $this->offsetY;
                break;

            case 'bottomRight':

                $positionX = $pictureWidth  - $watermarkWidth  - $this->offsetX;
                $positionY = $pictureHeight - $watermarkHeight - $this->offsetY;
                break;

            case 'bottomLeft':

                $positionX = $this->offsetX;
                $positionY = $pictureHeight - $watermarkHeight - $this->offsetY;
                break;

            default:

                $positionX = $this->offsetX;
                $positionY = $this->offsetY;
                break;
        }

        imagecopy($resource, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight);

        return $resource;
    }
1114186
function brightness($level) {
        imagefilter($this->image, IMG_FILTER_BRIGHTNESS, $this->keep_within($level, -255, 255));
        return $this;
    }
601094
public function apply($resource){
		// Extract arguments
		@list(,$matrix,$offset)=func_get_args();
		$matrix=(array)$matrix;
		$offset=(int)$offset;
		// Verify matrix
		if(count($matrix)!=9){
			throw new Exception("The array parameter is a 3x3 matrix and must so contain 9 rows");
		}
		// Normalization
		$matrix=array(
			array($matrix[0],$matrix[1],$matrix[2]),
			array($matrix[3],$matrix[4],$matrix[5]),
			array($matrix[6],$matrix[7],$matrix[8])
		);
		// Compute normalization divisor
		$div=array_sum($matrix[0])+array_sum($matrix[1])+array_sum($matrix[2]);
		// Apply effect
		if(!imageconvolution($resource,$matrix,$div,$offset)){
			throw new Exception("Image convolution failed");
		}
		return $resource;
	}
664041
public function apply(Picture $picture)
    {
        $resource = $picture->getResource();

        if($picture->isGd())
        {
            imagefilter($resource, IMG_FILTER_GRAYSCALE);

            $picture->setResource($resource);
        }
        else
        {
            $resource->modulateImage(100, 0, 100);
        }
    }
736062
def apply(self, resource):
        
        if not isinstance(resource, Image.Image):
            raise ValueError('Unsupported resource format: %s' % str(type(resource)))

        original_width, original_height = resource.size

        if original_width > self.size[0] or original_height > self.size[1]:
            k = original_width / original_height

            if original_width >= original_height:
                target_width = self.size[0]
                target_height = int(target_width / k)
            else:
                target_height = self.size[1]
                target_width = int(target_height * k)

            resource_format = resource.format
            resource = resource.resize((target_width, target_height), Image.ANTIALIAS)
            resource.format = resource_format

        return resource
144645
public function apply($resource){
		if(!function_exists('imagefilter')){
			throw new Exception("It seems your PHP version is not compiled with the bundled version of the GD library");
		}
		@list(,$level)=func_get_args();
		if(!imagefilter($resource,IMG_FILTER_CONTRAST,(int)$level)){
			throw new Exception("CONTRAST filter failed");
		}
		return $resource;
	}
698242
public function apply($resource){
		// Extract arguments
		@list(,$w_ratio,$h_ratio,$position)=func_get_args();
		$w_ratio=(float)$w_ratio;
		$h_ratio=(float)$h_ratio;
		if($w_ratio<=0 or $h_ratio<=0){
			throw new Exception("Ratio cannot be null or negative");
		}
		// Get resolution
		$w_src=imagesx($resource);
		$h_src=imagesy($resource);
		if($w_src===false or $h_src===false){
			throw new Exception("An error was encountered while getting image resolution");
		}
		// Compute basic chop dimensions
		$w_tmp=round(($h_src/$h_ratio)*$w_ratio);
		$h_tmp=round(($w_src/$w_ratio)*$h_ratio);
		if($w_tmp>$h_tmp){
			$width=$w_src;
			$height=$h_tmp;
		}
		else{
			$width=$w_tmp;
			$height=$h_src;
		}
		// Compute position
		switch($position){
			case self::LEFT:
				$x=0;
				if($w_ratio<$h_ratio)		$y=0;
				elseif($w_ratio>$h_ratio)	$y=round(($h_src-$height)/2);
				else						$y=round(($h_src-$height)/2);
				break;
			case self::RIGHT:
				$x=$w_src-$width;
				if($w_ratio<$h_ratio)		$y=0;
				elseif($w_ratio>$h_ratio)	$y=round(($h_src-$height)/2);
				else						$y=round(($h_src-$height)/2);
				break;
			case self::TOP:
				if($w_ratio<$h_ratio)		$x=round(($w_src-$width)/2);
				elseif($w_ratio>$h_ratio)	$x=0;
				else						$x=round(($w_src-$width)/2);
				$y=0;
				break;
			case self::BOTTOM:
				if($w_ratio<$h_ratio)		$x=round(($w_src-$width)/2);
				elseif($w_ratio>$h_ratio)	$x=0;
				else						$x=round(($w_src-$width)/2);
				$y=$h_src-$height;
				break;
			case self::CENTERED:
			default:
				if($w_ratio<$h_ratio){
					$x=round(($w_src-$width)/2);
					$y=0;
				}
				elseif($w_ratio>$h_ratio){
					$x=0;
					$y=round(($h_src-$height)/2);
				}
				else{
					$x=round(($w_src-$width)/2);
					$y=round(($h_src-$height)/2);
				}
		}
		// Chop image
		return parent::apply($resource,$x,$y,$width,$height);
	}
196204
protected function matchesResource(Resource $resource = null)
    {
        // If the resource is null we should only return true if the current resource is also null.
        if ($resource === null) {
            return $this->getResource() === null || (
                $this->getResourceType() === null && $this->getResourceId() === null
            );
        }

        // If the permission's resource id is null then all resources with a specific ID are accepted.
        if ($this->getResourceId() === null) {
            return $this->getResourceType() === $resource->getResourceType();
        }

        // Otherwise make sure that we're matching a specific resource.
        return (
            $this->getResourceType() === $resource->getResourceType() &&
            $this->getResourceId() === $resource->getResourceId()
        );
    }
948261
def apply(self, resource):
        
        if not isinstance(resource, Image.Image):
            raise ValueError('Unknown resource format')

        original_width, original_height = resource.size

        resource_format = resource.format

        if self.mode == 'outbound':
            target_width, target_height = self.outbound_sizes(original_width, original_height, self.width, self.height)
            resource = resource.resize((target_width, target_height), Image.ANTIALIAS)

            crop_sizes = self.crop_sizes(target_width, target_height, self.width, self.height)
            resource = resource.crop(crop_sizes)
        else:
            target_width, target_height = self.inset_sizes(original_width, original_height, self.width, self.height)
            resource = resource.resize((target_width, target_height), Image.ANTIALIAS)

        resource.format = resource_format

        return resource
567833
public function apply(Picture $picture)
    {
        $resource = $picture->getResource();

        if($picture->isGd())
        {
            $resize = new Resize(

                imagesx($resource) - $this->width  * 2,
                imagesy($resource) - $this->height * 2,
                Resize::MODE_STRETCH
            );

            $resize->apply($picture);

            $resource = $picture->getResource();

            imagesavealpha($resource, TRUE);

            $width  = imagesx($resource);
            $height = imagesy($resource);

            $borderedWidth  = $width  + $this->width * 2;
            $borderedHeight = $height + $this->height * 2;

            $borderColor = $this->hex2rgb($this->color);
            $borderColor = imagecolorallocate($resource, $borderColor[0], $borderColor[1], $borderColor[2]);

            $borderedImage = imagecreatetruecolor($borderedWidth, $borderedHeight);
            $transparent = imagecolorallocatealpha($borderedImage, 0, 0, 0, 127);

            imagesavealpha($borderedImage, TRUE);
            imagefill($borderedImage, 0, 0, $transparent);

            // BORDER: TOP x BOTTOM x LEFT x RIGHT
            imagefilledrectangle($borderedImage, 0, 0, $borderedWidth - 1, $this->height - 1, $borderColor);
            imagefilledrectangle($borderedImage, 0, $borderedHeight - 1, $borderedWidth - 1, $borderedHeight - $this->height, $borderColor);
            imagefilledrectangle($borderedImage, 0, 0, $this->width - 1, $borderedHeight - 1, $borderColor);
            imagefilledrectangle($borderedImage, $borderedWidth - 1, 0, $borderedWidth - $this->width, $borderedHeight - 1, $borderColor);

            imagecopyresampled($borderedImage, $resource, $this->width, $this->height, 0, 0, $width, $height, $width, $height);

            $picture->setResource($borderedImage);
        }
        else
        {
            $width  = $resource->getImageWidth();
            $height = $resource->getImageHeight();

            $resize = new Resize(

                $resource->getImageWidth()  - $this->width  * 2,
                $resource->getImageHeight() - $this->height * 2,
                Resize::MODE_STRETCH
            );

            $resize->apply($picture);

            $borderedImage = new \Imagick();
            $borderedImage->setFormat('png');
            $borderedImage->newImage($width, $height, new \ImagickPixel('transparent'));

            // BORDER: TOP x BOTTOM x LEFT x RIGHT
            $border = new \ImagickDraw();
            $border->setFillColor(new \ImagickPixel($this->color));
            $border->rectangle(0, 0, $width - 1, $this->height - 1);
            $border->rectangle(0, $height - $this->height, $width - 1, $height - 1);
            $border->rectangle(0, 0, $this->width - 1, $height - 1);
            $border->rectangle($width - $this->width, 0, $width - 1, $height - 1);

            $borderedImage->compositeImage($resource, $resource->getImageCompose(), $this->height, $this->width);
            $borderedImage->drawImage($border);

            $picture->setResource($borderedImage);
        }
    }
485595
public function setResource ($name, $resource)
	{
		
		// Get the resource
		
		$obj	=& Sonic::getResource ($resource);
		
		if (!$obj)
		{
			return FALSE;
		}
		
		// Set resource object
		
		$this->setResourceObj ($name, $obj);
		
		// Return
		
		return TRUE;
		
	}
739867
public function apply(Picture $picture)
    {
        $resource = $picture->getResource();

        if($picture->isGd())
        {
            imagefilter($resource, IMG_FILTER_CONTRAST, -15);
            imagefilter($resource, IMG_FILTER_BRIGHTNESS, 8);

            $picture->setResource($resource);
        }
        else
        {
            $resource->modulateImage(108, 170, 100);
        }
    }
605962
public function apply($resource){
		// Format
		@list(,$size,$type,$corners)=func_get_args();
		$size=$size?(int)$size:10;
		$type=(int)$type;
		$corners=(int)$corners;
		// Normalize
		if(!$size){
			$size=10;
		}
		if(
			$corners!=self::ALL &&
			$corners!=self::TOPLEFT &&
			$corners!=self::TOPRIGHT &&
			$corners!=self::BOTTOMRIGHT &&
			$corners!=self::BOTTOMLEFT
		){
			$corners=self::ALL;
		}
		// Get resolution
		$width=imagesx($resource);
		$height=imagesy($resource);
		if($width===false || $height===false){
			throw new Exception("An error was encountered while getting image resolution");
		}
		// Create mask
		$corner=$this->_createTrueColor($size,$size);
		imageantialias($corner,true);
		$white=imagecolorallocate($corner,255,255,255);
		$gray=imagecolorallocate($corner,150,150,150);
		$black=imagecolorallocate($corner,0,0,0);
		imagefill($corner,0,0,$white);
		switch($type){
			case self::BEVELLED:
				imagefilledarc($corner,$size,$size,$size*2+2,$size*2+2,180,270,$gray,IMG_ARC_CHORD); // anti-aliasing
				imagefilledarc($corner,$size,$size,$size*2+1,$size*2+1,180,270,$black,IMG_ARC_CHORD);
				break;
			case self::TRUNCATED:
				break;
			case self::ROUNDED:
			default:
				imagefilledarc($corner,$size,$size,$size*2+2,$size*2+2,180,270,$gray,IMG_ARC_PIE); // anti-aliasing
				imagefilledarc($corner,$size,$size,$size*2+1,$size*2+1,180,270,$black,IMG_ARC_PIE);
		}
		// Prepare corners to apply
		if($corners==self::ALL || $corners==self::TOPLEFT)      $topleft=$corner;
		if($corners==self::ALL || $corners==self::TOPRIGHT)     $topright=imagerotate($corner,270,0);
		if($corners==self::ALL || $corners==self::BOTTOMRIGHT)  $bottomright=imagerotate($corner,180,0);
		if($corners==self::ALL || $corners==self::BOTTOMLEFT)   $bottomleft=imagerotate($corner,90,0);
		// Apply corners
		if(isset($topleft))		$resource=parent::apply($resource,$topleft,0);
		if(isset($topright))	$resource=parent::apply($resource,$topright,$width-$size);
		if(isset($bottomright))	$resource=parent::apply($resource,$bottomright,$width-$size,$height-$size);
		if(isset($bottomleft))	$resource=parent::apply($resource,$bottomleft,0,$height-$size);
		// Clean up
		@imagedestroy($topleft);
		@imagedestroy($topright);
		@imagedestroy($bottomright);
		@imagedestroy($bottomleft);
		return $resource;
	}
344832
public function removeResource(Resource $resource)
    {
        if (!isset($this->resources[$resource->getUri()])) {
            return;
        }
        unset($this->resources[$resource->getUri()]);
    }