Author

Making Image Uploading Trait in Laravel

Image

by Samarth Sinha on 2017-04-17 04:38:49

For uploading image in laravel we can simply make a trait and use it in any model and do the image upload. We can also do validations on the image which is explained in http://blog.samarthasinha.com/blog/making-validation-trait-laravel and http://blog.samarthasinha.com/blog/using-events-laravel-eloquent link. The code for image validation is below:

<?php
namespace App\Traits;

use Image;
use Exception;
use Webpatser\Uuid\Uuid;

trait ImageTrait
{
    private $image_trait_file = null;
    protected $image_trait_path = null;
    public function getFile()
    {
        return $this->image_trait_file;
    }

    public function setFile($value)
    {
        $this->image_trait_file = $value;
    }

    protected function imageTraitProperties()
    {}

    private function base_directory()
    {
        return array_first([$this->image_trait_path, public_path()."/".env('IMAGE_REPO'), public_path().'/image_trait'], function ($value) {return $value != null;});
    }

    private function getImageUniqueId()
    {
        return array_first([$this->uuid, $this->id, Uuid::generate()], function ($value) {return $value != null;});
    }

    private function getImagePath($value)
    {
        return $this->base_directory() ."/".get_class($this)."/".$this->getImageUniqueId()."_".$value.".".$this->files->getClientOriginalExtension();
    }

    public function saveImage($data)
    {
        try
        {
            $img = Image::make($data);
            $img->backup();
            $img->save($this->getImagePath(), 100);
            foreach ($this->imageTraitProperties() as $version_identifier => $version_identifier_options)
            {
                if ($version_identifier_options['dimension'] != null)
                {
                    $img->resize($version_identifier_options['dimension'][0], $version_identifier_options['dimension'][1]);
                }
                $img->save($this->getImagePath(), ($version_identifier_options['quality'] * 100));
                $img->reset();
            }
        }
        catch (Exception $e)
        {
            $this->slack($e->getMessage());
            return false;
        }
        return true;
    }

    public function uploadImage()
    {
        try
        {
            if (!$this->saveImage($this->image_trait_file))
            {
                $this->addError(['image' => ['Something went wrong. Please try again']]);
                return false;
            }
        }
        catch (Exception $e)
        {
            $this->slack($e->getMessage());
            return false;
        }
        return true;
    }
}

Viewer Comments (1)

About the Author

  • avatar
    Samarth Sinha

    My name Samarth Sinha. I work on developing Web Applications using PHP and MySQL and in different frameworks of PHP like Laravel, CakePHP, CodeIgnitor and Smarty. In databases I have worked in MySQL and Postgres. I started this blog to share my knowledge to those who wants to start developing Web Applications using PHP.