Giter Club home page Giter Club logo

yii2-images's Introduction

yii2-images

Guys, we definetly need to do something with this repo. I see several ways:

  • to write tests
  • to find active contributers
  • to find some alternative repo
What do you prefer? please let me know.

Yii2-images is yii2 module that allows to attach images to any of your models, next you can get images in any sizes, also you can set main image of images set.

Module supports Imagick and GD libraries, you can set up it in module settings.

Usage instance:

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

Details

  1. Get images

    $model->getImage(); //returns main image for model (first added image or setted as main)
    
    $model->getImages(); //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. Remove image/images

    $model->removeImage($image); //you must to pass image (object)
    
    $model->removeImages(); //will remove all images of this model
  3. Set main image

    $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. Get image sizes

    $image = $model->getImage();
    $sizes = $image->getSizesWhen('x500');
    echo '<img width="'.$sizes['width'].'" height="'.$sizes['height'].'" src="'.$image->getUrl('x500').'" />';
  5. Get original image

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

Installation

  1. Add Yii2-images to the require section of your composer.json file:

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

       php composer.phar update
     
  3. run migrate

     php yii migrate/up --migrationPath=@vendor/costa-rico/yii2-images/migrations
     
  4. setup module

    '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. attach behaviour to your model (be sure that your model has "id" property)

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

Thats all!

yii2-images's People

Contributors

akim004 avatar andku83 avatar arogachev avatar colinrlly avatar costarico avatar ctolet avatar evgenybukharev avatar rrhyne avatar synatree avatar vlykhoshva avatar vollossy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.