public function testCreate() { $img = PaletteImage::create(10, 10); $this->assertTrue($img instanceof PaletteImage); $this->assertTrue($img->isValid()); $this->assertFalse($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 an image with only specified channels copied * * @param \WideImage\PaletteImage $img * @param array $channels * @return \WideImage\PaletteImage */ public function execute($img, $channels) { $blank = array('red' => 0, 'green' => 0, 'blue' => 0); if (isset($channels['alpha'])) { unset($channels['alpha']); } $width = $img->getWidth(); $height = $img->getHeight(); $copy = PaletteImage::create($width, $height); if ($img->isTransparent()) { $otci = $img->getTransparentColor(); $TRGB = $img->getColorRGB($otci); $tci = $copy->allocateColor($TRGB); } else { $otci = null; $tci = null; } for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $ci = $img->getColorAt($x, $y); if ($ci === $otci) { $copy->setColorAt($x, $y, $tci); continue; } $RGB = $img->getColorRGB($ci); $newRGB = $blank; foreach ($channels as $channel) { $newRGB[$channel] = $RGB[$channel]; } $color = $copy->getExactColor($newRGB); if ($color == -1) { $color = $copy->allocateColor($newRGB); } $copy->setColorAt($x, $y, $color); } } if ($img->isTransparent()) { $copy->setTransparentColor($tci); } return $copy; }
/** * 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 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; }
/** * Factory method for creating a palette image * * @param int $width * @param int $height * @return \WideImage\PaletteImage */ public static function createPaletteImage($width, $height) { return PaletteImage::create($width, $height); }