rrhyne/yii2-images

yii2 图片存储模块

维护者

详细信息

github.com/rrhyne/yii2-images

源代码

安装: 41

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 97

类型:yii2-extension

1.0.4 2014-10-27 15:14 UTC

This package is not auto-updated.

Last update: 2024-09-11 09:13:55 UTC


README

Yii2-images 是一个 yii2 模块,允许您将图片附加到任何模型,接下来您可以获取任何尺寸的图片,还可以设置图片集的主图片。

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

使用示例

$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');
}

//Returns main model image
$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);
}

详细信息

  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 images 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');
    echo '<img width="'.$sizes['width'].'" height="'.$sizes['height'].'" src="'.$image->getUrl('x500').'" />';
  5. 获取原始图片

    $img = $model->getImage();
    echo $img->getPathToOrigin();

安装

  1. 将 Yii2-user 添加到您的 composer.json 文件的 require 部分

        {
             "require": {
                 "costa-rico/yii2-images": "dev-master"
             }
        }
     
  2. 运行

       php composer.phar update
     
  3. 运行迁移

     php yii migrate/up --migrationPath=@vendor/costa-rico/yii2-images/migrations
     
  4. 设置模块

    'modules' => [
            'yii2images' => [
                'class' => 'rico\yii2images\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
            ],
        ],
  5. 将行为附加到您的模型(请确保您的模型有 "id" 属性)

        public function behaviors()
        {
            return [
                'image' => [
                    'class' => 'rico\yii2images\behaviors\ImageBehave',
                ]
            ];
        }

这就完成了!