use public path helper
How to get correct path to an uploaded image file to pass to a PHP function?
I'm trying to access an image file (uploaded with AJAX) to a Laravel 5.4 backend, and having trouble getting native PHP functions to access it. File is uploading fine (appears in the correct upload folder) but Laravel error reports that PHP's getimagesize() can't find the file) Actual error is:
(1/1) ErrorException
getimagesize(storage/public/images/viE9JvY8jfA4fVuZL7vkh3RdZY0zyT4svH4Xldv1.jpeg): failed to open stream: No such file or directory
Partial code is below:
public function imgupload(Request $request)
{
// Store file from 'imgfile' field from uploaded form (imguploadform in blade template) in /storage/app/public/images/
$path = $request->file('imgfile')->store('public/images');
// Generate a thumbnail from the image using PHP's inbuilt GD library and store in thumbs dir:;
$thumbpath = $this->makethumbs($path);
return response()->json(['thumb' => asset($thumbpath)]); // Return path
}
// Reads full size uploaded image from storage, generates a smaller thumbnail and saves it to the thumbs/ dir
public function makethumbs($img, $maxwidth = 120, $maxheight = 120)
{
$img = 'storage/app/'.$img;
$arr_image_details = getimagesize($img);
/* Other stuff to generate thumbnail here */
// Write thumbnail image out to thumbs/ folder
return($thumbnail_path);
}
This is on my local Windows 7 laptop (tho will need to work online later, probably Heroku). I'm running on Bitnami's WAMP stack, and root Laravel folder is apache2/htdocs/seg . In there, public/ contains a folder link called 'storage' which maps to storage/app/ and that folder contains public/ then in there folders images/ and thumbs/ . So intention is to read uploaded image out of images/ then resize and put into /thumbs.
I did a few Log:info()'s and found $path (in my imgupload func) is: public/images/viE9JvY8jfA4fVuZL7vkh3RdZY0zyT4svH4Xldv1.jpeg
and in makethumbs() PHP's current working dir (via getcwd()) is: C:\Bitnami\wampstack-7.1.2-0\apache2\htdocs\seg\public
So if getimagesize() is starting from PHP's working dir, in theory it should be looking in: C:\Bitnami\wampstack-7.1.2-0\apache2\htdocs\seg\public/storage/app/public/images/[filename]
Any ideas welcome!
Please or to participate in this conversation.