How to use Base64 Encoded Image as img tag's source? - html

now, I am make thumbnail pages using BASE64.
I store Image on server, and call them back as Base64 on client.
I want use these Base 64 encoded Imgs as src.
It it work well
. but, I got an err like that
GET http://localhost:8080/%7B%7Bitem.THUMBNAIL%7D%7D 404
what is problem? and it is proper way to use base64 as src without any convert?
here is my Source
Script
$http.get( "app/dashboard/recentWithInfo").then( function( rtn) {
rtn.data.map( item => {
if( item.THUMBNAIL){
item.THUMBNAIL = "data:image/png;base64," +item.THUMBNAIL.body;
}
})
$scope.items = rtn.data;
});
HTML
<img class="block-thumbnail-img" ng-if="item.THUMBNAIL != null" src="{{item.THUMBNAIL}}">
<h2 ng-if="item.THUMBNAIL == null" style="color:#ccc"> No THUMBNAIL</h2>
Additionally, I use Springboot and AngularJS.
Thanks in advance!

Yes you can use base64 images but make your the images are small and less in no.. below is a basic code where base64 images are called into html pages and rendered
<div>
<p>Taken from wikpedia</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>
check the fiddle http://jsfiddle.net/hpP45/

Related

Why is my svg image not found while the url is correct?

I'm using https://identicon-api.herokuapp.com/ to embed identicons in my page. You can choose either SVG or PNG. PNG works just fine, but for performance reasons, I want to use SVG. But when I use SVGs, the image isn't found, and the alt-text is displayed.
Nevertheless, the URL is valid, and when I open the image in a new tab, it's rendered correctly.
<img
v-if="iconsenabled"
style="max-width:1.75em"
:src="'https://identicon-api.herokuapp.com/'+e.author+'/2000?format=svg'"
:alt="e.author+`'s identcon.`"
:title="e.author+`'s identicon. Toggle 'show icons' to see the name instead`"
/>
(BTW, Yes, I'm using Vue, but that doesn't affect anything)
The SVG has an incorrect MIME type set by the server. <img> requires the MIME type to be image/svg+xml, but https://identicon-api.herokuapp.com/ currently sets it to text/html, which causes a loading error:
For reference, here's a sample SVG with the proper MIME type that renders correctly in an <img>:
<img src="https://upload.wikimedia.org/wikipedia/commons/4/42/Sample-image.svg" width="200">
As a workaround, you could fetch the SVG, convert it to a base64 string, and use that as a data URL:
<img :src="'data:image/svg+xml;base64,' + __SVG_BASE64__">
Example:
new Vue({
el: '#app',
data: () => ({
myImgUrl: ''
}),
mounted() {
fetch('https://identicon-api.herokuapp.com/tony19/2000?format=svg')
.then(resp => resp.text())
.then(text => this.myImgUrl = 'data:image/svg+xml;base64,' + btoa(text))
}
})
<script src="https://unpkg.com/vue#2.6.12"></script>
<div id="app">
<img :src="myImgUrl" width="200" height="200">
</div>

How to set filename for base64 rendered pdf using object tag?

Well I am using the following code to render a PDF that is returned from a webservice as base64 string.
<html>
<object data="data:application/pdf;base64,{!pdfStampingResponse.pdfBase64}" type="application/pdf" width="100%" class="internal">
<param name="view" value="fitH" />
</object>
</html>
This works well but I want to set the download filename when user tries to save the file. Somehow I am unable to find a working solution. Any idea how to do this ?
please try this
const FILE_NAME = 'myfile.pdf';
const file_header = ';headers=filename';
fetch('https:your-url/myfile.pdf?dl=0').then(r => r.blob())
.then(blob=>{
const f = new FileReader();
f.onload = () => myPdfViewer.src = f.result.replace(';', file_header + encodeURIComponent(FILE_NAME) + ';');
f.readAsDataURL(blob);
});
Then insert id myPdfViewer to an iframe.
I hope it can help.
You can use the following code syntax
<object data="data/test.pdf" type="application/pdf" width="300" height="200">
alt : test.pdf
</object>
Found this here.
Hope it helps.
Use download="yourfilename" in anchor tag.
The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.
PS:This attribute is only used if the href attribute is set.
Is there any way around this so that I can control the name?
No. You cannot control the name of a file stored at user local filesystem.
See here: https://stackoverflow.com/a/44061387/9913319

Image tags returned by KCfinder are incomplete on CKeditor

