PHP code example of ankitpokhrel / laravel-image

1. Go to this page and download the library: Download ankitpokhrel/laravel-image 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/ */

    

ankitpokhrel / laravel-image example snippets


composer 

AnkitPokhrel\LaravelImage\ImageUploadServiceProvider::class

php artisan vendor:publish --provider="AnkitPokhrel\LaravelImage\ImageUploadServiceProvider"

class ContentsController extends Controller
{
    ...

    protected $image;

    /**
     * @param ImageUploadService $image
     */
    public function __construct(ImageUploadService $image)
    {
        ...

        //set properties for file upload
        $this->image = $image;
        $this->image->setUploadField('image'); //default is image
        $this->image->setUploadFolder('contents'); //default is public/uploads/contents
    }

    ...


/**
 * Store method
 */
public function store(ContentRequest $request)
{
    $input = $request->all();

    if (Input::hasFile($this->image->getUploadField()) && $this->image->upload()) {
        //image is uploaded, get uploaded image info
        $uploadedFileInfo = $this->image->getUploadedFileInfo();

        ...
        //do whatever you like with the information
        $input = array_merge($input, $uploadedFileInfo);
    } else {
        //get validator object
        $validator = $this->image->getValidationErrors();
        
        //get error messages
        $errors = $validator->messages()->all();
    }

    ...
}

/**
 * Update method
 */
public function update(Request $request, $id)
{
    ...

    if (Input::hasFile($this->image->getUploadField()) && $this->image->upload()) {
        ...

        //remove old files
        if (!empty(trim($model->file_path))) {
            $this->image->clean(public_path($model->file_path), true);
        }
    }

    ...
}

image               : Saved image name
upload_dir          : Image upload path
original_image_name : Real name of uploaded image
size                : Size of the uploaded image
extension           : Extension of the uploaded image
mime_type           : Mime type of the uploaded image

//set base path
$this->image->setBasePath('uploads/user-images/');

//set upload folder
$this->image->setUploadFolder('users');

//set absolute base path
$this->image->setBasePath('/absolute/path/to/your/folder/uploads/', true);

//set upload folder
$this->image->setUploadFolder('users');
html
<-- @laravelImage(uploadDir, imageName, width, height, options, attributes) -->
@laravelImage($uploadDir, $imageName, 300, 200, [
    'fit' => 'crop-top-left',
    'filt' => 'sepia'
], [
    'alt' => 'Alt text of an image',
    'class' => 'custom-class'
])