function execute($image) { $width = $image->getWidth(); $height = $image->getHeight(); $mask = wiTrueColorImage::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; }
function testCreate() { $img = wiTrueColorImage::create(10, 10); $this->assertTrue($img instanceof wiTrueColorImage); $this->assertTrue($img->isValid()); $this->assertTrue($img->isTrueColor()); }
public function createImage($width, $height, $bgColor = '#000000', $bgTransparentamount = 0) { $newImage = wiTrueColorImage::create($width, $height); list($bgR, $bgG, $bgB) = $this->hex2rgb($bgColor); $bgColor = $newImage->allocateColorAlpha($bgR, $bgG, $bgB, $bgTransparentamount); $newImage->fill(0, 0, $bgColor); return $newImage; }
function asTrueColor() { $width = $this->getWidth(); $height = $this->getHeight(); $new = wiTrueColorImage::create($width, $height); if ($this->isTransparent()) { $new->copyTransparencyFrom($this); } imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height); return $new; }
function execute($img, $left, $top, $width, $height) { $width = wiDimension::fix($img->getWidth(), $width); $height = wiDimension::fix($img->getHeight(), $height); $new = wiTrueColorImage::create($width, $height); if ($img->isTransparent()) { $new->copyTransparencyFrom($img); imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height); } else { $new->alphaBlending(false); $new->saveAlpha(true); imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height); } return $new; }
function execute($img, $width, $height, $fit) { if (!$img instanceof wiImage || !$img->isValid()) { throw new wiInvalidImageException("Can't resize an invalid image."); } $dim = $this->prepareDimensions($img, $width, $height, $fit); $new = wiTrueColorImage::create($dim['width'], $dim['height']); if ($img->isTransparent()) { $new->copyTransparencyFrom($img); imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, 0, 0, $new->getWidth(), $new->getHeight(), $img->getWidth(), $img->getHeight()); } else { $new->alphaBlending(false); $new->saveAlpha(true); imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, 0, 0, $new->getWidth(), $new->getHeight(), $img->getWidth(), $img->getHeight()); } return $new; }
function execute($img, $left, $top, $width, $height) { $left = wiDimension::fix($img->getWidth(), $left); $top = wiDimension::fix($img->getHeight(), $top); $width = wiDimension::fix($img->getWidth(), $width); if ($width > $img->getWidth() - $left) { $width = $img->getWidth() - $left; } $height = wiDimension::fix($img->getHeight(), $height); if ($height > $img->getHeight() - $top) { $height = $img->getHeight() - $top; } if ($left < 0) { $width = $left + $width; $left = 0; } if ($left + $width > $img->getWidth()) { $width = $img->getWidth() - $left; } if ($width < 0) { $width = 0; } if ($top < 0) { $height = $top + $height; $top = 0; } if ($top + $height > $img->getHeight()) { $top = $img->getHeight() - $top; } if ($height < 0) { $height = 0; } $new = wiTrueColorImage::create($width, $height); if ($img->isTransparent()) { $new->copyTransparencyFrom($img); imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height); } else { $new->alphaBlending(false); $new->saveAlpha(true); imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height); } return $new; }
/** * * @var string $scale 'up', 'down' or 'any' */ function execute($img, $width, $height, $fit, $scale = 'any') { if (!$img instanceof wiImage || !$img->isValid()) { throw new wiInvalidImageException("Can't resize an invalid image."); } $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 wiInvalidResizeDimensionException("Both dimensions must be larger than 0."); } $new = wiTrueColorImage::create($dim['width'], $dim['height']); if ($img->isTransparent()) { $new->copyTransparencyFrom($img); imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, 0, 0, $new->getWidth(), $new->getHeight(), $img->getWidth(), $img->getHeight()); } else { $new->alphaBlending(false); $new->saveAlpha(true); imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, 0, 0, $new->getWidth(), $new->getHeight(), $img->getWidth(), $img->getHeight()); } return $new; }
function execute($img, $channels) { $blank = array('red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0); $width = $img->getWidth(); $height = $img->getHeight(); $copy = wiTrueColorImage::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; }
function testSaveOverrideFormat() { $uri = IMG_PATH . 'temp/test.gif'; $img = wiTrueColorImage::create(10, 10); $img->saveToFile($uri, 'png'); try { $img = @wiImage::load($uri); $this->fail("Exception expected"); } catch (wiInvalidImageSourceException $e) { $this->pass(); } $img = wiImage::load($uri, 'png'); $this->assertTrue(10, $img->getWidth()); $this->assertTrue(10, $img->getHeight()); unlink($uri); }