/** * Returns a mask * * @param \WideImage\Image $image * @return \WideImage\Image */ public function execute($image) { $width = $image->getWidth(); $height = $image->getHeight(); $mask = TrueColorImage::create($width, $height); $mask->setTransparentColor(-1); $mask->alphaBlending(false); $mask->saveAlpha(false); for ($i = 0; $i <= 255; $i++) { $greyscale[$i] = ImageColorAllocate($mask->getHandle(), $i, $i, $i); } imagefilledrectangle($mask->getHandle(), 0, 0, $width, $height, $greyscale[255]); $transparentColor = $image->getTransparentColor(); $alphaToGreyRatio = 255 / 127; for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $color = $image->getColorAt($x, $y); if ($color == $transparentColor) { $rgba['alpha'] = 127; } else { $rgba = $image->getColorRGB($color); } imagesetpixel($mask->getHandle(), $x, $y, $greyscale[255 - round($rgba['alpha'] * $alphaToGreyRatio)]); } } return $mask; }
public function testCreate() { $img = TrueColorImage::create(10, 10); $this->assertTrue($img instanceof TrueColorImage); $this->assertTrue($img->isValid()); $this->assertTrue($img->isTrueColor()); }
/** * Saves resized image to a file * * @param string $path File location * @param quality $quality Quality options * @return Image */ public function save($path, $quality = array()) { $ext = pathinfo($path, PATHINFO_EXTENSION); $jpeg_quality = elgg_extract('jpeg_quality', $quality); $png_quality = elgg_extract('png_quality', $quality); $png_filter = elgg_extract('png_filter', $quality); switch ($ext) { default: $this->source->saveToFile($path, $jpeg_quality); break; case 'gif': $this->source->saveToFile($path); break; case 'png': $this->source->saveToFile($path, $png_quality, $png_filter); break; } return $this; }
/** * Returns a copy of the image * * @param $trueColor True if the new image should be truecolor * @return \WideImage\Image */ protected function copyAsNew($trueColor = false) { $width = $this->getWidth(); $height = $this->getHeight(); if ($trueColor) { $new = TrueColorImage::create($width, $height); } else { $new = PaletteImage::create($width, $height); } // copy transparency of source to target if ($this->isTransparent()) { $rgb = $this->getTransparentColorRGB(); if (is_array($rgb)) { $tci = $new->allocateColor($rgb['red'], $rgb['green'], $rgb['blue']); $new->fill(0, 0, $tci); $new->setTransparentColor($tci); } } imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height); return $new; }
/** * Returns an image with only specified channels copied * * @param \WideImage\Image $img * @param array $channels * @return \WideImage\Image */ public function execute($img, $channels) { $blank = array('red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0); $width = $img->getWidth(); $height = $img->getHeight(); $copy = TrueColorImage::create($width, $height); if (count($channels) > 0) { for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $RGBA = $img->getRGBAt($x, $y); $newRGBA = $blank; foreach ($channels as $channel) { $newRGBA[$channel] = $RGBA[$channel]; } $color = $copy->getExactColorAlpha($newRGBA); if ($color == -1) { $color = $copy->allocateColorAlpha($newRGBA); } $copy->setColorAt($x, $y, $color); } } } return $copy; }
/** * Returns a resized image * * @param \WideImage\Image $img * @param smart_coordinate $width * @param smart_coordinate $height * @param string $fit * @param string $scale * @return \WideImage\Image */ public function execute($img, $width, $height, $fit, $scale) { $dim = $this->prepareDimensions($img, $width, $height, $fit); if ($scale === 'down' && ($dim['width'] >= $img->getWidth() && $dim['height'] >= $img->getHeight()) || $scale === 'up' && ($dim['width'] <= $img->getWidth() && $dim['height'] <= $img->getHeight())) { $dim = array('width' => $img->getWidth(), 'height' => $img->getHeight()); } if ($dim['width'] <= 0 || $dim['height'] <= 0) { throw new InvalidResizeDimensionException("Both dimensions must be larger than 0."); } if ($img->isTransparent() || $img instanceof PaletteImage) { $new = PaletteImage::create($dim['width'], $dim['height']); $new->copyTransparencyFrom($img); if (!imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, 0, 0, $new->getWidth(), $new->getHeight(), $img->getWidth(), $img->getHeight())) { throw new GDFunctionResultException("imagecopyresized() returned false"); } } else { $new = TrueColorImage::create($dim['width'], $dim['height']); $new->alphaBlending(false); $new->saveAlpha(true); if (!imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, 0, 0, $new->getWidth(), $new->getHeight(), $img->getWidth(), $img->getHeight())) { throw new GDFunctionResultException("imagecopyresampled() returned false"); } $new->alphaBlending(true); } return $new; }
public function testInternalOpCaseInsensitive() { $img = TrueColorImage::create(10, 10); $result = $img->AUTOcrop(); $this->assertTrue($result instanceof Image); }
/** * Factory method for creating a true-color image * * @param int $width * @param int $height * @return \WideImage\TrueColorImage */ public static function createTrueColorImage($width, $height) { return TrueColorImage::create($width, $height); }
/** * https://sourceforge.net/tracker/?func=detail&aid=3312764&group_id=190526&atid=933712 * @group bugs */ public function testResizeBug950to256() { $img = TrueColorImage::create(950, 266); $op = new ResizeTestable(); $dim = $op->prepareDimensions($img, 256, null, 'inside'); $this->assertEquals(256, $dim['width']); $this->assertEquals(72, $dim['height']); }