I was successfully able to get an image(which is stored as a BLOB file) from an API endpoint in guzzle, but I am having issues displaying this BLOB file as an image
I have tried several PHP functions for manipulating and converting a BLOB file to an image but all has been abortive.
after sending the request to my guzzle trait, I decided to var_dump the result to be sure that it was successful
$userid = $_COOKIE['id'];
$url = 'users/image/'.$userid;
$requestResult = $this->sendGetWithHeader('users/image/'.$userid);
$result = $requestResult->getBody()->read(1024);
//$res = file_get_contents($result);
// fopen($result, 0);
var_dump($result);
echo "<hr>";
var_dump($requestResult);
// $code = $requestResult->getStatusCode();
// var_dump($code);
My expected result is an image file but the actual is still the raw BLOB file gotten from my Guzzle request
string(1024) "����JFIF��� ��V�"��6 ���UG_ks1��r�[�% ��JMd�CL,��8�Q�B�yoA��g�p�-�a�f� D �)8#�.)ɠ� �yea#4��ԃ(��t��Ew��մPV'J��R��u������Ju��U ��ԥ��zѩaė��ya$�2�C�Sa�R$�4� ���� 2t��$��L�D���R�$�ґ-&RVdm��IDq)Jc�"'9�;/ �sb{��Z�r�̮��Q_MR=5�Gt���ddA�6�Fa�L���JGi�kh�d�FA�%(�$� �����.�� H�)��-BL������h|#���WW)r�"V[V2˚:��j��˩�>��-$��I�Y�#%dRL(K�(�-�m�I�t��R�JV#�I&JR��0����O2��d�-e#5�2}�2�D'��,Bע��{��!�k�Uw�2ʶ�]cAD�WPw���$��!�Ғu�X!��(�D%I0i�����Lye XI9) �[Nc����#!�miY,Rkiq(���$����mƬ��y��a����崔͒�t7"����͌ΊJgk Q��Ah�Iq$���ʍA�FmJ���7��AJ�a/�rB�"02A����#�d��J%D� �P�yH���m\ż>�i$��I�(�3jm�542k,��t#��#��#D�F�TJDP" �Z(��7�IZ���h9)$��$�Ԣ�Y� m�D�I�4�����Da�%DX�o ���fhh�.�c��aQhcΉ�٠��)��"
Save your blob response to a file as follows
$stream = \GuzzleHttp\Psr7\Utils::streamFor($requestResult->getBody());
file_put_contents('path/to/file.extension',$stream->getContents())
See Convert blog image to file
Look at this example - https://github.com/andriichuk/php-curl-cookbook#download-file
Use 'sink' option to save file from request:
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
$imageFilePath = __DIR__ . '/resource/image.jpeg';
$imageFileResource = fopen($imageFilePath, 'w+');
$httpClient = new Client();
$response = $httpClient->get(
'https://httpbin.org/image/jpeg',
[
RequestOptions::SINK => $imageFileResource,
]
);
if ($response->getStatusCode() === 200) {
echo 'The image has been successfully downloaded: ' . $imageFilePath;
}
Related
i have saved images in 'public/images' folder in Laravel and the link/name is saved in mysql database. all i want to get all images via api call. the problem is i can get a single image easily but couldnt get all the images.[[[enter image description here](https://i.stack.imgur.com/9gerk.jpg)](https://i.stack.imgur.com/MIBIk.jpg)](https://i.stack.imgur.com/QcPQm.jpg)
i have no issue with single image however unable to get all at once. thanks
Try this instead
Store the only image name and extension into database and then retrieve it by using DB and use asset to get image, refer the following code as practice.
$custom_data = array();
for ($i = 0; i <= $total_images; $i++) {
$image_name = $data[$i]->image;
if ($image_name != "") {
$image_with_path = asset('public/products/') . '/' . $image_name;
} else {
$image_with_path = "";
}
$custom_data['image'] = $image_with_path;
}
I create api to upload image, it work on postman, but response not found on Android using Multipart data.
this is my snippet
if ($request->hasFile('attachment')) {
$file = $request->file('attachment');
$filename = $file->getClientOriginalName();
$attachmentName = 'bookingfee_' . $bookingData->salesforce_id . '_' . md5($filename) . '.' . $file->getClientOriginalExtension();
$file->move('attachment/', $attachmentName);
$bf = BookingFee::find($bookingfee_id);
$bf->attachment = $attachmentName;
$bf->save();
} else {
$this->result['message'] = 'File not found';
return $this->result;
}
have any clue for this? should i add something to my controller?
on my partner code, always return File not Found.
i use POST Method
you should remove header (content-type: multipart/form-data) if you using it.
I faced a problem like this once and i found this link
I'm using Laravel Excel 2.1.0 in a local project to write a row into an excel file.
This is my code:
$filePath = storage_path('myfile.xls');
$rows = \Excel::load($filePath, function($reader) {
$sheet = $reader->sheet(0);
$sheet->appendRow(
array(
'Hello'
)
);
});
Everything works and a new line was appended to my file.
Sometimes can happens the excel file is opened while user try to append a new line. In this case Laravel, rightly, show me this error:
fopen(mypath\myfile.xls): failed to open stream: Resource temporarily unavailable
How can I handle this error in order to skip the function and go on with my code without append the row?
I solved in this way:
$filePath = storage_path('myfile.xls');
$fp = #fopen($filePath, "r+");
if($fp) {
$rows = \Excel::load($filePath, function($reader) {
$sheet = $reader->sheet(0);
$sheet->appendRow(
array(
'Hello'
)
);
});
}
I am performing SSH in Laravel whereby I connect to another server and download a file. I am using Laravel Collective https://laravelcollective.com/docs/5.4/ssh
So, the suggested way to do this is something like this
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
if($result) {
return $path;
} else {
return 401;
}
Now that successfully downloads the file and moves it to my local server. However, I am always returned 401 because $result seems to be Null.
I cant find much or getting the result back from the SSH. I have also tried
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path, function($line){
dd( $line.PHP_EOL);
});
But that never gets into the inner function.
Is there any way I can get the result back from the SSH? I just want to handle it properly if there is an error.
Thanks
Rather than rely on $result to give you true / false / error, you can check if the file was downloaded successfully in another way:
// download the file
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
// see if downloaded file exists
if ( file_exists($path) ) {
return $path;
} else {
return 401;
}
u need to pass file name also like this in get and put method:
$fileName = "example.txt";
$get = \SSH::into('scripts')->get('/remote/somelocation/'.$fileName, base_path($fileName));
in set method
$set = \SSH::into('scripts')->set(base_path($fileName),'/remote/location/'.$fileName);
in list
$command = SSH::into('scripts')->run(['ls -lsa'],function($output) {
dd($output);
});
I am trying to upload multiple files but I only get 1 file in return.Below is my code:
public function uploadQuoteItemImage(){
$file=Input::file('filename');
$file_count=count($file);
dd($file_count);
$uploadcount=0;
foreach($file as $f){
$random_name=str_random(8);
$destinationPath='images/';
$extension=$file->getClientOriginalExtension();
$filename=$random_name.'_quote_itm_image.'.$extension;
$byte=File::size($file); //get size of file
$uploadSuccess=Input::file('filename')->move($destinationPath,$filename);
$uploadcount ++;
}
if ($uploadcount == $file_count){
QuoteItemImage::create(array(
'quote_item_id'=>Input::get('quote_item_id'),
'filename'=>$filename,
'filesize'=>$byte
));
return Common::getJsonResponse(true, 'image created', 200);
}
}
Even though I sent 3 files its returning only 1 file. Please help.
so in the form-data of postman you are giving the key attribute as filename for files
in turn it should be filename[] since you are sending array of data
once you set it it works fine .
now you can check in the php code like below
$files = Input::file('filename');
foreach ($files as $one) {
$filename = $one->getClientOriginalName();
$listfilenames[] = $filename;
}
echo $listfilenames