Laravel Intervention Saving Image in a specific disk
Asked Answered
T

3

6

I'm trying to use Intervention to resize an image, then save it in a disk that I've defined, but I can't get it to work. Using public_folder is saving it in a folder in the root of my app called /public

I've created a new disk:

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'visibility' => 'public',
        ],

        'images' => [
            'driver' => 'local',
            'root' => storage_path('app/public/images'),
            'visibility' => 'public',
        ],

    ],

And I need to accept input from the user, then save their uploaded file to that disk, then the filepath to the database.

    $fieldFile = $request->file($fieldName);
    $imageName = time();
    Image::make($fieldFile)->crop(350, 350)->save(public_path() . '/' . $imageName . '.' . $fieldFile->getClientOriginalExtension());
    $object->$fieldName = public_path() . '/' . $imageName . '.' . $fieldFile->getClientOriginalExtension();

I'd like to end up with something similar to what should happen from

$fieldValue = $fieldFile->store($fieldName, 'images');

The file should end up at

/path/to/laravel/storage/app/public/images/

Later, if I change the images disk to s3 or somewhere else, I'd like this code to continue to function.

Trice answered 26/2, 2017 at 0:19 Comment(1)
Can I save image in Drive D:/ in windows file?Etsukoetta
A
9

You need to encode the image like so

$fieldFile = $request->file($fieldName);

$mime= $fieldFile->getClientOriginalExtension();
$imageName = time().".".$mime;

$image = Image::make($fieldFile)->crop(350, 350)

Storage::disk('public')->put("images/".$imageName, (string) $image->encode());
Aixlachapelle answered 21/11, 2018 at 16:59 Comment(0)
L
0
$fieldFile = $request->file($fieldName);
//$imageName = time();

$mime= $fieldFile->getClientOriginalExtension();
$imageName = time().".".$mime;
$image = Image::make($fieldFile)->crop(350, 350)

$location = Storage::disk('images')->put($imageName, $image);
//images can be substituted for any disk local or s3 etc.
Liederman answered 26/2, 2017 at 1:21 Comment(0)
A
0

I have a helper controller you can try it. https://github.com/RashiqulRony/Help_Content/blob/master/MediaController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Image;

class MediaController
{
    public $basePath = '';
    public $originalPath = '';
    public $file = '';
    public $name = '';
    public $thumbPath = '';
    public $thumb = false;
    public $storageFolder = 'storage/';
    public $imageResize = [];
    public $thumbResize = [300, 300];

    //Common File Upload Function...
    private function upload()
    {
        $file = $this->file;
        if ($this->name) {
            $fileName = Str::slug($this->name, '-').'.'.$file->getClientOriginalExtension();
        } else {
            $newName = str_replace('.'.$file->getClientOriginalExtension(), '', $file->getClientOriginalName());
            $fileName = time().'-'.Str::slug($newName, '-').'.'.$file->getClientOriginalExtension();
        }
        $data['name'] = $fileName;
        $data['originalName'] = $file->getClientOriginalName();
        $data['size'] = $file->getSize();
        $data['mime_type'] = $file->getMimeType();
        $data['ext'] = $file->getClientOriginalExtension();
        $data['url'] = url($this->storageFolder.$this->originalPath.$data['name']);

        //If real image need to resize...
        if (!empty($this->imageResize)) {
            $file = Image::make($file)->resize($this->imageResize[0], $this->imageResize[1]);
            Storage::put($this->originalPath.'/'.$fileName, (string) $file->encode());
        } else {
            Storage::putFileAs($this->originalPath, $file, $data['name']);
        }

        if ($this->thumb) {
            Image::make($this->storageFolder.$this->originalPath.$data['name'])
                ->resize($this->thumbResize[0], $this->thumbResize[1])
                ->save($this->storageFolder.$this->thumbPath.'/'.$data['name']);
        }
        return $data;
    }

    //Upload Image ("$definePath" and "$definePath/thumb") folder....
    public function imageUpload($requestFile, $path, $thumb = false, $name = null, $imageResize = [], $thumbResize = [300, 300])
    {
        //Path Create...
        $realPath = $this->basePath.$path.'/';
        if (!Storage::exists($realPath)) {
            Storage::makeDirectory($realPath);
        }

        if (!Storage::exists($realPath.'thumb') && $thumb) {
            Storage::makeDirectory($realPath.'thumb');
        }

        $this->file = $requestFile;
        $this->originalPath = $realPath;
        $this->thumbPath = $realPath.'thumb';
        $this->thumb = $thumb;
        $this->name = $name;
        $this->imageResize = $imageResize;
        $this->thumbResize = $thumbResize;
        return $this->upload();
    }

    //Upload Video in "$definePath" folder....
    public function videoUpload($requestFile, $path, $name = null)
    {
        //Path Create...
        $realPath = $this->basePath.$path.'/';
        if (!Storage::exists($realPath)) {
            Storage::makeDirectory($realPath);
        }

        $this->file = $requestFile;
        $this->originalPath = $realPath;
        $this->name = $name;
        return $this->upload();
    }

    //Upload AnyFile in "$definePath" folder....
    public function anyUpload($requestFile, $path, $name = null)
    {
        //Path Create...
        $realPath = $this->basePath.$path.'/';
        if (!Storage::exists($realPath)) {
            Storage::makeDirectory($realPath);
        }

        $this->file = $requestFile;
        $this->originalPath = $realPath;
        $this->name = $name;
        return $this->upload();
    }


    //Upload Content in "$definePath" folder....
    public function contentUpload($content, $path, $name)
    {
        //Path Create...
        $realPath = $this->basePath.$path.'/';
        if (!Storage::exists($realPath)) {
            Storage::makeDirectory($realPath);
        }

        Storage::put($name, $content);

        $data['name'] = $name;
        $data['url'] = $path.'/'.$name;
        return $data;
    }

    //Only thumb image create in "$definePath/thumb" folder....
    public function thumb($path, $file, $thumbPath = false, $thumbWidth = 300, $thumbHeight = 300)
    {
        $realPath = $this->basePath.$path;
        if (!$thumbPath) {
            $thumbPath = $this->basePath.$path.'/thumb';
        }

        if (!Storage::exists($thumbPath)) {
            Storage::makeDirectory($thumbPath);
        }

        $img = Image::make($this->storageFolder.$realPath.'/'.$file)
            ->resize($thumbWidth, $thumbHeight)
            ->save($this->storageFolder.$thumbPath.'/'.$file);

        if (isset($img->filename)) {
            return true;
        } else {
            return false;
        }
    }

    //Delete file "$definePath" folder....
    public function delete($path, $file, $thumb = false)
    {
        $path = $this->basePath.$path.'/';
        if (Storage::exists($path.'/'.$file)) {
            Storage::delete($path.'/'.$file);

            if ($thumb) {
                Storage::delete($path.'/thumb/'.$file);
            }
            return true;
        }
        return false;
    }
}
Anosmia answered 28/10, 2020 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.