/** * Create a blank image with of given dimensions and fill it with $bgColor. * * @param int $width Width of the new image. * @param int $height Height of the new image. * @param string $bgColor Background color. Following formats are acceptable * - "fff" * - "ffffff" * - array(255,255,255) * @param int $alpha Alpha transparency. * * @return \Webiny\Component\Image\ImageInterface */ public function create($width, $height, $bgColor = null, $alpha = 100) { $size = new Box($width, $height); $palette = new \Imagine\Image\Palette\RGB(); $color = $palette->color($bgColor, $alpha); return new Image($this->instance->create($size, $color)); }
/** * @param $image \pavlinter\display2\objects\Image * @param $originalImage \Imagine\Gd\Image * @return mixed */ public function resize($image, $originalImage) { $Box = new Box($image->width, $image->height); $newImage = $originalImage->thumbnail($Box); $boxNew = $newImage->getSize(); $x = ($Box->getWidth() - $boxNew->getWidth()) / 2; $y = ($Box->getHeight() - $boxNew->getHeight()) / 2; $point = new \Imagine\Image\Point($x, $y); $palette = new \Imagine\Image\Palette\RGB(); $color = $palette->color($image->bgColor, $image->bgAlpha); return \yii\imagine\Image::getImagine()->create($Box, $color)->paste($newImage, $point); }
public function getImagineImage(ImagineInterface $imagine, FontCollection $fontCollection, $width, $height) { $fontPath = $fontCollection->getFontById($this->font)->getPath(); if ($this->getAbsoluteSize() !== null) { $fontSize = $this->getAbsoluteSize(); } elseif ($this->getRelativeSize() !== null) { $fontSize = (int) $this->getRelativeSize() / 100 * $height; } else { throw new \LogicException('Either relative or absolute watermark size must be set!'); } if (true || !class_exists('ImagickDraw')) { // Fall back to ugly image. $palette = new \Imagine\Image\Palette\RGB(); $font = $imagine->font($fontPath, $fontSize, $palette->color('#000')); $box = $font->box($this->getText()); $watermarkImage = $imagine->create($box, $palette->color('#FFF')); $watermarkImage->draw()->text($this->text, $font, new \Imagine\Image\Point(0, 0)); } else { // CURRENTLY DISABLED. // Use nicer Imagick implementation. // Untested! // @todo Test and implement it! $draw = new \ImagickDraw(); $draw->setFont($fontPath); $draw->setFontSize($fontSize); $draw->setStrokeAntialias(true); //try with and without $draw->setTextAntialias(true); //try with and without $draw->setFillColor('#fff'); $textOnly = new \Imagick(); $textOnly->newImage(1400, 400, "transparent"); //transparent canvas $textOnly->annotateImage($draw, 0, 0, 0, $this->text); //Create stroke $draw->setFillColor('#000'); //same as stroke color $draw->setStrokeColor('#000'); $draw->setStrokeWidth(8); $strokeImage = new \Imagick(); $strokeImage->newImage(1400, 400, "transparent"); $strokeImage->annotateImage($draw, 0, 0, 0, $this->text); //Composite text over stroke $strokeImage->compositeImage($textOnly, \Imagick::COMPOSITE_OVER, 0, 0, \Imagick::CHANNEL_ALPHA); $strokeImage->trimImage(0); //cut transparent border $watermarkImage = $imagine->load($strokeImage->getImageBlob()); //$strokeImage->resizeImage(300,0, \Imagick::FILTER_CATROM, 0.9, false); //resize to final size } return $watermarkImage; }
/** * @param \pavlinter\display2\objects\Image $image * @param \Imagine\Gd\Image $originalImage * @return static * @throws InvalidConfigException */ public function resize($image, $originalImage) { if (empty($image->width)) { throw new InvalidConfigException('The "width" property must be set for "' . $image::className() . '".'); } if (empty($image->height)) { throw new InvalidConfigException('The "height" property must be set for "' . $image::className() . '".'); } /* @var $size \Imagine\Image\Box */ $size = $originalImage->getSize(); $wDivider = $size->getWidth() >= $image->width ? $size->getWidth() / $image->width : 0; $hDivider = $size->getHeight() >= $image->height ? $size->getHeight() / $image->height : 0; $w = $image->width; // if image smaller $h = $image->height; // if image smaller if ($wDivider > $hDivider) { if ($size->getHeight() >= $image->height) { $w = $size->getWidth() / $hDivider; $h = $image->height; } } else { //$wDivider <= $hDivider if ($size->getWidth() >= $image->width) { $w = $image->width; $h = $size->getHeight() / $wDivider; } } $Box = new Box($w, $h); $newImage = $originalImage->thumbnail($Box); $boxNew = $newImage->getSize(); $x = ($Box->getWidth() - $boxNew->getWidth()) / 2; $y = ($Box->getHeight() - $boxNew->getHeight()) / 2; $point = new \Imagine\Image\Point($x, $y); $palette = new \Imagine\Image\Palette\RGB(); $color = $palette->color($image->bgColor, $image->bgAlpha); return \yii\imagine\Image::getImagine()->create($Box, $color)->paste($newImage, $point); }
/** * @param $image \pavlinter\display2\objects\Image * @param $originalImage \Imagine\Gd\Image * @return mixed */ public function resize($image, $originalImage) { /* @var $size \Imagine\Image\Box */ $size = $originalImage->getSize(); if ($image->width) { if ($size->getWidth() >= $image->width) { $divider = $size->getWidth() / $image->width; } else { $divider = $image->width / $size->getWidth(); } $h = $size->getHeight() / $divider; $w = $image->width; } else { if ($image->height) { if ($size->getHeight() >= $image->height) { $divider = $size->getHeight() / $image->height; } else { $divider = $image->height / $size->getHeight(); } $w = $size->getWidth() / $divider; $h = $image->height; } else { $w = $size->getWidth(); $h = $size->getHeight(); } } $Box = new Box($w, $h); $newImage = $originalImage->thumbnail($Box); $boxNew = $newImage->getSize(); $x = ($Box->getWidth() - $boxNew->getWidth()) / 2; $y = ($Box->getHeight() - $boxNew->getHeight()) / 2; $point = new \Imagine\Image\Point($x, $y); $palette = new \Imagine\Image\Palette\RGB(); $color = $palette->color($image->bgColor, $image->bgAlpha); return \yii\imagine\Image::getImagine()->create($Box, $color)->paste($newImage, $point); }
/** * Apply background color to image when converting from transparent to non-transparent * * @param \Imagine\Image\ImageInterface $imageInstance * @param $bgColor */ private function _applyBackgroundColor(\Imagine\Image\ImageInterface &$imageInstance, $bgColor) { $palette = new \Imagine\Image\Palette\RGB(); $color = $palette->color($bgColor); $topLeft = new \Imagine\Image\Point(0, 0); $backgroundImage = $this->imagineInstance->create($imageInstance->getSize(), $color); $backgroundImage->paste($imageInstance, $topLeft); $imageInstance = $backgroundImage; }
/** * @param $text * @param $width * @param $height * @param string $fontSize * @param string $color * @param string $backgroundColor * @return $this */ public function createPlaceholder($text, $width, $height, $fontSize = '20', $color = '000', $backgroundColor = 'ddd') { $this->metaData = null; $this->imageSource = null; $this->imageWidth = $width; $this->imageHeight = $height; if ($this->imageFormat === null) { $this->imageFormat = 'jpg'; } if ($this->font === null) { $this->setFontFile('Public/fonts/fraym/arial.ttf'); } $bgBox = new \Imagine\Image\Box($width, $height); $imgColor = new \Imagine\Image\Palette\RGB(); $img = $this->imagine->create($bgBox, $imgColor->color($backgroundColor)); $descriptionBoxImg = new \Imagine\Gd\Font(realpath($this->font), $fontSize, $imgColor->color($color)); $descriptionBoxImg = $descriptionBoxImg->box($text, 0)->getWidth(); // set the point to start drawing text, depending on parent image width $descriptionPositionCenter = ceil(($img->getSize()->getWidth() - $descriptionBoxImg) / 2); if ($descriptionPositionCenter < 0) { $descriptionPositionCenter = 0; } $img->draw()->text($text, new \Imagine\Gd\Font(realpath($this->font), $fontSize, $imgColor->color($color)), new \Imagine\Image\Point($descriptionPositionCenter, $img->getSize()->getHeight() / 2 - $fontSize / 2), 0); $this->image = $img; return $this; }
function generate_image($image, $typeobj, $id) { global $faker; $image->setTitle(getRealText(20))->setDescription(getRealText(250))->setChapo(getRealText(40))->setPostscriptum(getRealText(40))->setFile(sprintf("sample-image-%s.png", $id))->save(); $palette = new \Imagine\Image\Palette\RGB(); // Generate images $imagine = new Imagine\Gd\Imagine(); $image = $imagine->create(new Imagine\Image\Box(320, 240), $palette->color('#E9730F')); $white = $palette->color('#FFF'); $font = $imagine->font(__DIR__ . '/faker-assets/FreeSans.ttf', 14, $white); $tbox = $font->box("THELIA"); $image->draw()->text("THELIA", $font, new Imagine\Image\Point((320 - $tbox->getWidth()) / 2, 30)); $str = sprintf("%s sample image", ucfirst($typeobj)); $tbox = $font->box($str); $image->draw()->text($str, $font, new Imagine\Image\Point((320 - $tbox->getWidth()) / 2, 80)); $font = $imagine->font(__DIR__ . '/faker-assets/FreeSans.ttf', 18, $white); $str = sprintf("%s ID %d", strtoupper($typeobj), $id); $tbox = $font->box($str); $image->draw()->text($str, $font, new Imagine\Image\Point((320 - $tbox->getWidth()) / 2, 180)); $image->draw()->line(new Imagine\Image\Point(0, 0), new Imagine\Image\Point(319, 0), $white)->line(new Imagine\Image\Point(319, 0), new Imagine\Image\Point(319, 239), $white)->line(new Imagine\Image\Point(319, 239), new Imagine\Image\Point(0, 239), $white)->line(new Imagine\Image\Point(0, 239), new Imagine\Image\Point(0, 0), $white); $image_file = sprintf("%s/media/images/%s/sample-image-%s.png", THELIA_LOCAL_DIR, $typeobj, $id); if (!is_dir(dirname($image_file))) { mkdir(dirname($image_file), 0777, true); } $image->save($image_file); }
/** * Creates a thumbnail image. The function differs from `\Imagine\Image\ImageInterface::thumbnail()` function that * it keeps the aspect ratio of the image. * @param string $filename the image file path or path alias. * @param integer $width the width in pixels to create the thumbnail * @param integer $height the height in pixels to create the thumbnail * @param string $mode * @return ImageInterface */ public static function thumbnail($filename, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND) { $box = new Box($width, $height); $img = static::getImagine()->open(Yii::getAlias($filename)); if ($img->getSize()->getWidth() <= $box->getWidth() && $img->getSize()->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) { return $img->copy(); } $img = $img->thumbnail($box, $mode); // create empty image to preserve aspect ratio of thumbnail //$thumb = static::getImagine()->create($box, new Color('FFF', 100)); $palette = new \Imagine\Image\Palette\RGB(); $thumb = static::getImagine()->create($box, $palette->color('#FFFFFF', 100)); // calculate points $size = $img->getSize(); $startX = 0; $startY = 0; if ($size->getWidth() < $width) { $startX = ceil($width - $size->getWidth()) / 2; } if ($size->getHeight() < $height) { $startY = ceil($height - $size->getHeight()) / 2; } $thumb->paste($img, new Point($startX, $startY)); return $thumb; }
/** * Generate a shirt image * Image::generateShirt('uploads/chuck.jpg', 'Member', 999, 200); * * @param string $url * @param integer $width * @param integer $height * @param boolean $crop * @return string */ public function generateShirt($url, $shirt_name = 'Member', $member_id = 0, $width = 200, $height = 200, $crop = true, $quality = 90) { if ($url) { // URL info $info = pathinfo($url); // The size if (!$height) { $height = $width; } // Font and text $text_offset = 64; $font_path = public_path() . '/fonts/trebuc.ttf'; $palette = new \Imagine\Image\Palette\RGB(); $font_color = $palette->color('#FFF'); $shirt_name = strtoupper(substr($shirt_name, 0, 12)); $shirt_name_length = strlen($shirt_name); $font_size = 16; if ($shirt_name_length < 6) { $font_size = 16; } else { if ($shirt_name_length > 5 && $shirt_name_length < 10) { $font_size = 11; } else { $font_size = 8; } } $font = $this->imagine->font($font_path, $font_size, $font_color); $font_width = $font->box($shirt_name, 0)->getWidth(); // Centering the text over the image in x axis $text_offset = (200 - $font_width) / 2; if ($text_offset < 64) { $text_offset = 64; } // Quality $quality = Config::get('image.quality', $quality); // Directories and file names $fileName = $info['basename']; $sourceDirPath = public_path() . '/' . $info['dirname']; $sourceFilePath = $sourceDirPath . '/' . $fileName; $targetDirName = $width . 'x' . $height . ($crop ? '_crop' : '') . '_' . $shirt_name . '_' . $member_id; $targetDirPath = $sourceDirPath . '/' . $targetDirName . '/'; $targetFilePath = $targetDirPath . $fileName; $targetUrl = asset($info['dirname'] . '/' . $targetDirName . '/' . $fileName); $fbTargetUrl = 'http://' . $_SERVER['SERVER_NAME'] . '/uploads/' . $targetDirName . '/' . $fileName; // Create directory if missing try { // Create dir if missing if (!File::isDirectory($targetDirPath) and $targetDirPath) { @File::makeDirectory($targetDirPath); } // Set the size $size = new \Imagine\Image\Box($width, $height); // Now the mode $mode = $crop ? \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND : \Imagine\Image\ImageInterface::THUMBNAIL_INSET; if (!File::exists($targetFilePath) or File::lastModified($targetFilePath) < File::lastModified($sourceFilePath)) { $canvas = $this->imagine->open($sourceFilePath)->thumbnail($size, $mode); $canvas->draw()->text($shirt_name, $font, new \Imagine\Image\Point($text_offset, 54), 0); $canvas->save($targetFilePath, array('quality' => $quality)); } } catch (\Exception $e) { Log::error('[IMAGE SERVICE] Failed to resize image "' . $url . '" [' . $e->getMessage() . ']'); return false; } return $fbTargetUrl; } }
/** * Creates an Imagine color object. * @param mixed $color the color. * @return Imagine\Image\Palette\RGB the object. */ protected function createColor($color) { if (is_array($color)) { list($color, $alpha) = $color; } if (!isset($color)) { $color = '#ffffff'; } if (!isset($alpha)) { $alpha = 0; } $palette = new Imagine\Image\Palette\RGB(); return $palette->color($color, $alpha); }