function execute()
 {
     $gdimage =& $this->image->getImage();
     $w = $this->image->getWidth();
     $h = $this->image->getHeight();
     $src_x = ceil($w);
     $src_y = ceil($h);
     $dst_x = $src_x;
     $dst_y = $src_y;
     // http://php.about.com/od/gdlibrary/ss/grayscale_gd.htm
     function yiq($r, $g, $b)
     {
         return $r * 0.299 + $g * 0.587 + $b * 0.114;
     }
     $dst_im = ImageCreateTrueColor($dst_x, $dst_y);
     ImageCopyResampled($dst_im, $gdimage, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y);
     for ($c = 0; $c < 256; $c++) {
         $palette[$c] = imagecolorallocate($dst_im, $c, $c, $c);
     }
     for ($y = 0; $y < $src_y; $y++) {
         for ($x = 0; $x < $src_x; $x++) {
             $rgb = imagecolorat($dst_im, $x, $y);
             $r = $rgb >> 16 & 0xff;
             $g = $rgb >> 8 & 0xff;
             $b = $rgb & 0xff;
             $gs = yiq($r, $g, $b);
             imagesetpixel($dst_im, $x, $y, $palette[$gs]);
         }
     }
     $gdimage = $dst_im;
 }
コード例 #2
0
function image_resize_greyscale(&$src_im)
{
    $src_x = ceil(imagesx($src_im));
    $src_y = ceil(imagesy($src_im));
    $dst_x = $src_x;
    $dst_y = $src_y;
    // http://php.about.com/od/gdlibrary/ss/grayscale_gd.htm
    function yiq($r, $g, $b)
    {
        return $r * 0.299 + $g * 0.587 + $b * 0.114;
    }
    $dst_im = ImageCreateTrueColor($dst_x, $dst_y);
    ImageCopyResampled($dst_im, $src_im, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y);
    for ($c = 0; $c < 256; $c++) {
        $palette[$c] = imagecolorallocate($dst_im, $c, $c, $c);
    }
    for ($y = 0; $y < $src_y; $y++) {
        for ($x = 0; $x < $src_x; $x++) {
            $rgb = imagecolorat($dst_im, $x, $y);
            $r = $rgb >> 16 & 0xff;
            $g = $rgb >> 8 & 0xff;
            $b = $rgb & 0xff;
            $gs = yiq($r, $g, $b);
            imagesetpixel($dst_im, $x, $y, $palette[$gs]);
        }
    }
    $src_im = $dst_im;
}
コード例 #3
0
function gray($base)
{
    global $h_base, $w_base;
    $bwimage = imagecreate($w_base, $h_base);
    for ($c = 0; $c < 256; $c++) {
        $palette[$c] = imagecolorallocate($bwimage, $c, $c, $c);
    }
    for ($y = 0; $y < $h_base; $y++) {
        for ($x = 0; $x < $w_base; $x++) {
            $rgb = imagecolorat($base, $x, $y);
            $r = $rgb >> 16 & 0xff;
            $g = $rgb >> 8 & 0xff;
            $b = $rgb & 0xff;
            $gs = yiq($r, $g, $b);
            imagesetpixel($bwimage, $x, $y, $palette[$gs]);
        }
    }
    return $bwimage;
}
コード例 #4
0
/**
 * Implementation of the YIQ color contrast algorithm for php-less
 *
 * See http://24ways.org/2010/calculating-color-contrast/ for details
 */
