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

chrisgrim's avatar

Image Intervention... "Can't write image data to path ({$path})"

Hi, I am trying to resize my images I upload using Image Intervention but I am having the hardest time getting intervention to save. Laravel has no issue creating a folder and saving it, but then when I use Image it gives me the cant write image data to path error, even when I am using the same folder. I right clicked on the folder and gave permissions to everyone but that doesnt seem to help. Here is the code I am using in my controller.

//This all works $title = str_slug(request('title')); 
$filenameWithExt = $request->file('cover_image')->getClientOriginalName(); 
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME); 
$extension = $request->file('cover_image')->getClientOriginalExtension(); 
$fileNameToStore= $title.'.'.$extension; 
$thumbnailpic= 'thumb'.'-'.$fileNameToStore;

//This store image creates the folder and saves the file 
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);

//Here is where I am trying to resize with image and it breaks

    $file = Input::file('cover_image');
    Image::make( $file->getRealPath() )->fit(340, 340)->save('public/cover_images/' . $thumbnailpic);

I have tried a ton of different methods for image, but none of them let image save to the folder. I am using the latest laravel 5.6 with valet for testing

0 likes
15 replies
Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

Snapey's avatar

You need to provide the full server path to where you want the image

chrisgrim's avatar

ok I updated. Here is my latest image code that still gives me the save error.

Image::make( storage_path().'/app/public/cover_images/'.$fileNameToStore)->fit(340, 340)->save('public/cover_images/' . $thumbnailpic);
rin4ik's avatar

try this. inside make should be the path of the existing file

Image::make( 'public/cover_images/'.$fileNameToStore)->fit(340, 340)->save();
chrisgrim's avatar

Oh! That seemed to fix that error! I am now getting

"Image source not readable"

I'm not sure why though because it is creating the non resized image in the folder.

rin4ik's avatar

it is not creating. it fails here Image::make( 'public/cover_images/'.$fileNameToStore)->fit(340, 340)->save(); read here

chrisgrim's avatar

Hi rin4ik, yeah it fails on that line, but at least its a new error. I have been stuck on the cant write image data to path. I tried looking into that link but I didnt really understand. As far as I can see, no one solved the problem for him.

rin4ik's avatar

@chrisgrim mark this discussion as solved because your main problem solved. and try to read more topics on new error, asked here few times u can search in stackOverflow as well. if u unable to solve by yourself we will help you , include all your code and create new discussion :)

Snapey's avatar

as I said, you need to give the full path to the place you want to save the image.

save() overwrites it where it is. Is this what you want?

Image::make( storage_path().'/app/public/cover_images/'.$fileNameToStore)->fit(340, 340)->save('public/cover_images/' . $thumbnailpic);

save('public/cover_images/' . $thumbnailpic) is not valid because you did not specify the full path.

use this instead

Image::make( storage_path().'/app/public/cover_images/'.$fileNameToStore)->fit(340, 340)->save(public_path('cover_images/' . $thumbnailpic));

otherwise you are just overwriting the original

chrisgrim's avatar

Hi Snapey,

Even if I use that new code, it still gives me the error: "Can't write image data to path

Snapey's avatar
Snapey
Best Answer
Level 122

easiest if you first breakdown the paths and check them and then use them in the Image library

So, assuming it is failing on the line you say

$source = storage_path().'/app/public/cover_images/'.$fileNameToStore;
$target = public_path('cover_images/' . $thumbnailpic);

dd($source, $target);

Image::make($source)->fit(340, 340)->save($target);

With the dd in there, you can check that these are both full filesystem paths to the source of the file and the target where it will be saved in its new size

Then you can just comment out the dd() and know what exactly what you are asking Intervention to do.

chrisgrim's avatar

You just made my week! Using the Die and dump I was able to see where the files were coming from. The problem was I was trying to save it to the public_path which I guess image intervention can't write too. So instead I saved it to the storage_path and it worked!!!

Thanks so much

Snapey's avatar

Great. Please mark it solved if you are happy we got to the bottom of it.

1 like
GastonChiquillo's avatar

Hello, I am from Peru, so my English is not good. So I write in English and Español.

-------------------- English: --------------------

After so many hours, I found the solution for Centos 8 (but it works for others too). We need to assign the necessary permissions to the folder that will save the images to upload. And using the "chmod" command is not enough, you need to use

httpd_sys_content_t

and

httpd_sys_rw_content_t

Ex: From the command line we enter the folder of our Laravel project, we enter the "/public" folder and execute the following commands to give permissions to the "/images" folder:

chown –R apache: images
chmod –R 755 images
systemctl restart httpd
chcon -R -t httpd_sys_content_t images
chcon -R -t httpd_sys_rw_content_t images
systemctl restart httpd

Note: ("chown -R apache: images" replaces in other Linux versions its equivalent "chown -R www-data: images").

-------------------- Español: --------------------

Hola, soy de Perú, así que mi inglés no es bueno. Asi que voy a escribir en Inglés y Español Después de tantas horas, encontré la solución para Centos 8 (pero también funciona para otros). Necesitamos asignar los permisos necesarios a la carpeta que guardará las imágenes a subir. Y usar el comando "chmod" no es suficiente, es necesario utilizar

httpd_sys_content_t

y

httpd_sys_rw_content_t

Explicación: Desde la línea de comandos entramos a la carpeta de nuestro proyecto Laravel, entramos a la carpeta "/public" y ejecutamos los siguientes comandos para darle permisos a la carpeta "/images":

chown –R apache: images
chmod –R 755 images
systemctl restart httpd
chcon -R -t httpd_sys_content_t images
chcon -R -t httpd_sys_rw_content_t images
systemctl restart httpd

Nota: ("chown -R apache: images" reemplaza en otras versiones de Linux a su equivalente "chown -R www-data: images")

magnificcoding's avatar

To solve this issue, it's good to know what prompted it. Was the code working well and all of a sudden thinks changed? In my own opinion, and which I think is the best answer to this problem is all about the permission to the storage folder. If you are using Laravel, create symbolic link of storage folder in public folder using php artisan storage:link. If you have access to your hosting server, you can always ensure that there's no folder with the same name as storage in public folder before executing the above command by examining folders in public folder. Thereafter ensure you give permission to your storage folder using sudo chmod -R 775 /var/www/yourprojectfolder/storage sudo chmod -R 775 /var/www/yourprojectfolder/bootstrap/cache This is the perfect answer to that problem. If you find this information useful to you don't hesitate to visit magnificcoding.com and learn more.

Please or to participate in this conversation.