circulon/yii2-images

用于存储图片的yii2模块

安装: 82

依赖者: 0

推荐者: 0

安全: 0

星标: 1

关注者: 1

分支: 98

类型:yii2-extension

1.3.1 2015-06-23 00:21 UTC

This package is auto-updated.

Last update: 2024-09-04 22:55:28 UTC


README

Yii2-images是一个yii2模块,允许将图片附加到任何模型,您还可以以任何大小检索图片。此外,您还可以设置一组图片的主(默认)图片。

模块支持Imagick和GD库,您可以在模块设置中设置。

特性

  • 一个可以附加到任何控制器以提供更干净URL的单个操作
  • 可选输出base64编码的数据,用于在标签中使用
  • 针对每个操作优化数据库中的图片引用搜索
  • 可自定义id属性
  • 内部处理UploadedFile类,因此无需在附加到模型之前先保存上传

安装

安装此扩展的首选方式是通过composer

运行以下命令之一

php composer.phar require --prefer-dist circulon/yii2-images "*"

或将

"circulon/yii2-images": "*"

添加到您的composer.json文件的require部分。

运行迁移

php yii migrate/up --migrationPath=@vendor/circulon/yii2-images/migrations

设置

将模块设置添加到您的应用程序配置中

'modules' => [
	...
	'images' => [
    	'class' => 'circulon\images\Module',
        // be sure, that permissions ok 
        // if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
        'imagesStorePath' => 'images/store', //path to origin images
        'imagesCachePath' => 'images/cache', //path to resized copies
        'graphicsLibrary' => 'GD', //but really its better to use 'Imagick' 
        'placeholderPath' => '@webroot/images/placeholder.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
    ],
],

可选地添加URL路由到UrlManager

注意:您可能需要将类似的规则添加到具有此附加操作的模块中

'components' => [
    ...
    'urlManager' => [
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'rules' => [
          ...
         
          '<controller:\w+>/<action:\w+>/<id:\d+>/<ref:[a-z0-9_-]+>' => '<controller>/<action>',
          
          ...
       ],
    ],
    ...
]

将行为附加到您的模型

 	public function behaviors(){
    	return [
        	'image' => [
            	'class' => 'circlulon\images\behaviors\ImageBehavior',
              	'idAttribute' => 'id' // set the models id column , default : 'id'
          	]
      	];
  	}
    

将操作添加到所需的控制器中

	public function actions(){
    	return [
        	'image' => [
          		'class' => 'circulon\images\actions\ImageAction',
          		
              // all the model classes to be searched by this action.
              // Can be fully qualified namespace or alias
          		'models' => ['User', ...]  
	        ]
	    ];
	}

使用方法

    $model = Model::findOne(12); //Model must have id
    
    //If an image is first it will be main image for this model
    $model->attachImage('../../image.png');
    
    //But if you need set another image as main, use second arg
    $model->attachImage('../../image2.png', true);
    
    //get all images
    $images = $model->getImages();
    foreach($images as $img){
        //retun url to full image
        echo $img->getUrl();
        
        //return url to proportionally resized image by width
        echo $img->getUrl('300x');
    
        //return url to proportionally resized image by height
        echo $img->getUrl('x300');
        
        //return url to resized and cropped (center) image by width and height
        echo $img->getUrl('200x300');
    }
    
    // get image model 
    $image = $model->getImage();
    
    if($image){
        //get path to resized image 
        echo $image->getPath('400x300');
        
        //path to original image
        $image->getPathToOrigin();
        
        //will remove this image and all cache files
        $model->removeImage($image);
        
        // get the content of the image
        $model->getContent();
    }

    

使用img标签

    <!-- create a thumbnail sized image with base64 encoding for fast display -->
    <img src="data:image/png;base64,<?= $user->getImage()->getContent('50x50', true) ?>" alt="">

详情

  1. 获取图片

    $model->getImage(); //returns main image for model (first added image or setted as main)
    
    $model->removeImages(); //returns array with images
    
    //If there is no images for model, above methods will return Placeholder image or null
    //If you want placeholder set up it in module configuration (see documentation)
  2. 删除图片/图片

    $model->removeImage($image); //you must to pass image (object)
    
    $model->removeImages(); //will remove all images of this model
  3. 设置主/默认图片

    $model->attachImage($absolutePathToImage, true); //will attach image and make it main
    
    foreach($model->getImages() as $img){
        if($img->id == $ourId){
            $model->setMainImage($img);//will set current image main
        }
    }
  4. 获取图片大小

    $image = $model->getImage();
    $sizes = $image->getSizesWhen('x500');
    
    // the url is relative to the current controller eg /site/image/2/876293878623-x500
    echo '<img width="'.$sizes['width'].'" height="'.$sizes['height'].'" src="'.$image->getUrl('x500').'" />';
    
    // using a different controller / module+controller 
    //  example generated url /product/image/2/876293878623-x500
    echo '<img width="'.$sizes['width'].'" height="'.$sizes['height'].'" src="'.$image->getUrl('x500','product').'" />';
  5. 获取原始图片

    $img = $model->getImage();
    echo $img->getPathToOrigin();
  6. 获取原始图片内容或编码内容

    $image = $model->getImage();
    $content = $model->getContent();
    
    // output base64 encoded thumbnail with bootstrap3 css
    echo '<img class="img-responsive img-rounded" src="data:image/png;base64,'.$user->getImage()->getContent('50x50', true).'" alt="">';