Laravel Nova: how to display image from binary string? - laravel

I save PNG's binary content in database.
I want display this PNG's on page without temporary save file on disk.
I think need generate img tag like <img src="data:image/png;base64,......
But I do not understand how it is better to implement it and what type of field to take as a basis.
Image::make('Image')->displayUsing(function($item) {
$mime_type = 'image/png';
return 'data: ' . $mime_type . ';base64,' . base64_encode($item);
}),
But Laravel Nova generated:
<img src="http://172.18.0.3/storage/data: image/png;base64,......" class="rounded-full w-8 h-8" style="object-fit: cover;">
Added unnecessary http://172.18.0.3/storage/and rounded class.
How to prevent it adding?

Work code for Laravel Nova 2.0.1:
Image::make('QRCode', 'qrcode')->thumbnail(function($value, $disk) {
return 'data: image/png;base64,' . $value;
})->preview(function($value, $disk) {
return 'data: image/png;base64,' . $value;
})->displayUsing(function($value) {
return base64_encode($value);})
Also need remove rounded-full from field.thumbnailUrl?t("img",{staticClass:"rounded-full w-8 h-8", in file public\vendor\nova\app.js

Override thumbnail & preview for image url
Try below code snippet
Image::make('Image')->thumbnail(function($value, $disk) {
return 'data: image/png;base64,' . base64_encode($value);
})->preview(function($value, $disk) {
return 'data: image/png;base64,' . base64_encode($value);
}),

Related

How to store image as URL in Laravel 8.*

In my application I add photos that are then scaled, I call them to my views using {{asset()}}
Everything works fine, but for my mobile app I need to send to API URL of image instead of just image path called from db.
That's how I save images now:
$image = $request->photo_patch;
$filename = time() . '.' . $image->extension();
$event->photo_patch = $filename;
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1200, 630);
$image_resize->save('storage/images/' . ($filename));
$event->save();
Example of saved image name:
1630531230.jpg
That's how I get image on view:
<img src="{{ asset('storage/images/' . $eventView->photo_patch) }}">
What I tried:
$url = Storage::url($filename);
$event->photo_patch = $url;
After this file name looks like this
"/storage/1631043493.png"
But that isnt really URL
What can I do to save photo path like this:
"localhost/storage/images/1631043493.png"
Edit:
# Фарид Ахмедов suggested to call whole URL in API.
How can I do that then?
This is what I tried:
Route::get('/events/{id}', function($id){
return [
Event::findOrFail($id),
'image' => asset('storage/images/'.$this->photo_patch)
];
});
That's it.
url(Storage::url($filename));
But I think, you don't need to save the whole path. You can convert it to full URL before sending response.

I want to Download multiple images as a zip file using laravel 7

I am a beginner at laravel and I want to downloads multiple images as a zip file using laravel but I do not have an idea how can I do that please help me thanks.
InboxController
public function dowloads($id)
{
$clientfiles = Inbox::where('id', $id)->first();
dd($clientfiles->file);
// "["phpIgRq3Q.jpg","phpe6b34M.jpg","phpnPGN2M.png","php8CQh5P.jpg"]"
$files =config('yourstitchart.file_url');
// $files = "http://localhost/yourstitchart.com/web/public/uploads/images/"
}
HTML view
<a href="{{ route('download.inbox',$digitizingInbox->id) }}" class="download
btn btn-warning">Download
</a>
Route
Route::get('downloads/{id}/files','DigitizingInboxController#dowloads')->name('download.inbox');
You can also use Chumper/Zipper package to make zip files. It is used by many developers. check here
You can try it like:
$zipper = Zipper::make(public_path('/documents/deals.zip'));
$clientfiles = Inbox::where('id', $id)->first();
foreach ($clientfiles->file as $file) {
$zipper->add(public_path($file)); // update it by your path
}
$zipper->close();
return response()
->download(
public_path('/temporary_files/' . "deals.zip"),
"deals.zip",
["Content-Type" => "application/zip"]
);

Getting 404 on images in public folder using a custom function in Laravel

I'm trying to use a simple function to auto-populate the images in a gallery page, but getting 404's on the images. Everything else is working as intended.
I'm using material css and all of that fluff is working correctly. I'm getting the correct number of image cards.
The path echoed out correctly, and within the function the name of the images is being generated with no issues. I'm using the {{asset}} function of Laravel and my images are in the public folder, so access shouldn't (?) be an issue.
public static function addImg($dirname){
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<div class='col s12 m6 l4'>
<img src='{{asset({$dirname}{$curimg})}}' class='materialbox responsive-img card'>$curimg
</div>";
}
}
}
and the call is
{{$gallery::addImg('/css/img/')}}
Console shows "GET http://127.0.0.1:8000/%7B%7Basset(~snip for privacy~/public/css/img/hero%20(2).jpg)%7D%7D 404 (Not Found)"
You are using Blade syntax in a regular string. It isn't going to be parsed. You have to return the string how you actually need it to be.
Any interpolation you need has to be done while defining the string like any other regular string in PHP.
$location = asset($dirname . $curimage);
echo "<img src='{$location}' ...>{$curimage}";
Also you are calling {{ $gallery::addImg(...) }} which doesn't return anything that could be echoed by Blade. {{ }} is for echoing in Blade, it is not for just executing PHP statements.
This sounds like something you could make into a custom Blade directive though.
You can't use blade syntax inside a string because blade will parse only the .blade.php file.
But you can just use simple php concatenation:
public static function addImg($dirname){
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if (!in_array($curimg, $ignore)) {
echo "<div class='col s12 m6 l4'>
<img src='" . asset($dirname . $curimg) . "' class='materialbox responsive-img card'>$curimg
</div>";
}
}
}

