Downloads
Stars
Version
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.
$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
foreach ($product->secondary_images as $image) {
echo $image->s;
}
if ($topic->has_image) {
//
}
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.
$topic->delete_image();
Or use delete_images()
to delete all the images connected to the model.
Install the package through Composer.
Run the Composer require command from the Terminal:
composer require levoolabs/imageable
Run migrations with artisan command:
php aritsan migrate
This will create the following table:
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();
});
Publish intervention config files and the Imageable default image with:
php artisan vendor:publish
For the simplest use just include SingleImageableTrait
or MultiImageableTrait
into your Eloquent model and you are all set.
class Topic extends Model
{
use \LevooLabs\Imageable\Traits\SingleImageableTrait;
protected $table = 'topics';
protected $fillable = [
'title'
];
}
Or you can extend flexibility by setting these properties:
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";
/* ... */
}
$template_base_name
contains the base name for the filters defined in the imagecache config file.$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
.$default_image_name
is the name of the placeholder image file located in public/images/imageable
folder for models without uploaded images.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.
'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.
LevooLabs Imageable is licensed under the MIT License.
Copyright 2018 LevooLabs