public function testMagicCallDrawRectangle() { $img = WideImage::createTrueColorImage(10, 10); $canvas = $img->getCanvas(); $canvas->filledRectangle(1, 1, 5, 5, $img->allocateColorAlpha(255, 0, 0, 64)); $this->assertRGBAt($img, 3, 3, array('red' => 255, 'green' => 0, 'blue' => 0, 'alpha' => 64)); }
public function testResizeCanvasPositionsCorner() { $img = WideImage::createTrueColorImage(20, 20); $black = $img->allocateColor(0, 0, 0); $white = $img->allocateColor(255, 255, 255); $img->fill(0, 0, $black); $res = $img->resizeCanvas(40, 40, 'bottom', 'right', $white); $this->assertRGBAt($res, 5, 5, $white); $this->assertRGBAt($res, 35, 35, $black); $this->assertRGBAt($res, 5, 35, $white); $this->assertRGBAt($res, 35, 5, $white); }
/** * Returns an image with a resized canvas * * The image is filled with $color. Use $scale to determine, when to resize. * * @param \WideImage\Image $img * @param smart_coordinate $width * @param smart_coordinate $height * @param smart_coordinate $left * @param smart_coordinate $top * @param int $color * @param string $scale 'up', 'down', 'any' * @param boolean $merge * @return \WideImage\Image */ public function execute($img, $width, $height, $left, $top, $color, $scale, $merge) { $new_width = Coordinate::fix($width, $img->getWidth()); $new_height = Coordinate::fix($height, $img->getHeight()); if ($scale == 'down') { $new_width = min($new_width, $img->getWidth()); $new_height = min($new_height, $img->getHeight()); } elseif ($scale == 'up') { $new_width = max($new_width, $img->getWidth()); $new_height = max($new_height, $img->getHeight()); } $new = WideImage::createTrueColorImage($new_width, $new_height); if ($img->isTrueColor()) { if ($color === null) { $color = $new->allocateColorAlpha(0, 0, 0, 127); } } else { imagepalettecopy($new->getHandle(), $img->getHandle()); if ($img->isTransparent()) { $new->copyTransparencyFrom($img); $tc_rgb = $img->getTransparentColorRGB(); $t_color = $new->allocateColorAlpha($tc_rgb); } if ($color === null) { if ($img->isTransparent()) { $color = $t_color; } else { $color = $new->allocateColorAlpha(255, 0, 127, 127); } imagecolortransparent($new->getHandle(), $color); } } $new->fill(0, 0, $color); $x = Coordinate::fix($left, $new->getWidth(), $img->getWidth()); $y = Coordinate::fix($top, $new->getHeight(), $img->getHeight()); // blending for truecolor images if ($img->isTrueColor()) { $new->alphaBlending($merge); } // not-blending for palette images if (!$merge && !$img->isTrueColor() && isset($t_color)) { $new->getCanvas()->filledRectangle($x, $y, $x + $img->getWidth(), $y + $img->getHeight(), $t_color); } $img->copyTo($new, $x, $y); return $new; }
public function testFactories() { $this->assertTrue(WideImage::createTrueColorImage(100, 100) instanceof TrueColorImage); $this->assertTrue(WideImage::createPaletteImage(100, 100) instanceof PaletteImage); }
/** * Resizes the image proportionally inside the given width and height. * The returned image will have always the specified width and height, and any space will be filled * with the given $fillCollor * * @param int $width Exact width in pixels * @param int $height Exact height in pixels * @param string $fit 'inside' (default), 'outside' or 'fill' * @param string $scale 'down' (default), 'up' or 'any' * @param mixed $alignLeft Left position of the image over the fill space, smart coordinate * @param mixed $alignTop Top position of the image over the fill space, smart coordinate * @param int $mergeOpacity The opacity of the image over the fill space * @param int|array $fillColor RGB color index or array. Background color to fill the resulting image with if it's smaller * than the given size. By default (if null), the top left pixel color will be used. * * @return TrueColorImage A new, resized image */ public function resizeInsideRect($width, $height, $fit = 'inside', $scale = 'down', $alignLeft = 'center', $alignTop = 'center', $mergeOpacity = 100, $fillColor = null) { if ($fillColor) { if (is_numeric($fillColor)) { $fillColor = $this->getColorRGB($fillColor); } } else { $fillColor = $this->getColorRGB($this->getColorAt(0, 0)); } $rect = WideImage::createTrueColorImage($width, $height); $rect->fill(0, 0, $rect->allocateColor($fillColor)); $img = $this; for ($i = 0; $i < 4; $i++) { //4 times $img = $img->resize($width, $height, $fit, $scale); } return $rect->merge($img, $alignLeft, $alignTop, $mergeOpacity); }
/** * (non-PHPdoc) * @see WideImage_Image#asTrueColor() */ function asTrueColor() { $width = $this->getWidth(); $height = $this->getHeight(); $new = WideImage::createTrueColorImage($width, $height); if ($this->isTransparent()) { $new->copyTransparencyFrom($this); } if (!imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height)) { throw new WideImage_GDFunctionResultException("imagecopy() returned false"); } return $new; }
<?php include 'helpers/common.php'; $img = \WideImage\WideImage::createTrueColorImage(400, 200); $canvas = $img->getCanvas(); $canvas->useFont('fonts/Vera.ttf', 36, $img->allocateColor(255, 0, 0)); $canvas->writeText('left', 'top', 'abc', 0); $canvas->writeText('right', 'top', 'def', 15); $canvas->writeText('right', 'bottom', 'ghi', 30); $canvas->writeText('left', 'bottom', 'jkl', 45); $canvas->writeText('center', 'center', 'mno', 60); $img->output('png'); exit; // Create a 300x150 image $im = imagecreatetruecolor(600, 350); $black = imagecolorallocate($im, 0, 0, 0); $bgcolor = imagecolorallocate($im, 255, 255, 0); // Set the background to be white imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $bgcolor); // Path to our font file $font = './fonts/Vera.ttf'; $angle = 340; $font_size = 20; $text = 'jW| asdkasdlk alk,.,wedwer|w[r=?'; $text = '#j'; // First we create our bounding box $bbox = imageftbbox($font_size, $angle, $font, $text); function normalize_bbox($bbox) { return array('up-left' => array('x' => $bbox[6], 'y' => $bbox[7]), 'up-right' => array('x' => $bbox[4], 'y' => $bbox[5]), 'down-left' => array('x' => $bbox[0], 'y' => $bbox[1]), 'down-right' => array('x' => $bbox[2], 'y' => $bbox[3])); }
public function testResizeWithoutParametersDoesNothing() { $img = WideImage::createTrueColorImage(70, 20); $res = $img->resize(); $this->assertDimensions($res, $img->getWidth(), $img->getHeight()); }
/** * @param \WideImage\Image $image * @param int $radius * @param int $color * @param int $smoothness * @return \WideImage\Image */ public function execute($image, $radius, $color, $smoothness, $corners) { if ($smoothness < 1) { $sample_ratio = 1; } elseif ($smoothness > 16) { $sample_ratio = 16; } else { $sample_ratio = $smoothness; } $corner = WideImage::createTrueColorImage($radius * $sample_ratio, $radius * $sample_ratio); if ($color === null) { imagepalettecopy($corner->getHandle(), $image->getHandle()); $bg_color = $corner->allocateColor(0, 0, 0); $corner->fill(0, 0, $bg_color); $fg_color = $corner->allocateColor(255, 255, 255); $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color); $corner = $corner->resize($radius, $radius); $result = $image->asTrueColor(); $tc = $result->getTransparentColor(); if ($tc == -1) { $tc = $result->allocateColorAlpha(255, 255, 255, 127); imagecolortransparent($result->getHandle(), $tc); $result->setTransparentColor($tc); } if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) { $result = $result->applyMask($corner, -1, -1); } $corner = $corner->rotate(90); if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) { $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100); } $corner = $corner->rotate(90); if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) { $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100); } $corner = $corner->rotate(90); if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) { $result = $result->applyMask($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100); } return $result; } else { $bg_color = $color; $corner->fill(0, 0, $bg_color); $fg_color = $corner->allocateColorAlpha(127, 127, 127, 127); $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color); $corner = $corner->resize($radius, $radius); $result = $image->copy(); if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) { $result = $result->merge($corner, -1, -1, 100); } $corner = $corner->rotate(90); if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) { $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100); } $corner = $corner->rotate(90); if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) { $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100); } $corner = $corner->rotate(90); if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) { $result = $result->merge($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100); } return $result; } }