/** * Adds a watermark to an existing image. * @param string $filename the image file path or path alias. * @param string $watermarkFilename the file path or path alias of the watermark image. * @throws \CException if `$start` is invalid */ public static function addWatermark($filename, $watermarkFilename) { $imagine = static::getImagine(); $transformation = new Transformation(); $transformation->add(new Watermark($imagine, $watermarkFilename)); $transformation->apply($imagine->open($filename))->save(); }
/** * @param ImagineImageInterface $image * * @return ImagineImagineInterface */ public function updateFile(ImagineImageInterface $image) { if ($this->angle !== null && is_int($this->angle)) { $transformation = new Transformation(); $transformation->rotate($this->angle, null); $transformation->apply($image); } return $image; }
/** * @param ImagineImageInterface $image * * @return ImagineImageInterface */ public function updateFile(ImagineImageInterface $image) { if ($this->direction !== null) { $transformation = new Transformation(); if ($this->direction == 'x') { $transformation->flipHorizontally(); } elseif ($this->direction == 'y') { $transformation->flipVertically(); } $transformation->apply($image); } return $image; }
public function save() { $transaction = Yii::$app->db->beginTransaction(); try { $characteristics = new Characteristics(); $characteristics->display_type = $this->displayType; $characteristics->mechanism_type = $this->mechanismType; $characteristics->starp_type = $this->starpType; $characteristics->sex = $this->sex; if (!$characteristics->save(false)) { throw new \Exception('Charasteristic not save, transaction rollback'); } $products = new Products(); $products->clk_name = $this->name; $products->clk_description = $this->description; $products->characteristics_id = $characteristics->id; $products->price = $this->price; if (!$products->save(false)) { throw new \Exception('Product not save, transaction rollback'); } $hashName = Yii::$app->security->generateRandomString(); $fullImagePath = self::FULL_IMAGES_PATH . $hashName . '.' . $this->images->extension; if (!$this->images->saveAs($fullImagePath)) { throw new \Exception('Image not save in full image path'); } $imgSizeReduct = function ($side = 'width') use($fullImagePath) { $size = getimagesize($fullImagePath); if ($side === 'width') { return $size[0] / self::THUMB_REDUCTION; } if ($side === 'height') { return $size[1] / self::THUMB_REDUCTION; } }; $images = new Images(); $transformation = new Transformation(); $imagine = new Imagine(); $transformation->thumbnail(new Box($imgSizeReduct('width'), $imgSizeReduct('height')))->save(Yii::getAlias('@webroot/' . self::THUMBS_IMAGES_PATH . $hashName . '.' . $this->images->extension)); $transformation->apply($imagine->open(Yii::getAlias('@webroot/' . self::FULL_IMAGES_PATH . $hashName . '.' . $this->images->extension))); $images->product_id = $products->id; $images->img_name = $hashName . '.' . $this->images->extension; if (!$images->save(false)) { throw new \Exception('Images not save, transaction rollback'); } $transaction->commit(); Yii::$app->session->addFlash('success', 'Product successfully added'); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } }
public function testFilterSorting() { $filter1 = new TestFilter(); $filter2 = new TestFilter(); $filter3 = new TestFilter(); $transformation1 = new Transformation(); $transformation1->add($filter1, 5)->add($filter2, -3)->add($filter3); $expected1 = array($filter2, $filter3, $filter1); $transformation2 = new Transformation(); $transformation2->add($filter1)->add($filter2)->add($filter3); $expected2 = array($filter1, $filter2, $filter3); $this->assertSame($expected1, $transformation1->getFilters()); $this->assertSame($expected2, $transformation2->getFilters()); }
public function testCropFlipPasteShow() { $img1 = $this->getImage(); $img2 = $this->getImage(); $start = new Point(0, 0); $size = new Box(50, 50); $img1->expects($this->once())->method('paste')->with($img2, $start)->will($this->returnValue($img1)); $img1->expects($this->once())->method('show')->with('png')->will($this->returnValue($img1)); $img2->expects($this->once())->method('flipHorizontally')->will($this->returnValue($img2)); $img2->expects($this->once())->method('flipVertically')->will($this->returnValue($img2)); $img2->expects($this->once())->method('crop')->with($start, $size)->will($this->returnValue($img2)); $transformation2 = new Transformation(); $transformation2->flipHorizontally()->flipVertically()->crop($start, $size); $transformation1 = new Transformation(); $transformation1->paste($transformation2->apply($img2), $start)->show('png')->apply($img1); }
public function actionIndex() { exit; $imagesPath = Yii::app()->uploadManager->getBasePath() . DIRECTORY_SEPARATOR . 'realty' . DIRECTORY_SEPARATOR; $imagine = Imagine::getImagine(); $transformation = new Transformation(); $transformation->add(new Watermark($imagine, Yii::getPathOfAlias('webroot') . '/uploads/watersign.png')); if (file_exists($imagesPath)) { try { foreach (glob($imagesPath . '*.JPG') as $key => $path) { if ($key > 200) { break; } $transformation->apply($imagine->open($path))->save(); } echo "success!!!"; } catch (Exception $e) { echo 'Save operation failed: ' . $path . '<br />'; } } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $imagine = new Imagine(); $width = $input->getOption('width') ?: Faker::numberBetween(200, 1200); $height = $input->getOption('height') ?: Faker::numberBetween(200, 1200); $extension = $input->getOption('extension') ?: Faker::randomElement($this->getAvailableExtensions()); $selector = new Selector($input->getOption('samples')); $images = $selector->select($width, $height, $extension); $filter = array_filter($images, function (Image $image) use($width, $height) { return $width * $height >= $image->getPixels(); }); if (true === empty($filter)) { $image = Faker::randomElement($images); } else { $image = Faker::randomElement($filter); } $transformed = tempnam(sys_get_temp_dir(), uniqid()); $transformation = new Transformation(); $transformation->thumbnail(new Box($width, $height), ImageInterface::THUMBNAIL_OUTBOUND)->save($transformed); $transformation->apply($imagine->open($image->getPath())); $this->getFormatter($input->getOption('formatter'))->output(new Image($transformed), $output); }
public function uploadImages() { if ($this->validate()) { $transformation = new Transformation(); $imagine = new Imagine(); //$randHeight = function() { //return rand(250, 330); //}; foreach ($this->imageFiles as $file) { /* @var $file \yii\web\UploadedFile */ $imageRandName = Yii::$app->security->generateRandomString(12); $fullImagePath = self::FULL_IMAGES_PATH . $imageRandName . '.' . $file->extension; $file->saveAs($fullImagePath); $imgSizeReduct = function ($side = 'width') use($fullImagePath) { $size = getimagesize($fullImagePath); if ($side === 'width') { return $size[0] / self::THUMB_REDUCTION; } if ($side === 'height') { return $size[1] / self::THUMB_REDUCTION; } }; $transformation->thumbnail(new Box($imgSizeReduct(), $imgSizeReduct('height')))->save(Yii::getAlias('@webroot/' . self::THUMBS_IMAGES_PATH . $imageRandName . '.' . $file->extension)); $transformation->apply($imagine->open(Yii::getAlias('@webroot/' . self::FULL_IMAGES_PATH . $imageRandName . '.' . $file->extension))); $images = new Images(); $images->name = $imageRandName . '.' . $file->extension; $images->alt = $this->alt; if (!$images->save()) { Yii::$app->session->addFlash('danger', 'Image not saved into data base!'); return false; } } Yii::$app->session->addFlash('success', 'Image uploaded successfully'); return true; } Yii::$app->session->addFlash('danger', 'Incorrect data'); return false; }
/** * Scale an image to best fit a thumbnail size * * @param ImageInterface $image The ImageInterface instance from Imagine * @param int $width The width in pixels * @param int $height The height in pixels * @return ImageInterface */ protected function thumbnailScale(ImageInterface $image, $width, $height) { $transformation = new Transformation(); $transformation->thumbnail(new Box($width, $height)); return $transformation->apply($image); }
/** * Test if filter throws exception via Transformation without * passing Imagine instance. * * @expectedException \Imagine\Exception\InvalidArgumentException */ public function testFilterThrowsExceptionViaTransformation() { $filters = new Transformation(); $filters->add(new DummyImagineAwareFilter()); $filters->apply($this->getImage()); }