Saving image path to database using Laravel - php

I have a function that takes submitted form data which is an image, then validates it and it deletes an old image if one already exists. It currently does that and stores the image on in the storage folder.
I am now trying to save the URL of the image to the database. I have seen examples where they do not save the path to the database but would it be best to do so?
Here is my function
public function postAvatarUpload(Request $request)
{
$this->validate($request, [
'image' => 'required|image|max:3000|mimes:jpeg,jpg,bmp,png',
]);
$user = Auth::user();
$usersname = $user->username;
$file = $request->file('image');
$filename = $usersname . '.jpg';
if (Storage::disk('local')->has($filename)) {
Storage::delete($filename);
}
Storage::disk('local')->put($filename, File::get($file));
$avatarPath = Storage::url($filename);
Auth::user()->update([
'image' => $avatarPath,
]);
return redirect()->route('profile.index',
['username' => Auth::user()->username]);
}

I mostly store images people upload to a subfolder in the public folder from Laravel. The image can then be requested with only your webserver doing the work and not having to serve it via PHP.
For images like an avatar I create a folder structure based on the model name and id of the model. Like: laravel_folder/public/images/user/13/avatar.jpg.
Because the path and url are based on the modelname and id, you can easily generate a direct url and path to it. Downside is that the url and path to avatars of other users is predictable, so don't use it for sensitive images.
I see you accept an image in multiple formats (jpg,png,bmp).
When you save the image, you save it as an jpg image. You should use an image manipulation library like Intervention, or get the extension of the file with pathinfo($file, PATHINFO_EXTENSION), otherwise the file won't be readable because the image extension and the image contents don't match.
Also think about the image size. Users can upload an image in poster size, while you only need a small image. Use an image manipulation library to create a smaller version image of the original image.
If you save the original image in the format you get from the user, then just check all extensions if they exist. One way to do it would be:
$extensions = [ 'jpg', 'jpeg', 'png', 'bmp' ];
$user = Auth::user();
$imagePath = "{public_path()}/images/{class_basename($user)}/{$user->id}/";
$imageName = "avatar";
// go through all extensions and remove image if it exists
foreach($extensions as $ext)
{
$imageFullPath = "{$imagePath}/{$imageName}.{$ext}";
if( file_exists($imageFullPath) )
{
#unlink($imageFullPath);
break;
}
}

The less you store in your filepath, the better because you aren't locked down to any folderstructure.
For now you might store all your uploads in /public/uploads but when your app gets bigger you might want to put everything in /public/uploads/form_xyz_images. When you put your whole image path in the database you'll have to adjust all values in the database.
A better way would be to only save the filename and append the path somewhere else. For example you could use accessors and mutators to get the path: https://laravel.com/docs/master/eloquent-mutators#accessors-and-mutators

Related

Upload and Save File Image as Bytea Data Type in laravel Postgresql

I want to save image file into database as byte data type. I use framework laravel 8.
public function store(Request $request){
$this->validate($request, [
'file' => 'required|mimes:png,jpg,jpeg'
]);
$file = addslashes($request->file('file'));
$image_file = pg_escape_bytea(file_get_contents($file));
$file = new File;
$file['member_id'] = $request->session()->get('id');
$file['file'] = $image_file;
$file->save();
}
The image file can already be stored in the database but when I download the image file from the database if the uploaded file has a .jpeg extension then the result is like this..
result : enter image description here
And if the uploaded file has an extension other than .jpeg then the result will be like this..
result : enter image description here
How to fix this problem? I want the image file to still look good when downloaded.
Please help me...

Laravel File Upload different versions and formats of a file

Let me know a sample. how can I store the uploaded file to the database; laravel
ex.
In the system, if upload a doc file, it will automatically convert to PDF.(via cloud convert API ). how I can save both files in the database.
---------------------------------------------------
|id|proposal_id|version_id|Status|fileName|filetype|
---------------------------------------------------
Better to upload the files in a folder within the public folder and save the link folder in the database
you can use code like this:
if ($request->hasFile('image')) {
$file = array('image' => Input::file('image'));
$destinationPath = 'image/'; // upload path
$extension = Input::file('image')->getClientOriginalExtension();
$logofileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('image')->move($destinationPath, $logofileName);
}

How to download and store and then upload dropbox shared image in laravel?

