GithubHelp home page GithubHelp logo

laravel-images's Introduction

Image generation and caching in Laravel

This Laravel 4 package provides an easy way to generate and cache images on the fly

Installation

Begin by installing this package through Composer. Edit your project's composer.json file to require lifeentity/images.

"require": {
    "lifeentity/images": "2.*"
}

Next, update Composer from the Terminal:

composer update

Once this operation completes, the final step is to add the service provider. Open app/config/app.php, and add a new item to the providers array.

'Lifeentity\Images\ImagesServiceProvider'

Next, run this migrate command to create the images table (don't forget to configure your database)

php artisan migrate --package=lifeentity/images

Optional step, run this artisan command to publish the config file

php artisan config:publish lifeentity/images

The config file contains information about:

  • your images directory
  • the base path
  • the cache directory name

Usage

Attach any of your models to the image eloquent model.

<?php

use Lifeentity\Images\ImageDB;

class Product {
    // ...
    protected function images() {
        return $this->morphMany('Lifeentity\Images\ImageDB', 'imageable');
    }
}

Create new image and save it to the product

<?php
// Create a new image and attach it to a product
$image = new ImageDB(array(
    'path' => '/images/path/to/image/image.jpg'
));

Product::find(1)->image()->save($image);

Display product original image and resized version of the image

<?php
$image = Product::find(1);
echo '<img src="'.$image->original_url.'" />';

// Resize image before displaying to the user
echo '<img src="'.$image->addOperation('resize', 300, null, true)->cached_url.'" />';

Want to make complex operations on the image? Register an image filter

<?php
// Register new operation
App::make('Lifeentity\Images\ImageFilter')->register('watermark.v1', function(Intervention\Image\Image $image)
{
    // If you want you can add this facade to the aliases in the app.php config file
    $watermark = \Intervention\Image\Facades\Image::make(public_path('/images/watermark.jpg'));

    // For a list of operations you can do on an image please refer to intervention image package bellow
    $watermark->resize(0.4 * $image->getWidth(), null, function($constraints)
    {
        $constraints->aspectRatio();
    });

    $image->insert($watermark, 'bottom-right');
});

This package depends on intervention/image v2.*. For list of operations you can do on images follow this link Intervention Image

laravel-images's People

Contributors

kareemaly avatar

Watchers

 avatar

Forkers

henrydewa

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.