PHP Laravel how to create an image from SoareCostin\FileVault stream - php

I'm encrypting my images and uploaded it to S3 buckets.
I'm using of SoareCostin\FileVault library to encrypt/decrypt it
https://github.com/soarecostin/file-vault
I wanted to get the image or at least base64 of the image
FileVault::streamDecrypt('image.png')
however I'm having problems creating an image from it.
This works perfectly:
return response()->stream(function() use ($s3path) {
FileVault::disk('s3')->streamDecrypt($s3path);
}, 200, ["Content-Type" => 'image/png']);
however I wanted to get the base64 or the image with this code:
$stream = FileVault::disk('s3')->streamDecrypt($s3path);
$data = file_get_contents($s3path);
return 'data:image/png;base64,' . base64_encode($data));
I hope someone could help me out. Thanks in advance

Related

Laravel decode base64 image and hashName and store public folder

How I can store decoded image to laravel public image path and hash image name ?
I tried like this, but didnt work and doesn't get any error message;
if (!empty($request->image)) {
$file = base64_decode($request->image)->hashName();
base64_decode($request->image)->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
This code works fine but I use API and via curl sending image then needed base64 decoding:
if ($request->hasFile('image')) {
$file = $request->file('image')->hashName();
$request->image->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
What you suggest save image and send image url to api and downloads image too path or send base64 image encode and in api decode, like I'm trying at the moment?
You can't call functions on the output of base64_decode, as it returns as string.
I would suggest you post the name of the file along with the base 64 encoded content, which can then be saved using laravel's storage helpers:
Storage::disk('local')->put('image.png', base64_decode($request->image))

Intervention Image + Laravel's File Storage: Store resized/decoded base64 image (Intervention Image encode() doesn't work)

I am using Croppie jQuery plugin which returns the cropped image encoded in base64.
After submitting the form (with the cropped image encoded in base64) - I decode & resize it using the Intervention Image library:
public function decodeResizeAndStore(Request $request)
{
$croppie_code = $request->croppie_code;
// https://stackoverflow.com/a/11511605/4437206
if (preg_match('/^data:image\/(\w+);base64,/', $croppie_code, $type)) {
$encoded_base64_image = substr($croppie_code, strpos($croppie_code, ',') + 1);
$type = strtolower($type[1]);
$decoded_image = base64_decode($encoded_base64_image);
$resized_image = Image::make($decoded_image)->resize(300, 200);
// AND NOW I WANT TO STORE $resized_image using Laravel filesystem BUT...
}
}
Finally, I want to store the resized image using Laravel's filesytem (File Storage) and that's where I'm stuck - when I try this:
Storage::put($path, (string) $resized_image->encode());
... it doesn't work. Actually, it is working something - it looks like there is some memory leak or something, the browser's tab freezes, my RAM & CPU usage go high...
So I just tried:
dd($resized_image->encode());
... and yes, this is where it definitely crashes - when using encode() method.
I am not sure why, maybe this is happening because I'm not working with a standard image upload but with decoded base64?
But, on the other side, Intervention Image can create a new image instance from the base64 as well as from the decoded base64: http://image.intervention.io/api/make
... and, in my case, this works OK:
$resized_image = Image::make($decoded_image)->resize(300, 200);
I could then use the save() method and everything would work OK. But I need to use Laravel's File Storage.
Do you know how I can handle this?
Assuming you're using latest version of Laravel (5.7):
you can use stream method like so:
// use jpg format and quality of 100
$resized_image = Image::make($decoded_image)->resize(300, 200)->stream('jpg', 100);
// then use Illuminate\Support\Facades\Storage
Storage::disk('your_disk')->put('path/to/image.jpg', $resized_image); // check return for success and failure

Save image in hosted server in PHP

I am working on a project using ( LARAVEL 5.5) that creates QR codes and i would like to store the image that is built in the backend
this the code that creates the image and convert it to base64:
public function CreateQr($data)
{
//Encrypting the data + creating the QR code with the Encrypted text
$enc = (new CryptoController())->Encrypt($data);
$src = base64_encode(QrCode::format('png')->size(250)->generate($enc));
// the ID of the logged user
$userID = $_SESSION['currentUserID'];
$target = 'img/'.$userID.'/';
if (!file_exists($target)) {
mkdir($target, 0777, true);
}
copy($src, $target);
return $src;
}
the CreateQr function receives a string text and convert that text
into a crypted QR code
I would like to store the ( base64 ) images into a folder ( $target )
I have a seen almost every question in here but didn't find something closer to my needs.
This is my first time using PHP so please do advise me on how to fix and approache this. thanx in advance
The way that you are storing the files is recommended. Visit the Laravel's documentation in File Storage on how to create and move files.
Also, In my opinion, do not convert the files to base64, store the QR image as you wo

Upload image from Android app through php move_uploaded_file

I'm trying to upload image file from my android app to a server. This is my php file that is processing incoming images from my website and iOS app to be used in move_uploaded_file function:
<?php include '../../../init.php';
$post_id = $_GET['post_id'];
$image_temp = $_FILES['image']['tmp_name'];
$image_name = $_FILES['image']['name'];
$image_ext = strtolower(end(explode('.', $image_name)));
upload_page_image($image_temp, $image_ext, $post_id);
Now, in android I came up to the point when I have Base64 encoded string:
params.put("image",imageToString(bitmap));
Then, I bring this to my PHP file like this:
<$php
....
$image = base64_decode(_POST['image']);
...
But now, how do I brake it in to $_FILES['image']['temp_name'] and $_FILES['image']['name']? All the other examples I went through are using file_put_contents. I need it to be move_uploaded_files.
Any help is appreciated.
Thanks
this worked for me enter link description here
You must submit image using Multipart Form Data for use move_uploaded_files in php
Ok, that was the most difficult stuff I ever went through. The problem is that when I use php function file_put_contents(), it first makes everything easy on android app side, just convert bitmap to Base64 and pass it to php file. But... one very important thing got lost in this process.... it's that $exif['Orientation'] when using exif_read_data. This parameter is not there any more.
Now, when going back to android app and trying to get image orientation before sending the file to php, the standard function such as below:
ExifInterface exifReader = new ExifInterface(mFilePath);
exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
.. or similar doesn't work. It always returns 0. ALWAYS
Solution came from using Glide https://github.com/bumptech/glide library and get current image orientation using:
InputStream is = getActivity().getContentResolver().openInputStream(originalUri);
int orientation = new ImageHeaderParser(is).getOrientation();
Once you get orientation, you pass image as Base64 and orientation number and then process upload and do orientation change and so on.

Check if file is an image or an xml on aws S3

I need a away to see if an image exist on the S3 cloud, I'm build an APP to sort of merge data from local to the web, it is actually simple and I almost done, from the local app I'm getting a json post... so on that json I'm getting an image name "prety_image.jpg" which then is save to the DB, but before i save it I need to check if that image exist in the S3 cloud, I have the S3 url ... //foo.cloudfront.net/ and to display the image all I do is add the name of the image... for what I have seen is that if the image do exist it display the image.. but if the image doesn't exist I'm getting an xml file
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>13A57760</RequestId>
<HostId>3/uj7Ro77bGnjzGZVABUXvVVg4=</HostId>
</Error>
so what I want is that if i'm getting this error then save a blank image if not then just save the image... but I don;t know how to do it... I haven't use aws sdk, i downloaded but I don't know what to do with it... I have use other SDK's but AWS SDK is just way to much for my brain..
I find a solution without the use of AWS SDK, is actually very simple...
This function is inside my class but I'm just gonna put it here...
function httperr($url)
{
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
The I just call it like this
if (intval(httperr(S3AWS . $img)) === 200) {
$image = '<img src="'.S3AWS . $img.'" > <br>- Exist';
} else {
$image = 'No image here...';
}
the S3AWS is the url and the $img is just the name of the image... so if the header is ok 200 then the image do exist if not then there is no image, and is working just fine...

Categories