Laravel 4 get image from url - image

OK so when I want to upload an image. I usually do something like:
$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
if( $uploadSuccess ) {
// save the url
}
This works fine when the user uploads the image. But how do I save an image from an URL???
If I try something like:
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
and then:
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
I get the following error:
Call to a member function move() on a non-object
So, how do I upload an image from a URL with laravel 4??
Amy help greatly appreciated.

I don't know if this will help you a lot but you might want to look at the Intervention Library. It's originally intended to be used as an image manipulation library but it provides saving image from url:
$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');

$url = "http://example.com/123.jpg";
$url_arr = explode ('/', $url);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$destinationPath = public_path().'/img/'.$name;
file_put_contents($destinationPath, file_get_contents($url));
this will save the image to your /public/img, filename will be the original file name which is 123.jpg for the above case.
the get image name referred from here

Laravel's Input::file method is only used when you upload files by POST request I think. The error you get is because file_get_contents doesn't return you laravel's class. And you don't have to use move() method or it's analog, because the file you get from url isn't uploaded to your tmp folder.
Instead, I think you should use PHP upload an image file through url what is described here.
Like:
// Your file
$file = 'http://....';
// Open the file to get existing content
$data = file_get_contents($file);
// New file
$new = '/var/www/uploads/';
// Write the contents back to a new file
file_put_contents($new, $data);
I can't check it right now but it seems like not a bad solution. Just get data from url and then save it whereever you want

Related

How to upload a file using laravel that will use the exact file name on my public folder and at my database

I have been searching a few codes but didnt manage to work.
The one I found is using jscon encode.
Is there any way to upload a file without changing its name into storage and database?
$fileModal = new Image();
$fileModal->name = json_encode($imgData);
$fileModal->image_path = json_encode($imgData);
$fileModal->save();
I didnt understand the json part. Was hoping to use a built in laravel.
I used the one in the documentation but didnt help.
$path = $request->file('namefile')->storeAs(
'DocumenSokongan', $request->user()->id
);
$image = $request->file('filename');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$image->move($path, $imageName);

Generate multiple PDF documents with loop with dompdf in laravel

I am trying to download multiple pdf using dompdf in laravel, but below solution just concatenate into one pdf file
$weeks = ['test','test2'];
$html = '';
foreach($weeks as $hours)
{
$view = view('contract');
$html .= $view->render();
}
$pdf = PDF::loadHTML($html);
$sheet = $pdf->setPaper('a4', 'landscape');
return $sheet->download('download.pdf');
the server can only return a response, if you want several pdf files you should modify your method so that it accepts some parameter and for each parameter generate a pdf
It is not possible to download more than one file in a single HTTP request.
What you could do is pack the files into o zip and download that. You can try https://github.com/zanysoft/laravel-zip or any other available solution.
Another option would be to save the files and return only paths for the saved files back to the client. Then make a new request and download the file for each of the returned filepaths.
Also if you want to save to separate PDF files you need to move PDF creation into your foreach loop (create a PDF for each parameter).
$weeks = ['test','test2'];
$files = [];
foreach($weeks as $hours)
{
$view = view('contract');
$html = $view->render();
$pdf = PDF::loadHTML($html);
$pdf->setPaper('a4', 'landscape');
$pdf->save('documents/'.$hours.'.pdf');
$files[] = 'documents/'.$hours.'.pdf';
}
return response()->json(['files' => $files]);
In this case the foreach doesn't really do anything other than produce two identical PDF files. If you want to actually apply some kind of changes to your view based on values in $weeks you need to pass that to your view.
$view = view('contract', ['data' => $hours]);
That makes $data available in your view and you can change the resulting PDF in that way (show your contract.blade.php if you need more help regarding that).

Laravel image intervention unable to init from given url

