The image cannot be displayed because it contains errors laravel - laravel

I am working on a laravel project in which I am trying to show image using url but getting error. I have tried a lot and search everything but I don't understand why I am getting this error. I am using the below function
public function displayImage($filename){
$path = storage_path("app/taskImage/".$filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = \Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
And the route is
Route::get('taskImg/{filename?}', [
'uses' => 'FormController#displayImage',
]);
And the URL which I am tring is like
http://localhost/project_name/public/taskImg/test.jpg
when I have print something I am getting the that but Not getting the Image. It is showing a blank screen with error message The image cannot be displayed because it contains errors

you can do it like this way
$storagePath = storage_path("app/taskImage/".$filename);
return Image::make($storagePath)->response();

I've spent a LOT of hours searching for a solution for this, I found it at last! just add ob_end_clean before returning the response, like the following:
ob_end_clean();
return response()->file($path,['Content-type' => 'image/jpeg']);
I can't explain because I don't know what was going on, any one could explain this?

Related

How to add validate data search

I am using laravel for this project, i'm newbie at laravel then i want to add validate data if there is true then go to pdf blade, unless the data is false or wrong (i don't know what is it call so i named it True and False, but i hope you understand what i mean)
there is code in controller method search pdf
$id = $request->id;
$date = $request->date;
$pegawai = DB::table('pegawais')
->where('id', $id)
->whereDate('date', $date)
->get();
$pdf = PDF::loadview('pegawai_pdf', [
'pegawai'=>$pegawai
]);
return $pdf->stream();
and this is the output blade when i searched the data is true or exist
here
and this is the output blade when i searched but the data is false or not found data exist
here
fyi the data are fake data from seeder,
From your example, I will assume that you want to check if there is a result or not in $pegawai right? Then just count it.
if (count($pegawai) > 0) {
// show your pdf output here
} else {
// there is no data, do something here
}
to check this does not need to be a true false variable you can check this like this
if($pegawai){
$pdf = PDF::loadview('pegawai_pdf', [
'pegawai'=>$pegawai
]);
return $pdf->stream();
}else{
//show error here
}

Importing generated csv from Laravel into Google Sheets

I'm trying to import an automatically generated csv into Google Sheets using this as cell value:
=importData("https://example.com/reports/some-report")
When I try importData on a static file on the server everything works as expected (like =importData("https://example.com/storage/some-static-report.csv") )
..but when I generate the file on the fly () I get a #N/A in the cell; "Resource at url not found." error.
This is the Laravel code to generate the csv:
public function downloadReportsAsCSV(Request $request) {
$list = [];
$list[] = ['timestamp', 'id', 'value'];
// ... fill the list with data here
$callback = function() use ($list) {
$out = fopen('php://output', 'w+');
// Write CSV lines
foreach ($list as $line) {
fputcsv($out, $line);
}
fclose($out);
};
$name = 'somereport.csv';
$headers = [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename='. $name,
];
return response()->stream($callback, 200, $headers);
}
The route to this method is public so authentication is not a problem in this case.
When I call https://example.com/reports/some-report in a browser it downloads the file (as somereport.csv), but somehow Google Sheeds can't handle it the way I expect it to.
Any ideas on how to make this work?
It seems to be working after all, it's just that Google Sheets apparently needed quit some time before updating the field (at least a couple of minutes).
If anyone has any idea how to trigger Google Sheets to update the data immediately I'd sure like to know.

Symfony 406 (Not Acceptable)

I am trying to set image path with controller, because i choose to add my image uploaded out of web/ directory.
I create an ServeController :
public function urlAction($filename)
{
$path = "../data/uploads/admin/";
$response = new BinaryFileResponse($path.$filename);
$response->headers->set('Content-Type', 'image/jpeg');
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE);
return $response;
}
This work from my frontend reactJs, but with i am trying to display the image on my symfony backoffice i have got error 406 Not Acceptable
In my twig template i am using {{url('url_image', {'filename' : data.filename }) }}
Any ideas ?
Can you try this:
public function urlAction($filename)
{
$path = "../data/uploads/admin/";
$response = new Response();
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'image/jpeg');
$response->setContent(file_get_contents($path));
return $response;
}
Not sure if that will fix it though. Please try it.

guzzle 5.0 postAsync error in laravel 5.0

Hi I am trying to use Guzzle in my laravel application to send some data to a remote application. When i try to use $client->post('url') it works fine but whenever i use any of async functions like postAsync, sendAsync i get the BadMethodException.
local.ERROR: exception 'BadMethodCallException' with message 'Unknown method, sendAsync' in /home/lycan/Documents/workspace/my_project/vendor/guzzlehttp/guzzle/src/functions.php:324
My method is below
private function sendDataToRemoteApplication()
{
$client = new \GuzzleHttp\Client();
$url='http://localhost:9000/submitResults/?key1=valu1&key2=22';
$request = new Request('POST', $url);
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
}
i followed everything from the documents so it should work !
Tried to find out the solution on google but didn't find a link where any one faced this issue....( i would consider my self lucky if i am the first to face this issue though :D ) . Can one please suggest what is the problem.
You may try this (\GuzzleHttp\Psr7\Request):
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://example.com');
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();

Multiple file upload and validation

I want to allow multiple files upload, so I did this in my view:
{{ Form::file('files[]', array('multiple'=>true)); }}
it works, but I can't validate it. For testing purposes I've created this rule:
'files' => 'required|mimes:png,jpg,jpeg'
but it doesn't work, it always says the mime type is incorrect, I also tried it with png.
Can anyone help me please? I also tried to remove the [] from the file input name.
Could be the problem that laravel doesn't support multiple files at validation?
Thanks
I had the same problem, i got the solution by editing the code of the request handler.
public function rules()
{
$rules = [];
$nbr = count($this->file('field_name')) - 1;
foreach(range(0, $nbr) as $index) {
$rules['field_name.' . $index] = 'required|mimes:jpeg,jpg,png';
}
return $rules;
}

Resources