1. Go to this page and download the library: Download lidai/laravel-imageup library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lidai / laravel-imageup example snippets
Lidai\ImageUp\ImageUpServiceProvider::class
namespace App;
use Lidai\ImageUp\HasImageUploads;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
use HasImageUploads;
// assuming `users` table has 'cover', 'avatar' columns
// mark all the columns as image fields
protected static $imageFields = [
'cover', 'avatar'
];
}
namespace App;
use App\Http\Controllers\Controller;
class UserController extends Controller {
public function store(Request $request){
return User::create($request->all());
}
}
namespace App;
use Lidai\ImageUp\HasImageUploads;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
use HasImageUploads;
// which disk to use for upload, can be override by field options
protected $imagesUploadDisk = 'local';
// path in disk to use for upload, can be override by field options
protected $imagesUploadPath = 'uploads';
// auto upload allowed
protected $autoUploadImages = true;
// all the images fields for model
protected static $imageFields = [
'avatar' => [
// width to resize image after upload
'width' => 200,
// height to resize image after upload
'height' => 100,
// set true to crop image with the given width/height and you can also pass arr [x,y] coordinate for crop.
'crop' => true,
// what disk you want to upload, default config('imageup.upload_disk')
'disk' => 'public',
// a folder path on the above disk, default config('imageup.upload_directory')
'path' => 'avatars',
// placeholder image if image field is empty
'placeholder' => '/images/avatar-placeholder.svg',
// validation rules when uploading image
'rules' => 'image|max:2000',
// override global auto upload setting coming from config('imageup.auto_upload_images')
'auto_upload' => false,
// if request file is don't have same name, default will be the field name
'file_input' => 'photo',
// if field (here "avatar") don't exist in database or you wan't this field in database
'update_database' => false,
// a hook that is triggered before the image is saved
'before_save' => BlurFilter::class,
// a hook that is triggered after the image is saved
'after_save' => CreateWatermarkImage::class
],
'cover' => [
//...
]
];
// any other than image file type for upload
protected static $fileFields = [
'resume' => [
// what disk you want to upload, default config('imageup.upload_disk')
'disk' => 'public',
// a folder path on the above disk, default config('imageup.upload_directory')
'path' => 'docs',
// validation rules when uploading file
'rules' => 'mimes:doc,pdf,docx|max:1000',
// override global auto upload setting coming from config('imageup.auto_upload_images')
'auto_upload' => false,
// if request file is don't have same name, default will be the field name
'file_input' => 'cv',
// a hook that is triggered before the file is saved
'before_save' => HookForBeforeSave::class,
// a hook that is triggered after the file is saved
'after_save' => HookForAfterSave::class
],
'cover_letter' => [
//...
]
];
}
class User extends Model {
use HasImageUploads;
// assuming `users` table has 'cover', 'avatar' columns
// mark all the columns as image fields
protected static $imageFields = [
'cover', 'avatar'
];
// override cover file name
protected function coverUploadFilePath($file) {
return $this->id . '-cover-image.jpg';
}
}
// override cover file name
protected function coverUploadFilePath($file) {
return $this->id .'-'. $file->getClientOriginalName();
}
/** Some of methods on file */
// $file->getClientOriginalExtension()
// $file->getRealPath()
// $file->getSize()
// $file->getMimeType()
$user = User::findOrFail($id);
$fieldOptions = [
'cover' => [ 'width' => 1000 ],
'avatar' => [ 'width' => 120, 'crop' => true ],
];
// override image fields defined on model
$user->setImagesField($fieldOptions);
$fileFieldOption = [
'resume' => ['path' => 'resumes']
];
// override file fields defined on model
$user->setFilesField($fileFieldOption);
$user = User::findOrFail($id);
// resize image, it will give you resized image, you need to save it
$imageFile = '/images/some-big-image.jpg';
$image = $user->resizeImage($imageFile, [ 'width' => 120, 'crop' => true ]);
// or you can use uploaded file
$imageFile = request()->file('avatar');
$image = $user->resizeImage($imageFile, [ 'width' => 120, 'crop' => true ]);
$user = User::findOrFail($id);
// uploaded file from request
$imageFile = request()->file('avatar');
// coordinates from request
$coords = request()->only(['crop_x', 'crop_y']);
// resizing will give you intervention image back
$image = $user->cropTo($coords)
->resizeImage($imageFile, [ 'width' => 120, 'crop' => true ]);
// or you can do upload and resize like this, it will override field options crop setting
$user->cropTo($coords)
->uploadImage(request()->file('cover'), 'avatar');
$user = User::findOrFail($id);
// in your view
<img src="{{ $user->imageUrl('cover') }}" alt="" />
// http://www.example.com/storage/uploads/iGqUEbCPTv7EuqkndE34CNitlJbFhuxEWmgN9JIh.jpeg
$user->setImagesField([
'avatar' => [
'width' => 100,
'height' => 100,
'before_save' => function($image) {
// The image will be 50 * 50, this will override the 100 * 100
$image->resize(50, 50);
},
]
]);
$user->setImagesField([
'logo' => [
'after_save' => function($image) {
// Create a watermark image and save it
},
]
]);
return [
/**
* Default upload storage disk
*/
'upload_disk' => 'public',
/**
* Default Image upload directory on the disc
* eg. 'uploads' or 'user/avatar'
*/
'upload_directory' => 'uploads',
/**
* Auto upload images from incoming Request if same named field or
* file_input field on option present upon model update and create.
* can be override in individual field options
*/
'auto_upload_images' => true,
/**
* It will auto delete images once record is deleted from database
*/
'auto_delete_images' => true,
/**
* Set an image quality
*/
'resize_image_quality' => 80
];