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

mohamedhamidi's avatar

Fails to rename image before upload using storeAs

I created a form to store article with an image , and generate a resized version as thumbnail from it.

I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error

This is the article upload handler function in my controller :

    private function handleRequest($request)
   {
  	$data = $request->all();
  	if ($request->hasFile('image')) {
     	$image = $request->file('image');
      	$fileName = $request->slug;

      		
      $successUploaded = $image->storeAs('img/articles-images', $fileName);

      if($successUploaded) {
        $width = config('cms.image.thumbnail.width');
        $height = config('cms.image.thumbnail.height');

   $extension = $image->getClientOriginalExtension();
   $thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);

        Image::make('img/articles-images' . '/' . $fileName)
         	->resize($width, $height)
         	->save('img/articles-images' . '/' . $thumbnail);
   }

      $data['image'] = $fileName;
  }

  return $data;
}
0 likes
1 reply
mohamedhamidi's avatar
mohamedhamidi
OP
Best Answer
Level 2

I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :

  if ($request->hasFile('image')) {
      $image = $request->file('image');
      $fileName = $request->slug.'.' .$image->getClientOriginalExtension();

      $destination = $this->uploadPath;

      $successUploaded = $image->move($destination, $fileName);
1 like

Please or to participate in this conversation.