### Install Laravel ImageUp via Composer
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Installs the qcod/laravel-imageup package using Composer. This command fetches and installs the latest version of the package into your Laravel project.
```bash
composer require qcod/laravel-imageup
```
--------------------------------
### after_save Hook Example (PHP)
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
An example of using an 'after_save' hook to perform an action after the image has been saved to disk, such as creating a watermark.
```php
$user->setImagesField([
'logo' => [
'after_save' => function($image) {
// Create a watermark image and save it
},
]
]);
```
--------------------------------
### Handle Image Uploads in Laravel Controller
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Shows a basic example of how to handle image uploads in a Laravel controller using the User model with the HasImageUploads trait. The trait automatically manages the image upload process when the model is saved.
```php
all());
}
}
```
--------------------------------
### Get File URL
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Retrieves the public URL for a given file field. This can be used for linking to documents or other files.
```php
$user = User::findOrFail($id);
// echo $user->fileUrl('resume');
```
--------------------------------
### Get Image URL
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Retrieves the public URL for a given image field. This is typically used in views to display the image.
```php
$user = User::findOrFail($id);
// in your view
//
```
--------------------------------
### Running Tests (Bash)
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Command to run the package's integration and smoke tests using phpunit.
```bash
$ composer test
```
--------------------------------
### Running Tests in laravel-imageup
Source: https://github.com/qcod/laravel-imageup/blob/master/CONTRIBUTING.md
This snippet shows the command to execute tests for the laravel-imageup project using PHPUnit.
```bash
$ vendor/bin/phpunit
```
--------------------------------
### Configuration File (PHP)
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
The default configuration settings for the Laravel Imageup package, including upload disk, directory, auto-upload behavior, auto-delete images, and resize quality.
```php
'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
];
```
--------------------------------
### Publish Laravel ImageUp Configuration
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Publishes the configuration file for Laravel ImageUp using the artisan vendor:publish command. This creates a config/imageup.php file with all customizable settings.
```bash
php artisan vendor:publish --provider="QCod\ImageUp\ImageUpServiceProvider" --tag="config"
```
--------------------------------
### Create Storage Symlink for Public Access
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Creates a symbolic link from the public directory to the storage directory. This command is essential for accessing uploaded files (images) through public URLs in a Laravel application.
```bash
php artisan storage:link
```
--------------------------------
### Laravel ImageUp Field Options Configuration
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Demonstrates how to configure various upload options for image and file fields within a Laravel Eloquent model using the HasImageUploads trait. This includes settings for resizing, cropping, disk, path, validation, placeholders, and custom save hooks.
```php
[
// 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-based Image Hooks (PHP)
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Defines image hooks using class names. The hook class must have a 'handle' method that accepts an Intervention Image instance. These classes are resolved via Laravel's IOC container.
```php
protected static $imageFields = [
'avatar' => [
'before_save' => BlurFilter::class,
],
'cover' => [
//...
]
];
class BlurFilter {
public function handle($image) {
$image->blur(10);
}
}
```
--------------------------------
### Callback Image Hooks (PHP)
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Defines image hooks using anonymous functions (callbacks). The callback receives an Intervention Image instance as an argument, allowing for direct image manipulation.
```php
$user->setImagesField([
'avatar' => [
'before_save' => function($image) {
$image->blur(10);
},
],
'cover' => [
//...
]
]);
```
--------------------------------
### Dynamic Field Configuration
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Dynamically sets image or file field options, overriding those defined in the model. This allows for per-instance configuration of upload parameters like dimensions and crop settings.
```php
$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);
```
--------------------------------
### Use HasImageUploads Trait in Eloquent Model
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Demonstrates how to use the HasImageUploads trait in a Laravel Eloquent model. It shows how to define image fields that will be automatically handled for uploads, resizing, and cropping.
```php
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');
```
--------------------------------
### Resizing in before_save Hook (PHP)
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Demonstrates how to resize an image within a 'before_save' hook. The resize dimensions specified in the hook will override any 'width' or 'height' options defined for the field.
```php
$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);
},
]
]);
```
--------------------------------
### Manual Image/File Upload
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Uploads an image or file for a specified field. If the field is null, it defaults to the first defined image/file option. Ensure auto-upload is disabled to prevent overwrites.
```php
$user = User::findOrFail($id);
$user->uploadImage(request()->file('cover'), 'cover');
$user->uploadFile(request()->file('resume'), 'resume');
```
--------------------------------
### Generate Image Tag
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Generates an HTML `
` tag for a specified image field, optionally including additional HTML attributes.
```html
{!! $model->imageTag('avatar') !!}
{!! $model->imageTag('avatar', 'class="float-left mr-3"') !!}
```
--------------------------------
### Resize Image
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Resizes an image using the field's configured options. This method returns an Intervention Image instance, which you must save manually. It can accept a file path or an uploaded file object.
```php
$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 ]);
```
--------------------------------
### Check if Field is Image/File
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Checks if a given field is configured as an image or file field within the trait's settings.
```php
$user->hasImageField('cover');
$user->hasFileField('resume');
```
--------------------------------
### Delete Image/File
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Deletes a specified image or file from the storage if it exists.
```php
$user->deleteImage('/path/to/image.jpg');
$user->deleteFile('/path/to/document.pdf');
```
--------------------------------
### Customizing Filename for Uploads
Source: https://github.com/qcod/laravel-imageup/blob/master/README.md
Illustrates how to customize the saved filename for uploaded files by defining a model method with the naming convention `{fieldName}UploadFilePath`. This method receives the uploaded file object and should return the desired relative path for the saved file.
```php
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';
}
}
```
```php
// 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()
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.