Image tags returned by KCfinder are incomplete on CKeditor and not displayed/saved correctly. Note that i am using an inline CKEditor and KCFinder for image upload.
Here are the integration codes:
ckeditor/config.js
config.filebrowserBrowseUrl = base_url+'/js/kcfinder/browse.php?type=files';
config.filebrowserImageBrowseUrl = base_url+'/js/kcfinder/browse.php?type=images';
config.filebrowserFlashBrowseUrl = base_url+'/js/kcfinder/browse.php?type=flash';
config.filebrowserUploadUrl = base_url+'/js/kcfinder/upload.php?type=files';
config.filebrowserImageUploadUrl = base_url+'/js/kcfinder/upload.php?type=images';
config.filebrowserFlashUploadUrl = base_url+'/js/kcfinder/upload.php?type=flash';
On page HTML
<div id="page_body" contenteditable="true" class="full">...</div>
On page JS
<script type="text/javascript">
CKEDITOR.disableAutoInline = true;
var editor = CKEDITOR.inline( 'page_body', {
on: {
focus: function(event){
var data = event.editor.getData();
alert(data);
},
blur: function( event ) {
var data = event.editor.getData();
var page_id = <?php echo $this->uri->segment(3) ?>;
var page_link =$("#page_link").val();
$.ajax({
type: 'POST',
url: '<?php echo site_url('admin/dashboard/ajaxChangePageData') ?>',
data: { page_id: page_id, page_body: data,page_link:page_link },
beforeSend:function(){},
success:function(data){},
error:function(){ alert("Error"); }
});
}
}
} );
</script>
Strange is that i can browse the server/upload without any error with KCFinder i can even select an image from the server and the image is shown successfully in the content. but the code width height info are not present after a reload. I figured that the html created for the image was incomplete
in source mode i see-
<img alt="" src="/gchalk/content/images/333(1).jpg" 300px; height: 224px;" />
The situation just gets worse if for the second time i make some changes to the div say add some text. The image is lost and its treated as text, the above piece of code is shown as
in source mode-
<img alt="" data-cke-saved-src="/gchalk/content/images/333(1).jpg" src="/gchalk/content/images/333(1).jpg" 300px;="" height:="" 224px;"="">
and it appears on browser/editor as -
<img alt="" data-cke-saved-src="/gchalk/content/images/333(1).jpg" src="/gchalk/content/images/333(1).jpg" 300px;="" height:="" 224px;"="">
I am tearing my hair for a day and cant find a way around. Please help me out to figure how to solve it.
Oh, and for the record the text is saved in MySQL as "TEXT" through the ajax post i am pretty sure its not a problem but still just saying!
I notice the image tag gets messed up in the default ckeditor(not inline) too.
Things that could effect the output of your code :
1- Magic Quotes when using PDO. if they are ON, turn them OFF in you php.ini! they are deprecated. Why am I telling you this? will because in your source mode you had 300px; height: 224px;" when you stored it and displayed it you had 300px;="" height:="" 224px;"=""
2- your CKeditor package. Try to download and reupload your Ckeditor (Update it to the last version if possible)
other than that, I do not see anything wrong with the code you have provided. Good luck!

capturing webview as a image snapshot using winjs

I have the following code inside the HTML body that contains several images inside div elements.I would like to capture the whole seen as it is visible on the webview.
<div data-win-control="SdkSample.ScenarioOutput" id="grid">
<img id ="Img0" src="/images/0.png" class="GestureTile"/>
<img id ="Img1" src="/images/1.png" class="GestureTile"/>
<img id ="Img2" src="/images/2.png" class="GestureTile"/>
<img id ="Img3" src="/images/3.png" class="GestureTile"/>
<img id ="Img4" src="/images/4.png" class="GestureTile"/>
<img id ="Img5" src="/images/5.png" class="GestureTile"/>
<img id ="Img6" src="/images/6.png" class="GestureTile"/>
<img id ="Img7" src="/images/7.png" class="GestureTile"/>
<img id ="Img8" src="/images/8.png" class="GestureTile"/>
<img id ="Img9" src="/images/9.png" class="GestureTile"/>
</div>
Is it possible to directly capture via winjs or should I approach some other way?.
Thanks,
As far as I know (I'd love to hear otherwise), the only thing that can be captured is the contents of a canvas. I don't think you can captured rendered HTML. Sorry.
I know the topic is a bit old, but as of 8.1 you can grab the webview content as a Blob and then do whatever you need to with it (send it to a server, create a data URI and put it in an <img>, etc...
myWebview.capturePreviewToBlobAsync().done(
function(imgBlob) {
// do whatever you need to with the Blob
},
function(err) {
// handle errors... not sure what you might get here, honestly
}
);
I have not yet implemented this, so I can't say if there are any gotchas.
UPDATE
So I tried implementing the above, and this is what it says in the documentation, but it didn't work for me. I'm going to leave it because the commenter below says it works for them. For me, I had to use the code below instead as the capturePreviewToBlobAsync method did not return a promise. Not sure why...
var myWebView = document.getElementById("theWebviewToCapture");
var captureOperation = myWebView.capturePreviewToBlobAsync();
captureOperation.addEventListener("complete", function (e) {
var inputStream = e.target.result.msDetachStream();
// Now you could copy that input stream to a file stream or whatever...
});
captureOperation.start();
Check out the example on this page (search for "capturePreviewToBlobAsync").

How to remove blank image container?

How can I remove a blank image container if the image's source is unavailable please? I would only like the image to appear if its source is available. Otherwise, I don't want anything to appear, including the blank container.
HTML image syntax:
<img src="url" alt="some_text">
You are able to do this using JavaScript and the error event on the img tag.
For example:
<img src="url" alt="some_text" id="myimage">
<script>
var imgElement = document.getElementById('myimage');
imgElement.addEventListener('error', function()
{
this.parentNode.removeChild(this);
}, true);
</script>
(Oops, sorry #JosephSilber, modified my example to be a bit different ;-))
Your best bet would be using server-side processing if you know that your image isn't going to be available. If you're intentionally leaving the src blank, for example, you can do something like this in PHP:
<?php
$src = ''; // or something else
if (!empty($src)) { ?>
<img src="<?=$src?>" alt="some_text" />
<?php } ?>
Loop through your images, and bind onerror:
imgNode.onerror = function () {
this.parentNode.removeChild(this);
};