I am trying to write a script to upload bulk product from excel sheet which contain product name, price and images. Images are the dropbox image share link. How can I download those image from the dropbox url, save them on my server, then upload the image url to my database?
Excel sheet reading:Maatwebsite/Laravel-Excel
Generic upload code:
public function productUpload(Request $request){
if($request->hasFile('products')){
$path= $request->file('products')->getRealPath();
$data = Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
//download the image create thumbain and store to /images/product/thumbnail folder and get the link,
$thumbnail = //here will be the path for the thumbnail
//Original image
$original = ;
$data['original']= $original;
$data['thumbnail']=$thumbnail;
$data['name']=$value->name;
$data['price']= $value->price;
Product::create($data);
}
return redirect()->back()->with('success','Product has been uploaded');
}
}
}
The image url that is in the excel sheet is like this one https://www.dropbox.com/s/x2tbsy49sraywvv/Ruby12.jpg?dl=0 This file has been removed at this point.
You can dowload the image directly by adding dl=1 to the dropbox link Source
And you can dowload the image to the server with the file_get_contents php command. Source and store it in the server with the Storage Facade
$url = "yoururl"; //check if has the dl=0 and change it to 1 or add dl=1
$contents = file_get_contents($url);
$name = 'image.png';//the name of the image to store on disk.
\Storage::disk('yourdisk')->put($name, $contents);
Also you will get the full path to the image like this.
$thumbnail = storage_path('yourdir/'.$name)
Tested it with a file that i have on my dropbox and worked without a problem.
You still need to handle some things like errors and delays while dowloading the images.

Get path of temp uploaded file with image extension in PHP

I have a form which lets the user upload the image file.
<div id="imageDiv">
Image Path : <input class="imageOption" type="file" id= "uploadImageFile" name="uploadImageFile" >
</div>
The problem comes when I try to fetch the path from temp folder. I won't be needing the image files after processing the request. When I try to fetch the path with something like this
$imagePath = $_FILES['uploadImageFile']['tmp_name'];
The path looks like C:\wamp\tmp\phpA123.tmp.
The API I'm using would require a path with extension of an uploaded image like this
C:\wamp\tmp\image.png
Couldn't figure out a way to do so unless I want to copy this image to some other upload folder and use it. I don't want these images logged in a server
Thanks
It would be helpful to know the specific API in use, but no well written file storage API should ever have to rely on the uploaded file name being used to store a file. You should be able to use the temp file contents in the API, and specify the file name separately.
In L5:
// Get the UploadedFile object
$file = Request::file('uploadImageFile');
// You can store this but should validate it to avoid conflicts
$original_name = $file->getClientOriginalName();
// This would be used for the payload
$file_path = $file->getPathName();
// Example S3 API upload
$s3client->putObject([
'Key' => $original_name, // This will overwrite any other files with same name
'SourceFile' => $file_path,
'Bucket' => 'bucket_name'
]);
If you want to get same output as -
$imagePath = $_FILES['uploadImageFile']['tmp_name'];
in Laravel, you can do something like this as described by #cdbconcepts -
$file = Request::file('uploadImageFile');
$imagePath = $file->getPathName()

Symfony2: Amazon S3 retrieve image and mofidy using Imagine

I'm using amazon s3 to store my images. For now I've got 3 different folders - temporary one to not accepted pictures, one for accepted and one for watermarked one.
When photo is being accepted it is copied to accepted and watermarked folders and deleted form temporary one.
The problem is with watermarked folder because I need to add watermark. During work in progress I've mocked method that is adding watermark like this:
protected function addWatermark($product)
{
$filterName = 'watermark';
$this->imagineFilterManager->getFilter($filterName)
->apply($this->imagine->open($product->getFilePath()))
->save($path, array('format' => "png"));
}
and it was pretty fine. But the problem is - mock was working on uploaded file, and FilePath was temporary file path to image.
Now I need to retrieve image from amazon s3, pass it to imagineFilterManager and save back at amazon s3 location. The problem is with retrieving photo.
I tried like this:
read method:
public function read($path)
{
return $this->fileSystem->read($path);
}
protected function addWatermark($product)
{
var_dump('addWatermark');
$filterName = 'watermark';
$path = $this->getShopPicturePath($product);
$file = $this->staticContentAdapter->read($path);
$file = imagecreatefromstring($file);
$this->imagineFilterManager->getFilter($filterName)
->apply($this->imagine->open($file))
->save($path, array('format' => "png"));
var_dump('filtered');exit;
}
but after this $this-imagine-open() returns
File Resource id #141 doesn't exist
The strange thing form me is that it is was possible for me to save image file on s3 using only path:
->save($path, array('format' => "png"));
but it is not possible to open file using only path:
->apply($this->imagine->open($file->getFilePath()))
It throws exceptions
File watermarked/asd_asd.png doesn't exist.

Categories