/** * 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)); }
/** * {@inheritdoc} */ public function resize($imagePath, array $size, $mode, $force = false) { $cacheKey = $this->getCacheKey($size, $mode); $filename = basename($imagePath); if (false === $force && $this->imageCache->contains($filename, $cacheKey)) { return $this->imageCache->getRelativePath($filename, $cacheKey); } $cacheAbsolutePath = $this->imageCache->getAbsolutePath($filename, $cacheKey); $imagine = new Imagine(); $imagineImage = $imagine->open($this->filesystem->getRootDir() . $imagePath); $imageSize = array($imagineImage->getSize()->getWidth(), $imagineImage->getSize()->getHeight()); $boxSize = $this->resizeHelper->getBoxSize($imageSize, $size); $box = $this->getBox($boxSize[0], $boxSize[1]); if (ImageResizerInterface::INSET === $mode) { $imageSizeInBox = $this->resizeHelper->getImageSizeInBox($imageSize, $boxSize); $imagineImage->resize($this->getBox($imageSizeInBox[0], $imageSizeInBox[1])); $palette = new RGB(); $box = $imagine->create($box, $palette->color($this->color, $this->alpha)); $imagineImage = $box->paste($imagineImage, $this->getPointInBox($imageSizeInBox, $boxSize)); } else { $imagineImage = $imagineImage->thumbnail($box); } $this->filesystem->mkdir(dirname($cacheAbsolutePath)); $imagineImage->save($cacheAbsolutePath, ImageOptionHelper::getOption($filename)); return $this->imageCache->getRelativePath($filename, $cacheKey); }
public function actionIndex($type = null, $size = null, $pictureFilename = null, $extension = null) { try { ini_set('memory_limit', '1024M'); error_reporting(1); list($x, $y) = explode('x', $size); $imagine = new Imagine(); $picture = $imagine->open(\Yii::$app->basePath . '/web/pictures/' . $type . '/full_size/' . $pictureFilename . '.' . $extension); $pictureSize = $picture->getSize(); $pictureWidth = $pictureSize->getWidth(); $pictureHeight = $pictureSize->getHeight(); if (!$x) { $aspectRatio = $pictureWidth / $pictureHeight; } elseif (!$y) { $aspectRatio = $pictureHeight / $pictureWidth; } $y = $y ? $y : round($aspectRatio * $x); $x = $x ? $x : round($aspectRatio * $y); $box = new Box($x, $y, ImageInterface::FILTER_UNDEFINED); $mode = ImageInterface::THUMBNAIL_OUTBOUND; $thumbnail = $picture->thumbnail($box, $mode); $thumbnailSize = $thumbnail->getSize(); $thumbnailX = $thumbnailSize->getWidth(); $xPos = $x / 2 - $thumbnailX / 2; $color = $extension == 'png' ? new Color('#fff', 100) : new Color('#fff'); $imagine->create($box, $color)->paste($thumbnail, new Point($xPos, 0))->save(\Yii::$app->basePath . '/web/pictures/' . $type . '/' . $size . '/' . $pictureFilename . '.' . $extension)->show($extension); } catch (Exception $e) { return false; } }
protected function setUp() { $this->imagePath = Core\Config()->paths('root') . 'image.png'; $this->width = 640; $this->height = 480; $this->size = array("{$this->width}x{$this->height}", "{$this->height}x{$this->width}"); $imagine = new Imagine(); $this->image = $imagine->create(new Box($this->width * 2, $this->width * 2))->save($this->imagePath); }
public function testCommand() { foreach ($this->getDataList() as $data) { $imagine = new Imagine(); $imageBox = new Box($this->imageWidth, $this->imageHeight); $image = $imagine->create($imageBox); $this->command->execute($image, $data['options']); $this->assertEquals($data['width'], $image->getSize()->getWidth()); $this->assertEquals($data['height'], $image->getSize()->getHeight()); } }
public function testLoadGrayscale() { $loader = new GrayscaleFilterLoader(); $palette = new RGB(); $imagine = new Imagine(); // Generate blue image $image = $imagine->create(new Box(20, 20), $palette->color(array(20, 90, 240))); //Apply Grayscale filter $result = $loader->load($image); //Test result $pixel = $result->getColorAt(new Point(10, 10)); $this->assertEquals('#565656', (string) $pixel); }
/** * Add photo * * @param string $photo * @return PrintContent */ public function addPhoto($photo) { $imagine = new Imagine(); if (@is_file($photo)) { $image = $imagine->open($photo); } else { $image = $imagine->load($photo); } // Fix transparent png $palette = new RGB(); $white = $palette->color('FFF'); $image = $imagine->create(new Box($image->getSize()->getWidth(), $image->getSize()->getHeight()), $white)->paste($image, new Point(0, 0)); return $this->addContent(self::TYPE_PHOTO, $image->get('jpg')); }
/** * Constructs a new ImageNotFoundResponse * @param int $maxWidth * @param int $maxHeight * @param int $code * @param string $message */ public function __construct($maxWidth, $maxHeight, $code, $message) { if ($maxWidth == 0) { $maxWidth = 300; } if ($maxHeight == 0) { $maxHeight = 300; } $imagine = new Imagine(); $size = new Box(300, 300); $image = $imagine->create($size); $black = $image->palette()->color("000"); $path = realpath(__DIR__ . "/../Resources/public/fonts/OpenSans-Regular.ttf"); $font = $imagine->font($path, 24, $black); $image->draw()->text($message, $font, new Point(0, 0)); $box = $image->getSize(); $box = $box->widen($maxWidth); if ($box->getHeight() > $maxHeight) { $box = $box->heighten($maxHeight); } $image->resize($box); return parent::__construct($image->get("png"), $code, array("Content-Type" => "image/png")); }
/** * Upload the photo * * @return boolean */ public function upload() { if (null === $this->file) { return false; } /* * Upload and resize */ $imagine = new Imagine(); // Create a transparent image $image = $imagine->create(new Box(200, 200), new Color('000', 100)); // Create the logo thumbnail in a 200x200 box $thumbnail = $imagine->open($this->file->getPathname())->thumbnail(new Box(200, 200), Image::THUMBNAIL_INSET); // Paste point $pastePoint = new Point((200 - $thumbnail->getSize()->getWidth()) / 2, (200 - $thumbnail->getSize()->getHeight()) / 2); // Paste the thumbnail in the transparent image $image->paste($thumbnail, $pastePoint); // Save the result $image->save(__DIR__ . '/../../../../../web/logos/' . $this->getLogin() . '.png'); $this->logo = $this->getLogin() . '.png'; return true; }
/** * Text to image * * @param string $text * @param array $options * @return \Imagine\Gd\Image|\Imagine\Image\ImageInterface */ public function textToImage($text, array $options = array()) { $options = array_merge(['align' => self::ALIGN_LEFT, 'size' => 20, 'font' => null, 'vertical' => false, 'padding_top' => null, 'padding_bottom' => null, 'padding_left' => null, 'padding_right' => null, 'line_height' => null], $options); $imagine = new Imagine(); $palette = new RGB(); $white = $palette->color('FFF'); $black = $palette->color('000'); $fontFile = $options['font'] ? $options['font'] : $this->getFont(); $font = new Font($fontFile, $options['size'], $black); $textBox = $font->box('我'); $textWidth = $textBox->getWidth(); $textHeight = $textBox->getHeight(); $paddingTop = $options['padding_top'] ? $options['padding_top'] : $textHeight / 2; $paddingBottom = $options['padding_bottom'] ? $options['padding_bottom'] : $textHeight / 2; $paddingLeft = $options['padding_bottom'] ? $options['padding_bottom'] : $textWidth / 2; $paddingRight = $options['padding_bottom'] ? $options['padding_bottom'] : $textWidth / 2; if ($options['vertical']) { $textBox = $font->box($text); $canvasWidth = $textBox->getWidth() + $paddingLeft + $paddingRight; $canvasHeight = $textBox->getHeight() + $paddingTop + $paddingBottom; $textCanvas = $imagine->create(new Box($canvasWidth, $canvasHeight), $white); $textCanvas->draw()->text($text, $font, new Point($paddingLeft, $paddingTop)); $textCanvas->rotate(90); $newHeight = $textCanvas->getSize()->getHeight(); $canvas = $imagine->create(new Box(self::CONTENT_MAX_WIDTH, $newHeight)); $canvas->paste($textCanvas, new Point((self::CONTENT_MAX_WIDTH - $textCanvas->getSize()->getWidth()) / 2, 0)); } else { $maxWidth = self::CONTENT_MAX_WIDTH - $paddingLeft - $paddingRight; $lines = $this->stringToMultipleLines($text, $font, $maxWidth); $lineCount = count($lines); $lineHeight = $options['line_height'] ? $options['line_height'] : $textHeight / 2; $canvasHeight = $paddingTop + $paddingBottom + $textHeight * $lineCount + $lineHeight * ($lineCount - 1); $canvas = $imagine->create(new Box(self::CONTENT_MAX_WIDTH, $canvasHeight)); $y = $paddingTop; foreach ($lines as $index => $line) { try { $width = $font->box($line)->getWidth(); } catch (\Imagine\Exception\InvalidArgumentException $e) { $width = $textBox->getWidth(); } switch ($options['align']) { case self::ALIGN_RIGHT: $x = $maxWidth - $width - $paddingLeft; break; case self::ALIGN_CENTER: $x = (self::CONTENT_MAX_WIDTH - $width) / 2; break; default: $x = $paddingLeft; break; } $x = $x < 0 ? 0 : $x; $point = new Point($x, $y); $canvas->draw()->text($line, $font, $point); if ($index + 1 < $lineCount) { $y += $textHeight + $lineHeight; } } } return $canvas; }
/** * @Route("/upload/{t}", name="_upload") */ public function uploadAction(Request $request, $t = 'b') { if ($t == 'a') { $title = ''; $type = 0; $num = (int) $request->get('num'); $file_name = 'photoA' . $num . '.png'; } else { $uploadedFile = $request->files->get('image'); if ($uploadedFile == null || !in_array(strtolower($uploadedFile->getClientOriginalExtension()), array('png', 'jpg', 'jpeg', 'gif'))) { return new Response('只能上传照片喔~'); } elseif (!$uploadedFile->isValid()) { return new Response($uploadedFile->getErrorMessage()); } else { $file_path = preg_replace('/app$/si', 'web/uploads', $this->get('kernel')->getRootDir()); //$file_path = $request->getBasePath().$this->path; $file_name = date('YmdHis') . rand(1000, 9999) . '.' . $uploadedFile->getClientOriginalExtension(); $fs = new Filesystem(); if (!$fs->exists($file_path)) { try { $fs->mkdir($file_path); } catch (IOException $e) { return new Response('服务器发生错误,无法创建文件夹:' . $e->getPath()); //$result['msg'] = '服务器发生错误,无法创建文件夹:'.$e->getPath(); } } $uploadedFile->move($file_path, $file_name); $scale = $request->get('scale'); $pos_x = (int) $request->get('pos_x'); $pos_y = (int) $request->get('pos_y'); $num = $request->get('num'); $title = trim($request->get('title')); $img_url = $file_path . '/' . $file_name; $imagine = new Imagine(); $exif = @exif_read_data($img_url, 0, true); if (isset($exif['IFD0']['Orientation'])) { $photo = $imagine->open($img_url); if ($exif['IFD0']['Orientation'] == 6) { $photo->rotate(90)->save($img_url); } elseif ($exif['IFD0']['Orientation'] == 3) { $photo->rotate(180)->save($img_url); } } $photo = $imagine->open($img_url); $size = $photo->getSize(); $width = 640; $height = 1039; $w1 = $width * $scale; $h1 = $w1 * $size->getHeight() / $size->getWidth(); $imagine->open($img_url)->resize(new Box($w1, $h1))->save($img_url); $imagine->open($img_url)->resize(new Box($w1, $h1))->save($img_url); #左移裁切 if ($pos_x < 0) { $size = $imagine->open($img_url)->getSize(); $imagine->open($img_url)->crop(new Point(abs($pos_x), 0), new Box($size->getWidth() - abs($pos_x), $size->getHeight()))->save($img_url); } #上移裁切 if ($pos_y < 0) { $size = $imagine->open($img_url)->getSize(); $imagine->open($img_url)->crop(new Point(0, abs($pos_y)), new Box($size->getWidth(), $size->getHeight() - abs($pos_y)))->save($img_url); } #右移拼贴 if ($pos_x > 0) { $photo = $imagine->open($img_url); $size = $photo->getSize(); $collage_width = $pos_x + $size->getWidth() > $width ? $pos_x + $size->getWidth() : $width; $collage_height = $size->getHeight(); $collage = $imagine->create(new Box($collage_width, $collage_height)); $collage->paste($photo, new Point($pos_x, 0))->save($img_url); } #下移拼贴 if ($pos_y > 0) { $photo = $imagine->open($img_url); $size = $photo->getSize(); $collage_width = $size->getWidth(); $collage_height = $pos_y + $size->getHeight() > $width ? $pos_y + $size->getHeight() : $height; $collage = $imagine->create(new Box($collage_width, $collage_height)); $collage->paste($photo, new Point(0, $pos_y))->save($img_url); } #超出剪切 $photo = $imagine->open($img_url); $size = $photo->getSize(); $_width = $size->getWidth(); if ($size->getWidth() > $width) { $_width = $width; $imagine->open($img_url)->crop(new Point(0, 0), new Box($_width, $size->getHeight()))->save($img_url); } if ($size->getHeight() > $height) { $imagine->open($img_url)->crop(new Point(0, 0), new Box($_width, $height))->save($img_url); } $photo = $imagine->open($img_url); $collage = $imagine->create(new Box($width, $height)); $collage->paste($photo, new Point(0, 0))->save($img_url); $collage = $imagine->open('bundles/app/default/images/photoB' . $num . '.png'); $photo = $imagine->open($img_url); $photo->paste($collage, new Point(0, 0))->save($img_url); $type = 1; /* $photo = $imagine->open($img_url); $color = new \Imagine\Image\Color; $font = new \Imagine\Gd\Font('',30, $color); $photo->draw()->text('tesintg',$font,new Point(0,0),0); */ } } $em = $this->getDoctrine()->getManager(); $em->getConnection()->beginTransaction(); try { $photo = new Entity\Photo(); $user = $this->getUser(); $photo->setImgUrl($file_name); $photo->setUser($user); $photo->setTitle($title); $photo->setType($type); $photo->setFavourNum(0); $photo->setCreateIp($request->getClientIp()); $photo->setCreateTime(new \DateTime('now')); $em->persist($photo); $em->flush(); $em->getConnection()->commit(); return $this->redirect($this->generateUrl('_finish', array('id' => $photo->getId()))); } catch (Exception $e) { $em->getConnection()->rollback(); return new Response($e->getMessage()); //$json['ret'] = 1001; //$json['msg']= $e->getMessage(); } }
/** * @param $size * @return Image|\Imagine\Image\ImageInterface */ protected function createArea($size) { return $this->imagine->create($size, $this->color('0C0', 0)); }