Good evening, i hosted a Laravel project and found some bug.
First of all, here is my code in my controller:
$request->file('FotoHT')->storeAs('FotoHT', $filenameToStore); //this is to safe
$path = asset('storage/app/FotoHT/'. $filenameToStore);
$width = Image::make($path)->width();
$height = Image::make($path)->height();
I save a picture, and it works but when i try using
Image::make($path)->width();
it give me the wrong url. It give me
https://myweb/public/storage/app/FotoHT/_MG_9549_1578913993.jpg
while the image should be accessed from
https://myweb/storage/app/FotoHT/_MG_9549_1578913993.jpg
Can anyone give me a help/solution?
You need to save the image encode to the public path
$fieldFile = $request->file('FotoHT');
$image = Image::make($fieldFile)->width();
Storage::disk('public')->put("FotoHT/".$filenameToStore, (string) $image->encode());
$path=$request->file('FotoHT')->storeAs('FotoHT',$filenameToStore);
$width = Image::make($path)->width();
$height = Image::make($path)->height();
Try this command.You can use asset() helper method for img tag in view

How can I get the full URL of file uploaded to s3 with Laravel?

I have this to upload pictures to one bucket on s3 in AWS
$image = $picture;
$ext = explode(";", explode("/",explode(",", $image)[0])[1])[0];
$image = str_replace('data:image/'.$ext.';base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imageName = str_random(10) . '.' . $ext;
$fullImagePath = 'datasheets/' . $imageName;
Storage::disk('s3')->put($fullImagePath, base64_decode($image));
$DataSheetPicture = new DataSheetPicture();
$DataSheetPicture->data_sheet_id = $DataSheet->id;
$DataSheetPicture->picture = Storage::disk('s3')->url($fullImagePath);
$DataSheetPicture->save();
The above code works fine, it uploads the pictures successfully to the bucket, but on this line
$DataSheetPicture->picture = Storage::disk('s3')->url($fullImagePath);
It saves the URL in the database like these
/datasheets/6GcfzgUPrA.jpeg
/datasheets/AuqHmu8p0W.jpeg
But I need get the URL like this
https://s3.REGION.amazonaws.com/BUCKET-NAME/FULL-IMAGE-PATH
I don't want to concatenate the region or the bucket name because it could be dynamic
The following will give you the proper URL:
return Storage::disk('s3')->url($filename);
Since Laravel 5.2 you're also able to use cloud()
return Storage::cloud()->url($filename);
I don't want to concatenate the region or the bucket name because it could be dynamic
Then you must be modifying your config and not doing this manually to work correctly, for example:
config([
'filesystem.disks.s3.bucket' => 'my_bucket',
'filesystem.disks.s3.region' => 'my_region'
]);
If you remove the AWS_URL setting from your .env file, the Storage::disk('s3')->url($fullImagePath) should give you the proper URL that you need
See discussion also here: https://laracasts.com/discuss/channels/laravel/storage-url-from-s3?page=1&replyId=482913

How to display file which had already upload laravel 5.2?

I upload file in to my database and moved that file as same name in documents folder in a root path like below :)
public function store(PslCall $call,Request $request)
{
$this->validate($request, $this->rules);
$uploadDestinationPath = base_path() . '/documents/';
$current_id = Document::where('call_id',$call->id)->count()+1;
if ($request->hasFile('file'))
{
$file =$request->file;
$fileName = $file->getClientOriginalName();
$file->move($uploadDestinationPath, $call->id.'_'.$current_id.'_'.$fileName);
}
$input = $request->all();
$input['file'] = $call->id.'_'.$current_id.'_'.$fileName;
$input['call_id'] = $call->id;
Auth::user()->documents()->create($input);
return Redirect::route('calls.documents.index',$call->id)->with('message','You have successfully submitted');
}
its works perfect now im display in my index page all the files like below :)
<td>{{strtoupper($document->title)}}</td>
<td>{{$document->file}}</td>
now my route file i have route to display my file like this :
Route::get('documents/{file}',function() {
return 'hi';
});
here im getting hi output when i click <td>{{$document->file}}</td> this path
but i want know how to display file which i upload same file name ?
As documentation says if you want to display the file content then you may use inside your route function something like:
return response()->file('pathToFile');
If download the file is what you want, then try to use instead:
return response()->download('pathToFile');

Resources