Exemplo n.º 1
0
 /**
  * Loads the image to Kohana_Image object
  * @param string $file the file path to the image
  * @param string $driver the image driver to use: GD or ImageMagick
  * @throws ErrorException if filename is empty or file doesn't exist
  * @return mixed object Image_GD or object Image_Imagick 
  */
 public function load($file = null, $driver = null)
 {
     if (empty($file)) {
         throw new ErrorException('File name can not be empty');
     }
     if (!realpath($file)) {
         throw new ErrorException(sprintf('The file doesn\'t exist: %s', $file));
     }
     return Image::factory($file, $driver ? $driver : $this->driver);
 }
 public function actionUploadModal()
 {
     $message = false;
     $status = true;
     Yii::$app->response->format = Response::FORMAT_JSON;
     // Загружаем файл
     $file_upload = UploadedFile::getInstanceByName('images');
     if (empty($file_upload)) {
         return [];
     }
     // Сохраняем файл
     $file_name = md5($file_upload->name . uniqid());
     $file_ext = strrchr($file_upload->name, '.');
     $file_dir = substr($file_name, 0, 2);
     $file_base_path = Yii::getAlias($this->module->base_path);
     FileHelper::createDirectory($file_base_path, 0777, true);
     $file_path = Yii::getAlias($this->module->base_path) . DIRECTORY_SEPARATOR . $file_dir;
     FileHelper::createDirectory($file_path, 0777, true);
     $file = $file_path . DIRECTORY_SEPARATOR . $file_name . $file_ext;
     //$imageTmp = Image::getImagine()->open($file_upload->tempName)->save($file);
     $imageTmp = Image::factory($file_upload->tempName, $this->module->image_driver);
     $imageTmp->save($file, 100);
     // Сохраняем в базу данных
     $model = new ImagesCommon();
     $model->exp = $file_ext;
     $model->title = Yii::$app->request->post('name');
     $model->name = $file_name;
     $model->path = $file_dir;
     $model->is_active = 1;
     if (!$model->save()) {
         Yii::error(['msg' => 'Ошибка сохрание загруженного изображения', 'data' => ['error' => $model->errors]]);
         unlink($file);
         $message = 'Ошибка загрузки изображения';
         $status = false;
     }
     $url = $model->getImgOrigin();
     $allow_size = $this->module->allow_size;
     if (is_array($allow_size)) {
         $btn_img = Html::beginTag('div', ['class' => 'col-md-12']);
         $btn_img .= 'Размеры: ';
         foreach ($allow_size as $size) {
             $btn_img .= Html::a($size, ['/mitrm_images/images-common/size', 'size' => $size, 'path' => $model->path, 'name' => $model->name], ['class' => 'btn btn-info js_mitrm_get_size_img', 'target' => '_blank']);
         }
         $btn_img .= Html::endTag('div');
     }
     $preview = Html::img($url, ['style' => 'height: 100px;']);
     $url = 'http://' . $this->module->domain . $url;
     return ['status' => $status, 'url' => $url, 'message' => $message, 'preview' => $preview, 'img' => ['name' => $model->name, 'path' => $model->path], 'btn_img' => $btn_img];
 }
Exemplo n.º 3
0
 /**
  * @brief Отдает изображение под нужный размер
  * @detailed если изображения нет под нужный размер, генерирует его в реальном времени
  * @param $img
  * @param int $size
  * @return bool|string
  */
 public static function getImg($path, $img, $exp, $size = 500)
 {
     $dir = Yii::$app->getModule('mitrm_images')->base_dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR;
     $path = self::getPathImg() . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR;
     $image = $dir . $img . '_' . $size . 'x' . $size . $exp;
     $image_path = $path . $img . '_' . $size . 'x' . $size . $exp;
     if (file_exists($image_path)) {
         return $image;
     } else {
         $file = $path . $img . $exp;
         if (!file_exists($file)) {
             return false;
         }
         $file_new = $path . $img . '_' . $size . 'x' . $size . $exp;
         $imageTmp = Image::factory($file, Yii::$app->getModule('mitrm_images')->image_driver);
         $imageTmp->resize($size, $size);
         $imageTmp->save($file_new, 100);
         //$imageTmp = Image::getImagine()->open($file)->resize(new Box($size,$size))->save($file_new);
         return $image;
     }
 }