Пример #1
0
 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;
     }
 }
 /**
  * {@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);
 }
Пример #3
0
 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);
 }