Laravel resize and upload to storage - laravel-5

I am trying to resize, rename and upload the image using laravel Storage, intervention on laravel5.
Here is my upload code:
if( $request->file('logo_image') ){
$logo_path = $request->file('logo_image')->store('university/'.Auth::User()->id);
if ( $logo_path ){
Storage::delete($university->logo_image);
}
$university->logo_image = $logo_path;
}
$university->update();
It is storing the image on folder like: storage/app/public/university/1/fjkdhjkfdh.jpg
Now I want to rename image name like university-name.jpg and also resize and upload to sam directory.
So I should have two images one is storage/app/public/university/1/university-name.jpg.jpg
and other is storage/app/public/university/1/thumbnail/university-name.jpg
I play around with this for the whole day but no success.
if(Input::file())
{
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('profilepics/' . $filename);
Image::make($image->getRealPath())->resize(200, 200)->save($path);
$user->image = $filename;
$user->save();
}
Please someone can help me?
Thanks in advance.

Related

How to delete image from storage in restfull api

$employee = Employee::find($id);
Storage::delete($employee->photo_file_path . $employee->photo_file_type);
$file = $request->image;
$path = $file->storeAs('public/images', $id . '.' . $file->getClientOriginalExtension());
$employee->photo_file_name = $id;
$employee->photo_file_type = $file->getClientOriginalExtension();
$employee->photo_file_path = $path;
when uploading photos, I want the previous photo to be deleted first and then insert a new photo again, I use Storage::delete. I have checked the public folder but the previous photo was not deleted, instead the photo was added
If you are uploading images in public folder then you need to add public before your path
Storage::delete('public/'.$employee->photo_file_path . $employee->photo_file_type);
if(file_exists(public_path('upload/bio.png'))){unlink(public_path('upload/bio.png'));
}else{dd('File does not exists.');}

Storing image in local drive and saving path in db

I would like to save an image in my local drive, and insert its path to a DB, and then preview the image through the website. I have no idea how to do it and also I have not found anything similar anywhere.
Try this one
$image = $request->file('image');
$filename = time() . $image->getClientOriginalName();
$destination_path = public_path('/image/');
$image->save($destination_path . $filename);
$image->image_path= $destination_path;
Hope this helps :)
You can try to do this:
// To save image on local drive and insert path to db.
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$path = $request->image->store('public/images');
$path = basename($path);
$image = new Images();
$image->photo = $path;
$image->save();
}
I hope it would be helpful.

Incorrect URL from AWS S3 with Laravel File Upload

I'm uploading an image to S3 with Laravel as follows:
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url($imageFileName);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}
The image uploads successfully but I need to store the URL in my database. This is being called here:
$image = Storage::cloud()->url($imageFileName);
The issue is the URL being returned, it looks like this:
http://test-env.XXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
Hence:
http://mentr-test-env.2w8sh3esch.us-west-2.elasticbeanstalk.com/profile/
is somewhat-correct. But the next piece is missing the 'profile' sub-folder, and obviously starts at HTTPS again:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
It would appear I'm getting two halves of the link in a single string. I don't edit the $image variable anywhere else.
The correct link is:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/profile/6_52644340.jpg
I have confirmed the files are uploading correctly and publicly available.
I have tried calling:
$image = Storage::cloud()->url($filePath);
And this returns:
http://test-env.XXXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-XXX123XXX.s3.us-west-2.amazonaws.com//profile/6_31595766.jpg
Update
I just noticed the first part of the returned URL is the BeanStalk instance URL with the /profile/ added. This is even stranger as I don't wish to use Beanstalk, I only want to use S3.
If you want to store the entire url you can get it from the return variable passed back from the put() function.
$s3_url = $s3->put($filePath, file_get_contents($image), 'public');
Sometimes I like to just store the path though and save just that piece to the database then I can pass the path to Storage::url($filePath); and it still works.
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url('s3-url/s3-bucket'.'/'.$filepath);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}

Image resize error using intervention in laravel 5

I am using intervention image to resize image and then save it to a destination folder. But its not working for me. I am getting error like, "Image source not readable".
Pls see the below code:
$image_name = $file->getClientOriginalName();
$thumbName = 'thumb_'. $image_name;
$destinationPath = public_path() . '/uploads/';
$thumbdestinationPath = public_path() . '/uploads/thumbnails/';
$imgUrl = URL::to('/').'/public/uploads/'.$image_name;
$thumbUrl = URL::to('/').'/public/uploads/thumbnails/'.$image_name;
$upload_success = $file->move( $destinationPath, $image_name);
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath );
}
You need to give the name of image:
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath . $image_name );
}

Laravel 5 image resize

I use Laravel 5 and have a form to upload a image. When saving the file I have in my controller methods to get the image and put it in a directory:
if ($request->hasFile('picture')) {
$destinationPath = 'uploads';
$filename = $image->getClientOriginalName();
$extension = $image->getClientOriginalExtension(); // add
$picture = sha1($filename . time()) . '.' . $extension; //add
$offer->image = $picture;
$image->move($destinationPath, $picture);
}
$offer->save();
Before I save the file I would like to resize the file with a max. width of 800px. Is there a image resize or compression function in Laravel available?
What would be the best idea to do this?
You can install and use package, similar to intervention.
Example from official website:
$img = Image::make('public/foo.jpg');
$img->resize(320, 240);
$img->insert('public/watermark.png');
$img->save('public/bar.jpg');
Image::make($request->file('image'))->resize(462, 462)->save('upload_path/filename.jpg'));
try this code

Resources