Laravel 5.1 Snappy pdf image not rendering in pdf file

I am using barryvdh/laravel-snappy to generate pdf file. I have two image files 1. yourlogohere.png is in public/image/ folder and 2. logo2.png is in folder other than public i.e. storage/app/logo and to get this file I defined a route (www.example.org/logo/logo.png) and use following code to access it.
public function logo($filename)
{
$file = Storage::disk('local_logo')->get($filename);
$mime = 'image/png';
return (new Response($file, 200))->header('Content-Type', $mime);
}
Problem:
When I use following code to generate pdf from the html containing the first file, pdf contains the yourlogohere.png image
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/images/yourlogohere.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
But when I do exact same thing for the second file, pdf does not render the image.(When I open the link http://www.example.org/logo/logo2.png in browser I get the image). What am I missing?
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/logo/logo2.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
Thanks,
K
You can also do:
<img src="data:image/jpeg;base64,
{{ base64_encode(#file_get_contents(url('your.image.url'))) }}">
I think I got the what the problem is, the route to access the image is via auth, even when user is logged in while accessing the snappy, the wkhtmltopdf exe runs in a shell that is totally different session. Now the right fix would be to be embed the image in the html that is sent to snappy instead of the link, Which I am not sure how I will do? Any suggestions welcome there.
Update:
I as able to convert the image to data:image/png;base64, and embed it in html.
$html = view('mytemplate.default', compact('variable1', 'variable2'))->render();
/*Convert logo image to base64 before pdf conversion*/
//search for <img src="http://example.org/mytemplate/logo/logo1.png">" and replace the src with data:image/png;base64,
$search = '/(<img\s+src=["\'])([^"\']+)(\/mytemplate\/logo\/)(.*)(\.)(.*?)(["\']\s+[^>]+>)/';
$html = preg_replace_callback($search, function ($matches) use ($invoicedetail) {
$filename = $matches[4] . $matches[5] . $matches[6];
$file = Storage::disk('local_logo')->get('yourlogohere.png');
$mime = "image/png";
$mytemplate = MyTemplate::where('logo_filename', '=', $filename)->first();
if (!empty($mytemplate)) {
$file = Storage::disk('local_logo')->get($mytemplate->logo_filename);
$mime = $mytemplate->logo_mime;
}
$base64 = 'data:' . $mime . ';base64,' . base64_encode($file);
return $matches[1] . $base64 . $matches[7];
}, $html);
$pdf_filename = 'template' . $mytemlpate->id . '.pdf';
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $filename);
$snappy = App::make('snappy.pdf');

WordPress plugin: finding the <!--more--> in the_content

I'm writing a WordPress plugin that filters the_content, and I'd like to make use of the <!--more--> tag, but it appears that it has been stripped out by the time it reaches me. This appears to be not a filter, but a function of the way WordPress works.
I could of course resort to reloading the already-loaded content from the database, but that sounds like it might cause other troubles. Is there any good way for me to get the raw content without the <!--more--> removed?
Chances are, by the time your plugin runs, <!--more--> has been converted to <span id="more-1"></span>
This is what I use in my plugin, which injects some markup immediately after the <!--more--> tag:
add_filter('the_content', 'inject_content_filter', 999);
function inject_content_filter($content) {
$myMarkup = "my markup here<br>";
$content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'."\n\n". $myMarkup ."\n\n", $content);
return $content;
}
You can use the follow code:
The !is_single() will avoid display the more link in the View Post page.
add_filter('the_content', 'filter_post_content');
function filter_post_content($content,$post_id='') {
if ($post_id=='') {
global $post;
$post_id = $post->ID;
}
// Check for the "more" tags
$more_pos = strpos($filtered_content, '<!--more-->');
if ($more_pos && !is_single()) {
$filtered_content = substr($filtered_content, 0, $more_pos);
$replace_by = '<a href="' . get_permalink($post_id) . '#more-' . $post_id
. '" class="more-link">Read More <span class="meta-nav">→</span></a>';
$filtered_content = $filtered_content . $replace_by;
}
return $filtered_content;
}
Based on Frank Farmer's answer I solved to add thumbnail photo after the generated more tag (<span id="more-...) in single.php file with this:
// change more tag to post's thumbnail in single.php
add_filter('the_content', function($content)
{
if(has_post_thumbnail())
{
$post_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class'=>'img img-responsive img-thumbnail', 'style'=>'margin-top:5px;'));
$content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'.$post_thumbnail, $content);
}
return $content;
}, 999);

Resources