Everything seems to be working as I do not get any error but I get garbage code instead of the image itself. The code is very simple in the controller:
// get image & resize
$img = ImgMgr::make('http://pathToImage.jpg')->resize(200,100);
// send HTTP header and output image data
echo $img->response('jpg', 70);
Then I get : HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Length: 2900 Content-Type: image/jpeg Date: Wed, 21 Jul 2021 16:05:21 GMT ����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 70 ��C #%$""!&+7/&)4)!"0A149;>>>%.DIC;��C ;("(;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;��dd"��
What am I missing?
Here is the solution...
$img = ImgMgr::make('http://pathToImage.jpg')->resize(200,100);
$img->response('jpg', 70);
$type = 'png';
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($img);
<img src="{!! $base64 !!}">
Related
I have this endpoint, which outputs (render in the browser) a PDF file:
$pdf = PDF::loadView('print.property', $data, []); // DOMPDF
$filename = 'test.pdf';
if($show) {
return $pdf->stream( $filename );
}
return $pdf->download($filename);
, where $show means render, but not download the PDF file.
For desktop everything works fine and the PDF file is rendered, but when I set with Chrome Dev Tools, the mobile simulator, the server doesn't response, but just stays in load mode.
I have tried with exit, without return with headers:
header("Content-Type: application/octet-stream");
header("Content-Disposition: inline; filename=\"$filename\"");
With Content-Disposition: attachment, it downloads the correct generated file.
The problem is somewhere in the headers I guess.
* I am using LiteServer.
This are some of the generated from the library response headers:
content-disposition: inline; filename="test.pdf"
content-type: application/pdf
I have tried and with this headers before: die ($pdf->stream( $filename));
header('Content-Length: 101840');
header('Content-Type: application/octet-stream');
or:
header('Content-Type: application/pdf');
or:
header('Content-Disposition: inline; filename="'.$filename.'"');
or:
header('Content-Disposition: attachment; filename="'.$filename.'"');
Nothing works. The closest that I ca get is downloading or render it as a string in the browser in Chrome (mobile).
personally I had problems with also. Now, I use "https://github.com/barryvdh/laravel-snappy" I do not know if on the v6 laravel it works.
I'm working on an API that would return an image. In case any error is encountered in the API, though I can issue a HTTP - 500 response (or similar error codes) with text content or no content at all, I need to be able to return an Image Content with the error represented inside the image itself. In other words, when any error is encountered, I create an image representation of the error text and return the image, with the http status code at as 500 - Internal Server Error. I'm using ASP.net Web API and I'm able to create a HTTP 500 Response with Image content and mime type as "image/jpg".
Http 500 with plain text response:
HTTP/1.1 500 Internal Server Error
Content-Length: 14
Content-Type: text/plain; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 24 Jul 2017 06:43:55 GMT
Error Occured!
Http 500 with Image Content:
HTTP/1.1 500 Internal Server Error
Content-Length: 622485
Content-Type: image/jpg
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 24 Jul 2017 06:44:56 GMT
<<ImageContent>>
Though it is possible, I need to know if this approach is OK as per the best practices of web API or not. Thanks.
Best-practice ("everybody does it") is to return HTML, with a Content-Type: text/html, even if the request was for an image.
You can refer to this site here
Below you can take look at piece of code from shared link:
var result = new HttpResponseMessage(HttpStatusCode.OK);
String filePath = HostingEnvironment.MapPath("~/Images/HT.jpg");
FileStream fileStream = new FileStream(filePath, FileMode.Open);
Image image = Image.FromStream(fileStream);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
result.Content = new ByteArrayContent(memoryStream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return result;
I use Laravel 5.1.
A jQuery ajax call is made like that:
$('#export_selected').click(function(){
var checked = $('.select_rows:checked');
var ids = [];
$.each(checked, function(index, value){
ids.push(value.id);
})
$.ajax({
method : "POST",
url : "{{URL::to('/spot/exportTable')}}",
data : {ids:ids}
});
});
And then the php method is defined that way:
public function exportTable(Request $req) {
$spots = array_flatten($req->all());
$res = Spot::whereIn('id', $spots)->get();
Excel::create('Spots', function($excel) use($res) {
$excel->setTitle('Title goes here');
$excel->setCreator('Creator Goes Here')->setCompany('Company Goes Here');
$excel->sheet('Excel sheet', function($sheet) use($res) {
$sheet->setOrientation('landscape');
$sheet->fromArray($res);
});
})->store('csv', storage_path('/exports', true));
$file = base_path() . '/storage/exports/Spots.csv';
$headers = ['Content-Type: application/csv', 'Content Description: File Transfer', 'Cache-Control: must-revalidate, post-check=0, pre-check=0'];
return response()->download($file, 'Spots.csv' , $headers);
}
Chrome developer console prints the results as raw lines.
The file is successfully exported and created in the disk.
The path to file is correct.
But the download is never started.
Echoing the response gives:
HTTP/1.0 200 OK
0: Content-Type: application/csv
Cache-Control: public
Content-Disposition: attachment; filename="Spots.csv"
Date: Mon, 26 Oct 2015 16:08:26 GMT
Last-Modified: Mon, 26 Oct 2015 16:08:26 GMT
1: Content-Description: File Transfer
2: Cache-Control: must-revalidate, post-check=0, pre-check=0
I put the answer here for everybody having the same issue.
#manix figured it out: I'm trying to download via Ajax, which needs to be done in another way, not the way I wrote my code
I'm trying to connect to Parse.com 's REST-API via NSURLConnection to track AppOpened metadata.
I get 200 OK back from the API and the headers are the same to the cURL headers but my API calls are not being represented in the data browser on Parse.com . Is NSURLConnection doing something silly I don't know of? API response is the same but one request gets represented while the other one isn't.
NSLog output:
<NSHTTPURLResponse: 0x7ff5eb331ca0> { URL: https://api.parse.com/1/events/AppOpened } { status code: 200, headers {
"Access-Control-Allow-Methods" = "*";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 3;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sun, 04 Jan 2015 22:42:54 GMT";
Server = "nginx/1.6.0";
"X-Parse-Platform" = G1;
"X-Runtime" = "0.019842";
} }
cURL output:
HTTP/1.1 200 OK
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Date: Sun, 04 Jan 2015 23:03:51 GMT
Server: nginx/1.6.0
X-Parse-Platform: G1
X-Runtime: 0.012325
Content-Length: 3
Connection: keep-alive
{}
It's the same output. What am I doing wrong? Has anyone experience with this?
Turns out Parse was showing funny API keys the moment I copied them out of the cURL example they provide in their lovely docs. Don't know whose analytics I screwed over but I'm terribly sorry and it wasn't my fault!
Always copy your API keys out of [Your-Parse-App-Name]->Settings->Keys
It probably was just a stupid glitch that happened on the Server.
I want to hide the broken image icon, but the image server that i'm using doesn't throw a 404 page, instead it redirects it to their alternative image, so I can't use the conventional onerror method.
This method works well if the server throws a 404 page in response.
<html>
<body>
<div><img src="http://stackoverflow.com/image.jpg" alt="image" onerror="this.style.display='none'"/></div>
</body>
</html>
but in conditions like, where image URL is: http://tinypic.com/someimage.jpg
the above method won't work. because this is a 302 response, which redirects it to some other image. I want to hide that as well.
Is that possible to somehow use http response as condition to display/hide image.
Here are http headers of the above image:
$ curl -I http://tinypic.com/notfound.jpg
HTTP/1.1 302 Found
Server: Apache
Location: http://tinypic.com/images/404.gif
Expires: Fri, 21 Mar 2014 21:33:07 GMT
Cache-Control: max-age=300
Content-Type: text/html; charset=iso-8859-1
Content-Length: 217
Accept-Ranges: bytes
Date: Fri, 21 Mar 2014 21:28:07 GMT
X-Varnish: 1535787681
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Varnish-Server: den2tpv01
X-Cache: MISS
Now, I think HTTP/1.1 302 Found can be used in someway. any suggestions?
Here you go, the simple solution:
<img src="www.google.com/noanyimage.png" width="200px" height ="200px" onerror="this.src ='';" />
Here is the solution using php...
<?PHP
$url = "http://tinypic.com/notfound.jpg"; //Image link here
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($status_code==302 or $status_code==301 or $status_code==404){
echo "no image found";
}
else {
echo '<img src="'.$url.'" />';
}
curl_close($ch);
?>