Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanjayacloud's avatar

Image source not readable in intervention image package

Hi all,

I am trying to resize the image using the intervention image package from my blade file. But I am getting below error while I am doing that. Can anyone help me with this?

Error: Image source not readable

Code

@foreach($galleries as $gallery)
                         @php
                            $img = \Intervention\Image\Facades\Image::make('public/uploads/'.$gallery->image);
                            dd($img->encode('jpg', 1))
                         @endphp
                         <a href="{{asset('uploads/'.$gallery->image)}}" class="btn-gallery">
                             <img src="{{asset('uploads/'.$gallery->image)}}" alt="{{$gallery->name}}"/>
                         </a>
                     @endforeach

0 likes
9 replies
Sergiu17's avatar

try this

Image::make(public_path('uploads/' . $gallery->image));
sanjayacloud's avatar

It's work. But when I try to embed it to the image tag it's not shown. It's said "unknown" inside the src tag

Here is my code.


@foreach($galleries as $gallery)
                         @php
                            $img = \Intervention\Image\Facades\Image::make(public_path('uploads/' . $gallery->image));;
 $enc = $img->encode('jpg', 1);

                         @endphp
                         <a href="{{asset('uploads/'.$gallery->image)}}" class="btn-gallery">
                             <img src="{{$enc->encoded}}" alt="{{$gallery->name}}"/>
                         </a>
                     @endforeach

Snapey's avatar

don't resize images in the blade file, it will make the page take much longer to load.

You should do the resize when you store the file

sanjayacloud's avatar

Hi @snapey and @sergiu17

I have tried to upload by resizing the image When I try it, I got the below error.


{message: "Unable to find file ().", exception: "Intervention\Image\Exception\NotReadableException",…}
exception: "Intervention\Image\Exception\NotReadableException"
file: "D:\laragon\www\projects\indeed-it-solutions\2021\sameera-photography-3\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php"
line: 20
message: "Unable to find file ()."
trace: [{,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…},…]

Here is my full source code.

 public function store(Request  $request)
    {
      
        $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));



// check if the upload is success, throw exception or return response you need
        if ($receiver->isUploaded() === false) {
            throw new UploadMissingFileException();
        }

// receive the file
        $save = $receiver->receive();


// check if the upload has finished (in chunk mode it will send smaller files)
        if ($save->isFinished()) {
            $fileName = $this->createFilename($save->getFile());
            // save the file and return any response you need, current example uses `move` function. If you are
            // not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())

            $imageThumbnail = $request['file'];
            $imageThumbnailName = time().'.'.$imageThumbnail->getClientOriginalExtension();

            $destinationPath = public_path('/uploads/gallery-thumbnail');
            $img = Image::make($request['file']);
            $img->resize(null,600, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            })->save($destinationPath.'/'.$imageThumbnailName);


            $result = $this->saveFile($save->getFile(),$fileName);
            $data = new GalleryImage();
            $data->image = $fileName;
            $data->thumbnail = $imageThumbnailName;
            $data->gallery_id=$request->gallery;
            $data->save();
        }
        return response('success');
// we are in chunk mode, lets send the current progress
        /** @var AbstractHandler $handler */
        $handler = $save->handler();
        
    }
    protected function saveFile(UploadedFile $file,$filename)
    {
        $fileName = $filename;
        // Group files by mime type
        $mime = str_replace('/', '-', $file->getMimeType());
        // Group files by the date (week
        $dateFolder = date("Y-m-W");

        // Build the file path
        $filePath = "upload/{$mime}/{$dateFolder}/";
        $finalPath =public_path('uploads/');

        // move the file name
        $file->move($finalPath, $fileName);
        return response()->json( $fileName);
    }

    /**
     * Create unique filename for uploaded file
     * @param UploadedFile $file
     * @return string
     */
    protected function createFilename(UploadedFile $file)
    {
        $extension = $file->getClientOriginalExtension();
        $filename = str_replace(".".$extension, "", $file->getClientOriginalName()); // Filename without extension

        // Add timestamp hash to name of the file
        $filename .= "_" . md5(time()) . "." . $extension;

        return $filename;
    }

Snapey's avatar

where did you copy this from? it's a mess!

What is FileReceiver ?

What does this line do ? $fileName = $filename;

You don't use this $filePath = "upload/{$mime}/{$dateFolder}/";

This line $handler = $save->handler(); is never called as it immediately follows a return statement

I could go on

Follow the code line by line and make sure you understand what each line does

1 like
Snapey's avatar

you should follow the example in the docs

sanjayacloud's avatar

I followed it. The code block I got from the documentation.

Please or to participate in this conversation.