Skip to content

sudippalash/mediauploader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mediauploader comes to Laravel

[![Latest Version on Packagist][ico-version]][link-packagist] ![Software License][ico-license] [![Total Downloads][ico-downloads]][link-downloads]

mediauploader is a simple file handling package of Laravel that provides variety of options to deal with files. such as Image upload, file upload, webp upload, base64 image, upload from URL, and upload any type of contents

Install

Via Composer

$ composer require sudippalash/mediauploader

For Laravel <= 5.4

After updating composer, add the ServiceProvider to the providers array in config/app.php

Sudip\MediaUploader\MediaUploaderServiceProvider::class,

You can use the facade for shorter code. Add this to your aliases:

'MediaUploader' => Sudip\MediaUploader\Facades\MediaUploader::class,

Publish config file

You will need to publish config file to add mediauploader global path.

php artisan vendor:publish --provider="Sudip\MediaUploader\MediaUploaderServiceProvider" --tag=config

In config/mediauploader.php config file you should set mediauploader global path.

    return [
        /*
        |--------------------------------------------------------------------------
        | Base Directory
        |--------------------------------------------------------------------------
        |
        | base_dir stores all other directory inside storage folder of your laravel application by default
        | if you specify any name. all storage will be done inside that directory or name that you specified
        |
        */

        'base_dir' => '',

        /*
        |--------------------------------------------------------------------------
        | Thumb Directory
        |--------------------------------------------------------------------------
        |
        | thumb_dir creates another folder inside the directory as a "thumb" by default
        | you can change the name thumb to any other name you like.
        */

        'thumb_dir' => 'thumb',

        /*
        |--------------------------------------------------------------------------
        | Timestamp Prefix
        |--------------------------------------------------------------------------
        |
        | If timestamp_prefix is true then create a file with a timestamp to ignore the same name image replacement. Example: image-1658562981.png.
        | If timestamp_prefix is false then the script checks file exists or not if the file exists then add the time() prefix for the new file otherwise leave it as the file
        | name.
        */

        'timestamp_prefix' => false,

        /*
        |--------------------------------------------------------------------------
        | Thumb Image Height Width
        |--------------------------------------------------------------------------
        |
        | specify the thumb image ratio of height and weight by default it takes 300px X 300px
        */

        'image_thumb_height' => 300,
        'image_thumb_width' => 300,

        /*
        |--------------------------------------------------------------------------
        | Fake Image Url
        |--------------------------------------------------------------------------
        |
        | fake_image_url , if you specify a fake image path here. the entire package will use
        | this image when there is no image found. or you can specify the fake image in the
        | function parameter as well.
        | Example: 'fake_image_url' => url('images/fake.png'),
        */

        'fake_image_url' => null,

        /*
        |--------------------------------------------------------------------------
        | Folder permission
        |--------------------------------------------------------------------------
        |
        | path_permission , if you create a folder in your project then you can define your folder permission.
        | Example: null, 0755, 0777
        */

        'path_permission' => 0777,
    ];

Configuartion Important

Before jumping to use this, you must do the following:

  1. Please make sure to link your storage before using this package
php artisan storage:link
  1. Change your env filesystem drive to this
FILESYSTEM_DISK=public

Usage (File Upload)

  1. Image Upload
 MediaUploader::imageUpload(<UploadedFile image>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);

Example:

    $file = MediaUploader::imageUpload($request->file, 'images', 1, null, [600, 600]);

    if ($file) {
        // File is saved successfully
    }
  1. Webp Image Upload
 MediaUploader::webpUpload(<UploadedFile image>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);

Example:

    $file = MediaUploader::webpUpload($request->file, 'webps', 1, null, [600, 600]);

    if ($file) {
        // File is saved successfully
    }
  1. Any Upload (if you are not sure what type of file is, you can use this)
 MediaUploader::anyUpload(<UploadedFile file>, <output path>, name=null);

Example:

    $file = MediaUploader::anyUpload($request->file, 'files', 'filename');

    if ($file) {
        // File is saved successfully
    }
  1. Thumb (to create a thumb from an existing image, you can use this)
 MediaUploader::thumb(<output path>, <file>, $thumbPath = false, $thumbWidth = 0, $thumbHeight = 0);

Example:

    $file = MediaUploader::thumb('thumbs', $file, 200, 200);

    if ($file) {
        // File is saved successfully
    }
  1. Image Upload from URL
 MediaUploader::imageUploadFromUrl(<Valid Image URL>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);

Example:

    $file = MediaUploader::imageUploadFromUrl($url, 'images', 1, null, [600, 600]);

    if ($file) {
        // File is saved successfully
    }
  1. Base64 image Upload
 MediaUploader::base64ImageUpload(<base64 Content>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);

Example:

    $file = MediaUploader::base64ImageUpload($content, 'images', 1, null, [600, 600]);

    if ($file) {
        // File is saved successfully
    }
  1. Content Upload
 MediaUploader::contentUpload(<Content>, <output path>, name=null);

Example:

    $file = MediaUploader::contentUpload($content, 'contents', 'name');

    if ($file) {
        // File is saved successfully
    }

Returns:

Response from every function looks like this

    [
        "name" => "example-image.jpg"
        "originalName" => "example-image.jpg"
        "size" => 148892
        "width" => 600
        "height" => 600
        "mime_type" => "image/jpeg"
        "ext" => "jpg"
        "url" => "http://localhost/storage/test/example-image.jpg"
    ]

Usage (File Delete)

File Delete

 MediaUploader::delete(<path>, <file_name>, $thumb = false)

Example:

    $file = MediaUploader::delete('images', $file_name, true);

    if ($file) {
        // File is deleted successfully
    }

Usage (Directory Delete)

Directory Delete

 MediaUploader::removeDirectory(<path>)

Example:

    $path = MediaUploader::removeDirectory('images');

    if ($path) {
        // Directory is deleted successfully
    }

Usage (FIle/Image Exists, FIle/Image URL, File Preview & Image Preview)

  1. FIle/Image Exists
 MediaUploader::fileExists(<path>, <file_name>)

Example:

    {!! MediaUploader::fileExists('images', $file_name) !!}
  1. FIle/Image Url
 MediaUploader::showUrl(<path>, <file_name>)

Example:

    {!! MediaUploader::showUrl('images', $file_name) !!}
  1. File Preview
 MediaUploader::showFile(<path>, <file_name>)

Example:

    {!! MediaUploader::showFile('images', $file_name) !!}
  1. Image Preview
 MediaUploader::showImg(<path>, <file_name>, <array options>)

Example:

    {!! MediaUploader::showImg('images', $file_name, [
        'thumb' => true, // If thumb image store via upload function.
        'popup' => true, // Currently support only jQuery fancybox.
        'fakeImg' => 'images/avatar.png', // Pass fake image path without url or pass true (if pass true it will generate fake image from config file fake_image_url value).
        'id' => 'image',
        'class' => 'img-fluid',
        'style' => '',
        'alt' => 'Nice Image',
    ]) !!}