function less_contrast_color_yiq($arg)
{
    list($type) = $arg;
    if ('color' == $arg[0]) {
        $color = $arg;
    } elseif ('list' == $type) {
        if (isset($arg[2][0])) {
            $color = $arg[2][0];
        }
        if (isset($arg[2][1])) {
            $dark = $arg[2][1];
        }
        if (isset($arg[2][2])) {
            $light = $arg[2][2];
        }
        if (isset($arg[2][3])) {
            $threshold = $arg[2][3];
        }
    }
    if (empty($color)) {
        return;
    }
    if (empty($dark) || 'color' != $dark[0]) {
        $dark = array('color', 0, 0, 0);
    }
    if (empty($light) || 'color' != $light[0]) {
        $light = array('color', 255, 255, 255);
    }
    if (empty($threshold) || 'number' != $threshold[0]) {
        $dark_yiq = yiq($dark[1], $dark[2], $dark[3]);
        $light_yiq = yiq($light[1], $light[2], $light[3]);
        $threshold = array('number', round(($dark_yiq + $light_yiq) / 2));
    }
    list($type, $r, $g, $b) = $color;
    $yiq = yiq($r, $g, $b);
    return $yiq >= $threshold[1] ? $dark : $light;
}
コード例 #5
0
ファイル: ImageResizer.php プロジェクト: raffpaquin/Gregory
 public function resize($maxwidth = 0, $maxheight = 0, $opts = array())
 {
     $config = self::getConfig();
     if ($config['cache'] && ($cache = $this->getCache())) {
         $this->_data = $cache;
         return $this->_data;
     }
     if (is_array($maxwidth)) {
         $opts = $maxwidth;
     } else {
         $opts['width'] = $maxwidth;
         $opts['height'] = $maxheight;
     }
     $origWidth = $width = $this->_imagesize[0];
     $origHeight = $height = $this->_imagesize[1];
     if (isset($opts['color'])) {
         $color = preg_replace('/[^0-9a-fA-F]/', '', (string) $opts['color']);
     } else {
         $color = FALSE;
     }
     if ((!isset($opts['width']) || !$opts['width']) && isset($opts['height'])) {
         $opts['width'] = 99999999999999.0;
     } elseif (isset($opts['width']) && (!isset($opts['height']) || !$opts['height'])) {
         $opts['height'] = 99999999999999.0;
     }
     if (!isset($opts['width'])) {
         $opts['width'] = $width;
     }
     if (!isset($opts['height'])) {
         $opts['height'] = $height;
     }
     $offsetX = 0;
     $offsetY = 0;
     if (isset($opts['ratio']) && $opts['ratio']) {
         //$cropRatio = explode(':', (string) $opts['ratio']);
         $cropRatio = array($opts['width'], $opts['height']);
         if (sizeof($cropRatio) == 2) {
             $ratioComputed = $width / $height;
             $cropRatioComputed = (double) $cropRatio[0] / (double) $cropRatio[1];
             if ($ratioComputed < $cropRatioComputed) {
                 $origHeight = $height;
                 $height = $width / $cropRatioComputed;
                 $offsetY = ($origHeight - $height) / 2;
             } else {
                 if ($ratioComputed > $cropRatioComputed) {
                     $origWidth = $width;
                     $width = $height * $cropRatioComputed;
                     $offsetX = ($origWidth - $width) / 2;
                 }
             }
         }
     }
     if (isset($opts["x"])) {
         $offsetX = (int) $opts["x"];
     }
     if (isset($opts["y"])) {
         $offsetY = (int) $opts["y"];
     }
     $xRatio = $opts['width'] / $width;
     $yRatio = $opts['height'] / $height;
     if ($xRatio * $height < $opts['height']) {
         $tnHeight = ceil($xRatio * $height);
         $tnWidth = $opts['width'];
     } else {
         $tnWidth = ceil($yRatio * $width);
         $tnHeight = $opts['height'];
     }
     // Determine the quality of the output image
     $quality = isset($opts['quality']) ? (int) $opts['quality'] : $config['quality'];
     $resizeFactor = isset($opts['scale']) && (double) $opts['scale'] != 0 ? (double) $opts['scale'] : 1;
     if (isset($opts['crop']) && sizeof($cropRatio) == 2) {
         $dst = imagecreatetruecolor($cropRatio[0] * $resizeFactor, $cropRatio[1] * $resizeFactor);
         //$dst = imagecreatetruecolor($cropRatio[0], $cropRatio[1]);
     } else {
         $dst = imagecreatetruecolor($tnWidth * $resizeFactor, $tnHeight * $resizeFactor);
         //$dst = imagecreatetruecolor($tnWidth, $tnHeight*$resizeFactor);
     }
     switch ($this->_mime) {
         case 'image/gif':
             $creationFunction = 'imagecreatefromgif';
             $outputFunction = 'imagepng';
             $mime = 'image/png';
             // We need to convert GIFs to PNGs
             $doSharpen = FALSE;
             $quality = round(10 - $quality / 10);
             break;
         case 'image/x-png':
         case 'image/png':
             $creationFunction = 'imagecreatefrompng';
             $outputFunction = 'imagepng';
             $doSharpen = FALSE;
             $quality = round(10 - $quality / 10);
             // PNG needs a compression level of 0 (no compression) through 9
             break;
         default:
             $creationFunction = 'imagecreatefromjpeg';
             $outputFunction = 'imagejpeg';
             $doSharpen = TRUE;
             break;
     }
     // Read in the original image
     $src = $creationFunction($this->_filename);
     if (in_array($this->_mime, array('image/gif', 'image/png'))) {
         if (!$color) {
             // If this is a GIF or a PNG, we need to set up transparency
             imagealphablending($dst, false);
             imagesavealpha($dst, true);
         } else {
             // Fill the background with the specified color for matting purposes
             if ($color[0] == '#') {
                 $color = substr($color, 1);
             }
             $background = FALSE;
             if (strlen($color) == 6) {
                 $background = imagecolorallocate($dst, hexdec($color[0] . $color[1]), hexdec($color[2] . $color[3]), hexdec($color[4] . $color[5]));
             } else {
                 if (strlen($color) == 3) {
                     $background = imagecolorallocate($dst, hexdec($color[0] . $color[0]), hexdec($color[1] . $color[1]), hexdec($color[2] . $color[2]));
                 }
             }
             if ($background) {
                 imagefill($dst, 0, 0, $background);
             }
         }
     }
     if (isset($opts['crop']) && !empty($opts['crop'])) {
         if ($width > $height) {
             $destW = $tnWidth / $tnHeight * $height * ($cropRatio[0] / $tnWidth);
             $destH = $height * ($cropRatio[0] / $tnWidth);
         } else {
             $destH = $tnHeight / $tnWidth * $width * ($cropRatio[0] / $tnWidth);
             $destW = $width * ($cropRatio[0] / $tnWidth);
         }
     } else {
         if (isset($opts['crop_width']) && isset($opts['crop_height'])) {
             $destW = (int) $opts['crop_width'];
             $destH = (int) $opts['crop_height'];
             $width = $origWidth;
             $height = $origHeight;
             $offsetX = $offsetX * ($width / $destW);
             $offsetY = $offsetY * ($height / $destH);
         } else {
             $destH = $tnHeight;
             $destW = $tnWidth;
         }
     }
     $w = $width;
     $h = $height;
     $dw = $destW * $resizeFactor;
     //$dw = $destW;
     $dh = $destH * $resizeFactor;
     //$dh = $destH;
     // Resample the original image into the resized canvas we set up earlier
     imagecopyresampled($dst, $src, 0, 0, $offsetX, $offsetY, $dw, $dh, $w, $h);
     if ($doSharpen) {
         $sharpness = self::findSharp($width, $tnWidth);
         $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1));
         $divisor = $sharpness;
         $offset = 0;
         imageconvolution($dst, $sharpenMatrix, $divisor, $offset);
     }
     if (isset($opts['blackwhite']) && $opts['blackwhite']) {
         for ($c = 0; $c < 256; $c++) {
             $palette[$c] = imagecolorallocate($dst, $c, $c, $c);
         }
         //Creates yiq function
         function yiq($r, $g, $b)
         {
             return $r * 0.299 + $g * 0.587 + $b * 0.114;
         }
         for ($y = 0; $y < $destH; $y++) {
             for ($x = 0; $x < $destW; $x++) {
                 $rgb = imagecolorat($dst, $x, $y);
                 $r = $rgb >> 16 & 0xff;
                 $g = $rgb >> 8 & 0xff;
                 $b = $rgb & 0xff;
                 //This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
                 $gs = yiq($r, $g, $b);
                 imagesetpixel($dst, $x, $y, $palette[$gs]);
             }
         }
     }
     if (isset($opts['rotation']) && !empty($opts['rotation'])) {
         $dst = imagerotate($dst, (double) $opts['rotation'], 0);
     }
     try {
         /*
         
         		Filters :
         
         			IMG_FILTER_GRAYSCALE: Converts the image into grayscale.
         			IMG_FILTER_BRIGHTNESS: Changes the brightness of the image. Use arg1 to set the level of brightness.
         			IMG_FILTER_CONTRAST: Changes the contrast of the image. Use arg1 to set the level of contrast.
         			IMG_FILTER_COLORIZE: Like IMG_FILTER_GRAYSCALE, except you can specify the color. Use arg1, arg2 and arg3 in the form of red, blue, green and arg4 for the alpha channel. The range for each color is 0 to 255.
         			IMG_FILTER_EDGEDETECT: Uses edge detection to highlight the edges in the image.
         			IMG_FILTER_EMBOSS: Embosses the image.
         			IMG_FILTER_GAUSSIAN_BLUR: Blurs the image using the Gaussian method.
         			IMG_FILTER_SELECTIVE_BLUR: Blurs the image.
         			IMG_FILTER_MEAN_REMOVAL: Uses mean removal to achieve a "sketchy" effect.
         			IMG_FILTER_SMOOTH: Makes the image smoother. Use arg1 to set the level of smoothness.
         			IMG_FILTER_PIXELATE: Applies pixelation effect to the image, use arg1 to set the block size and arg2 to set the pixelation effect mode.
         */
         if (isset($opts['IMG_FILTER_NEGATE']) && !empty($opts['IMG_FILTER_NEGATE'])) {
             imagefilter($dst, IMG_FILTER_NEGATE);
         }
         if (isset($opts['IMG_FILTER_GRAYSCALE']) && !empty($opts['IMG_FILTER_GRAYSCALE'])) {
             imagefilter($dst, IMG_FILTER_GRAYSCALE);
         }
         if (isset($opts['IMG_FILTER_BRIGHTNESS']) && !empty($opts['IMG_FILTER_BRIGHTNESS'])) {
             imagefilter($dst, IMG_FILTER_BRIGHTNESS, intval($opts['IMG_FILTER_BRIGHTNESS']));
         }
         if (isset($opts['IMG_FILTER_CONTRAST']) && !empty($opts['IMG_FILTER_CONTRAST'])) {
             imagefilter($dst, IMG_FILTER_CONTRAST, intval($opts['IMG_FILTER_CONTRAST']));
         }
         if (isset($opts['IMG_FILTER_COLORIZE']) && !empty($opts['IMG_FILTER_COLORIZE'])) {
             if (strlen($opts['IMG_FILTER_COLORIZE']) == 12) {
                 $arg1 = intval(substr($opts['IMG_FILTER_COLORIZE'], 0, 3));
                 $arg2 = intval(substr($opts['IMG_FILTER_COLORIZE'], 3, 3));
                 $arg3 = intval(substr($opts['IMG_FILTER_COLORIZE'], 6, 3));
                 $arg4 = intval(substr($opts['IMG_FILTER_COLORIZE'], 9, 3));
                 imagefilter($dst, IMG_FILTER_COLORIZE, $arg1, $arg2, $arg3, $arg4);
             }
         }
         if (isset($opts['IMG_FILTER_EDGEDETECT']) && !empty($opts['IMG_FILTER_EDGEDETECT'])) {
             imagefilter($dst, IMG_FILTER_EDGEDETECT);
         }
         if (isset($opts['IMG_FILTER_EMBOSS']) && !empty($opts['IMG_FILTER_EMBOSS'])) {
             imagefilter($dst, IMG_FILTER_EMBOSS);
         }
         if (isset($opts['IMG_FILTER_GAUSSIAN_BLUR']) && !empty($opts['IMG_FILTER_GAUSSIAN_BLUR'])) {
             imagefilter($dst, IMG_FILTER_GAUSSIAN_BLUR);
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_SELECTIVE_BLUR']) && !empty($opts['IMG_FILTER_SELECTIVE_BLUR'])) {
             imagefilter($dst, IMG_FILTER_SELECTIVE_BLUR);
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_MEAN_REMOVAL']) && !empty($opts['IMG_FILTER_MEAN_REMOVAL'])) {
             imagefilter($dst, IMG_FILTER_MEAN_REMOVAL);
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_SMOOTH']) && !empty($opts['IMG_FILTER_SMOOTH'])) {
             imagefilter($dst, IMG_FILTER_SMOOTH, intval($opts['IMG_FILTER_SMOOTH']));
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_PIXELATE']) && !empty($opts['IMG_FILTER_PIXELATE'])) {
             if (strlen($opts['IMG_FILTER_PIXELATE']) > 3) {
                 $arg1 = intval(substr($opts['IMG_FILTER_PIXELATE'], 0, 3));
                 $arg2 = $opts['IMG_FILTER_PIXELATE'] == true;
             } else {
                 $arg1 = intval($opts['IMG_FILTER_PIXELATE']);
                 $arg2 = false;
             }
             imagefilter($dst, IMG_FILTER_PIXELATE, $arg1, $arg2);
         }
         //: Reverses all colors of the image.
     } catch (Exception $e) {
         var_dump($e);
     }
     ob_start();
     $outputFunction($dst, null, $quality);
     $this->_data = ob_get_contents();
     ob_end_clean();
     imagedestroy($src);
     imagedestroy($dst);
     //The following depends on Imagick
     //$this->_image is the Imagick Object; add calls to the different native functions as needed, it is returned as $this->_data at the end.
     if (class_exists('Imagick') && (isset($opts['cropx']) || isset($opts['cropy']) || isset($opts['brightness']) || isset($opts['saturation']) || isset($opts['hue']) || isset($opts['contrast']) || isset($opts['composite']))) {
         IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 512);
         $this->_image = new Imagick();
         $this->_image->readImageBlob($this->_data);
         //For image cropping
         if (isset($opts['cropx']) && !empty($opts['cropx'])) {
             if (!isset($opts['cropy']) || empty($opts['cropy'])) {
                 $opts['cropy'] = 0;
             }
             if (isset($opts['lencropx']) && !empty($opts['lencropx'])) {
                 $maxwidth = $opts['lencropx'];
             } else {
                 $maxwidth = $config['maxLengthCropX'];
             }
             if (isset($opts['lencropy']) && !empty($opts['lencropy'])) {
                 $maxheight = $opts['lencropy'];
             } else {
                 $maxheight = $config['maxLengthCropY'];
             }
             $this->_image->cropImage($maxwidth, $maxheight, $opts['cropx'], $opts['cropy']);
         } else {
             if (isset($opts['cropy']) && !empty($opts['cropy'])) {
                 if (!isset($opts['cropx']) || empty($opts['cropx'])) {
                     $opts['cropx'] = 0;
                 }
                 if (isset($opts['lencropx']) && !empty($opts['lencropx'])) {
                     $maxwidth = $opts['lencropx'];
                 } else {
                     $maxwidth = 410;
                 }
                 if (isset($opts['lencropy']) && !empty($opts['lencropy'])) {
                     $maxheight = $opts['lencropy'];
                 } else {
                     $maxheight = 275;
                 }
                 $this->_image->cropImage($maxwidth, $maxheight, $opts['cropx'], $opts['cropy']);
             }
         }
         //Modulate: 100 is the norm
         if (isset($opts['brightness']) && !empty($opts['brightness'])) {
             $this->_image->modulateImage($opts['brightness'], 100, 100);
         }
         if (isset($opts['saturation']) && !empty($opts['saturation'])) {
             $this->_image->modulateImage(100, $opts['saturation'], 100);
         }
         if (isset($opts['hue']) && !empty($opts['hue'])) {
             $this->_image->modulateImage(100, 100, $opts['hue']);
         }
         if (isset($opts['contrast']) && !empty($opts['contrast'])) {
             $this->_image->contrastImage($opts['contrast']);
         }
         //To blend 2 images
         if (isset($opts['composite']) && !empty($opts['composite'])) {
             $over = new Imagick($opts['composite']);
             $d = $this->_image->getImageGeometry();
             $w = $d['width'];
             $h = $d['height'];
             $over->thumbnailImage($w, $h);
             $this->_image->compositeImage($over, imagick::COMPOSITE_DEFAULT, 0, 0);
             $over->clear();
             $over->destroy();
         }
         $this->_data = $this->_image->getImageBlob();
         $this->_image->clear();
         $this->_image->destroy();
     }
     if ($config['cache']) {
         $this->saveCache($opts);
     }
     return $this->_data;
 }
