/**
  * saveImage
  *
  * @param UploadedFile $file
  * @param string        $imageName
  */
 public function saveImage($file, $imageName)
 {
     if (!$file) {
         return;
     }
     $uploadDir = $this->getUploadDir();
     $this->prepareUploadDir($uploadDir);
     if (is_array($this->thumbs) and !empty($this->thumbs)) {
         foreach ($this->thumbs as $dir => $size) {
             $img = Image::factory($file->tempName);
             // If $size is array of dimensions - resize, else - just save
             if (!is_array($size)) {
                 $size = [$this->slider->width, $this->slider->height];
             }
             $img->resize(implode(',', $size))->save($uploadDir . '/' . $dir . '/' . $imageName);
         }
         @unlink($file->tempName);
     } else {
         $file->saveAs($uploadDir . '/' . $imageName);
     }
 }
 /**
  * @param integer       $width
  * @param integer     $height
  * @param string $dir
  * @param string $attr
  *
  * @return string
  */
 public function getCroppedImage($width, $height, $dir = 'full', $attr = 'image')
 {
     if (!is_file($this->getCroppedImagePath($width, $height, $attr)) && is_file($this->getImagePath($dir, $attr))) {
         $image = Image::factory($this->getImagePath($dir, $attr));
         $old_aspect = $image->width / $image->height;
         $new_aspect = $width / $height;
         if ($old_aspect == 1) {
             if ($width > $height) {
                 $image->resize($width, $height, Image::WIDTH);
             } else {
                 $image->resize($width, $height, Image::HEIGHT);
             }
         } elseif ($old_aspect < $new_aspect) {
             $image->resize($width, $height, Image::WIDTH);
         } else {
             $image->resize($width, $height, Image::HEIGHT);
         }
         $image->crop($width, $height);
         $image->save($this->getCroppedImagePath($width, $height, $attr));
     }
     return Yii::$app->request->baseUrl . '/images/' . trim($this->tableName(), '{}%') . '/_cropped/' . $width . '_' . $height . '_|_' . $this->{$attr};
 }
Beispiel #3
0
 public function uploadImage()
 {
     if ($this->validate()) {
         if (!$this->imageFile) {
             return true;
         }
         if ($this->avatar) {
             unlink(\Yii::getAlias('@webroot') . '/' . Yii::$app->params['uploadPath'] . $this->avatar);
             unlink(\Yii::getAlias('@webroot') . '/' . Yii::$app->params['uploadPreviewPath'] . $this->avatar);
         }
         $imageName = Yii::$app->security->generateRandomString() . '.' . $this->imageFile->extension;
         $imageDatePath = date('Ymd') . '/';
         $imageDir = \Yii::getAlias('@webroot') . '/' . Yii::$app->params['uploadPath'] . $imageDatePath;
         FileHelper::createDirectory($imageDir);
         $resultSaveImage = $this->imageFile->saveAs($imageDir . $imageName);
         if ($resultSaveImage) {
             $imagePreview = Image::factory($imageDir . $imageName);
             $imagePreview->resize(200, 200);
             $imagePreviewDir = \Yii::getAlias('@webroot') . '/' . Yii::$app->params['uploadPreviewPath'] . $imageDatePath;
             FileHelper::createDirectory($imagePreviewDir);
             $resultSaveImagePreview = $imagePreview->save($imagePreviewDir . $imageName);
             if ($resultSaveImagePreview) {
                 $this->avatar = $imageDatePath . $imageName;
                 return true;
             }
         }
     }
     return false;
 }
Beispiel #4
0
 /**
  * Execute a watermarking.
  *
  * @param   Image    $image     watermarking Image
  * @param   integer  $offset_x  offset from the left
  * @param   integer  $offset_y  offset from the top
  * @param   integer  $opacity   opacity of watermark
  * @return  void
  */
 protected function _do_watermark(Image $watermark, $offset_x, $offset_y, $opacity)
 {
     if (empty(self::$_available_functions[self::IMAGELAYEREFFECT])) {
         throw new ErrorException('This method requires :function, which is only available in the bundled version of GD', array(':function' => 'imagelayereffect'));
     }
     // Loads image if not yet loaded
     $this->_load_image();
     // Create the watermark image resource
     $overlay = imagecreatefromstring($watermark->render());
     imagesavealpha($overlay, TRUE);
     // Get the width and height of the watermark
     $width = imagesx($overlay);
     $height = imagesy($overlay);
     if ($opacity < 100) {
         // Convert an opacity range of 0-100 to 127-0
         $opacity = round(abs($opacity * 127 / 100 - 127));
         // Allocate transparent gray
         $color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity);
         // The transparent image will overlay the watermark
         imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
         // Fill the background with the transparent color
         imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
     }
     // Alpha blending must be enabled on the background!
     imagealphablending($this->_image, TRUE);
     if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height)) {
         // Destroy the overlay image
         imagedestroy($overlay);
     }
 }
Beispiel #5
0
 protected function _do_watermark(Image $image, $offset_x, $offset_y, $opacity)
 {
     // Convert the Image intance into an Imagick instance
     $watermark = new Imagick();
     $watermark->readImageBlob($image->render(), $image->file);
     if ($watermark->getImageAlphaChannel() !== Imagick::ALPHACHANNEL_ACTIVATE) {
         // Force the image to have an alpha channel
         $watermark->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE);
     }
     if ($opacity < 100) {
         // NOTE: Using setImageOpacity will destroy current alpha channels!
         $watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity / 100, Imagick::CHANNEL_ALPHA);
     }
     // Match the colorspace between the two images before compositing
     // $watermark->setColorspace($this->im->getColorspace());
     // Apply the watermark to the image
     return $this->im->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, $offset_x, $offset_y);
 }