LevooLabs Imageable
LevooLabs Imageable is an easy to use Eloquent Image model for uploading and displaying images with intervention/imagecache. The package includes Traits to add simple connection between the Image model and any other Eloquent model., (*1)
Demo
Get the models image in multiple size
$topic = Topic::where('name', 'Awsome topic')->first();
echo $topic->image->s; // Small image url
echo $topic->image->m; // Medium image url
echo $topic->image->l; // Large image url
echo $topic->image->o; // Original image url
Get secondary images
foreach ($product->secondary_images as $image) {
echo $image->s;
}
Check whenever a model has an uploaded image
if ($topic->has_image) {
//
}
Upload image to the server
public function uploadImageAjax(Request $request, Topic $topic)
{
if ($request->ajax()) {
$image = $topic->store_image($request->file('file'));
return response()->json(['ok' => $image->id], 200);
}
abort(404);
}
Or use store_images($files, $image_type = null) for multiple images., (*2)
Delete image from the server
$topic->delete_image();
Or use delete_images() to delete all the images connected to the model., (*3)
Installation
Step 1: Install package
Install the package through Composer., (*4)
Run the Composer require command from the Terminal:, (*5)
composer require levoolabs/imageable
Step 2: Migrations
Run migrations with artisan command:, (*6)
php aritsan migrate
This will create the following table:, (*7)
Schema::create('images', function(Blueprint $table) {
$table->increments('id');
$table->integer('imageable_id')->unsigned();
$table->string('imageable_type');
$table->string('image_type');
$table->string('image_path'); #NOTE relative filename with extension -> /folder/folder/filename.ext
$table->timestamps();
});
Step 3: Publish assets
Publish intervention config files and the Imageable default image with:, (*8)
php artisan vendor:publish
Step 4.1: Traits
For the simplest use just include SingleImageableTrait or MultiImageableTrait into your Eloquent model and you are all set., (*9)
class Topic extends Model
{
use \LevooLabs\Imageable\Traits\SingleImageableTrait;
protected $table = 'topics';
protected $fillable = [
'title'
];
}
Or you can extend flexibility by setting these properties:, (*10)
class Product extends Model
{
use \LevooLabs\Imageable\Traits\MultiImageableTrait;
public $template_base_name = "product";
protected $image_type = MyConstants\ImageType::PRODUCT_MAIN;
protected $secondary_image_type = MyConstants\ImageType::PRODUCT;
protected $default_image_name = "product.jpg";
protected $extension = "jpg";
/* ... */
}
- The
$template_base_name contains the base name for the filters defined in the imagecache config file.
- The
$image_type and $secondary_image_type properties hold the value for image_type column in the images table. The $secondary_image_type will only be used in MultiImageableTrait.
- The
$default_image_name is the name of the placeholder image file located in public/images/imageable folder for models without uploaded images.
Step 4.2: Custom filters (optional)
If you set the $template_base_name value in your model you have to define the filters for that template in the config/imagecache.php file., (*11)
'product' => \App\ImageFilters\Product\Upload::class,
'product-s' => \App\ImageFilters\Product\Small::class,
'product-m' => \App\ImageFilters\Product\Medium::class,
'product-l' => \App\ImageFilters\Product\Large::class,
You can read more about Intervention Image Filters here., (*12)
License
LevooLabs Imageable is licensed under the MIT License., (*13)
Copyright 2018 LevooLabs, (*14)