コード例 #6
0
ファイル: setData.php プロジェクト: Tripbull/newrepo
function greyscale($file)
{
    //$file = 'profile.jpg';
    // This sets it to a .jpg, but you can change this to png or gif if that is what you are working with
    //header('Content-type: image/jpeg');
    // Get the dimensions
    list($width, $height) = getimagesize($file);
    // Define our source image
    $source = imagecreatefromjpeg($file);
    // Creating the Canvas
    $bwimage = imagecreate($width, $height);
    //Creates the 256 color palette
    for ($c = 0; $c < 256; $c++) {
        $palette[$c] = imagecolorallocate($bwimage, $c, $c, $c);
    }
    //Creates yiq function
    function yiq($r, $g, $b)
    {
        return $r * 0.299 + $g * 0.587 + $b * 0.114;
    }
    //Reads the origonal colors pixel by pixel
    for ($y = 0; $y < $height; $y++) {
        for ($x = 0; $x < $width; $x++) {
            $rgb = imagecolorat($source, $x, $y);
            $r = $rgb >> 16 & 0xff;
            $g = $rgb >> 8 & 0xff;
            $b = $rgb & 0xff;
            //This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
            $gs = yiq($r, $g, $b);
            imagesetpixel($bwimage, $x, $y, $palette[$gs]);
        }
    }
    // Outputs a jpg image, but you can change this to png or gif if that is what you are working with
    //imagejpeg($bwimage);
    imagejpeg($bwimage, $file);
    imagedestroy($source);
    return;
}