Displaying images in webpage without src URL - html

Recently i learned that i can display images in a web page without referencing an image URL as follows :
<img class="disclosure" img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oIGRQbOY8MjgMAAABVSURBVBjTfc6xDcAwCETRM0rt5nbA+49j70DDAqSLsGXyJQqkVxxwNOeMiEA+waW1VuT/inrvG7wikht8UETy2ygVMjO4O8YYTf6AqrZyUwYlygAAXo+QLmeF4c4uAAAAAElFTkSuQmCC">
I had another small bmp image that i wanted to display, so i opened it in vim and the img source looke like:
When i paste this code where it needs to be pasted i only get "BMڀ"
How to i convert/paste this code properly to be used as an image source?

You need to encode it in Base64
http://www.motobit.com/util/base64-decoder-encoder.asp
Also you have to change (png) in...
<img src="data:image/png;base64,
according to image filetype.
Here is a little PHP function, haven't tested it.
function encode64($file){
$extension = explode(".", $file);
$extension = end($extension);
$binary = fread(fopen($file, "r"), filesize($file));
return '<img src="data:image/'.$extension.';base64,'.base64_encode($binary).'"/>';
}
echo encode64("test.bmp");
2.
function encode64($file){
$binary = fread(fopen($file, "r"), filesize($file));
return(base64_encode($binary));
}
echo '<img src="data:image/bmp;base64,'.encode64("test.bmp").'"/>';
Tested my second function... works great... http://debconf11.com/so.php

You can use online utilities like
http://software.hixie.ch/utilities/cgi/data/data
or
http://www.sveinbjorn.org/dataurlmaker
for the conversion.

To get the Base64 encoding of your image, you can use imagemagick's convert command with the INLINE: output format.
For example:
convert YourImage.png INLINE:PNG:YourImage_base64.txt
Now all you have to do in your HTML page is add <img src=" and "> around the content of the "YourImage_base64.txt" file.
Or you can directly write it all to STDOUT and add it directly at the end of your HTML file:
echo "<img src=\"$(convert YourImage.png INLINE:PNG:-)\">" >> some.html

The image data must be base64 encoded.

Related

Can't save img url in TinyMCE when it's rotated due to adding base64 data?

So I've been using TinyMCE for a while now in order to let users add images to pages, and they basically just drag the image into the editor and save it.
HOwever I've just realized a bug. If they put the image in the editor and rotate it, it won't save.
WHen I save an image without rotating it saves the HTML with the image source like 'img_2012.jpg' but when it's rotated it looks more like 'src=\"data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAeAAAAKACA.....' and on and on
So it looks like rotating it creates some kind of base64 encoding that won't allow it to save as content in the database.
Is there a reason for this?
$addContent = "
INSERT INTO htmlCont.content(content)
VALUES('$content');
";
if ($mysqlConn->query($addContent) === TRUE) {
$cont_id = $mysqlConn->insert_id;
$data['last_insert_id'] = $cont_id;
echo json_encode($data);
} else {
echo "Error: " . $addContent . "<br>" . $mysqlConn->error;
}
The data being inserted here is in html format and is coming from an ajax data object

Yii2 render image in browser without image tag using yii2-imagine

I'm using yii2-imagine
$imagine = yii\imagine\Image::getImagine();
Imagine->open('path/watermark.jpg')->show('jpg');
My problem is it not show the image, it show that:
����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222����"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�
Any idea?
You need to use the getImagine() function first to invoke the GD or Imagick which ever available instance then call ->open() and ->show() on the object. Moreover, you need to provide the $options for the image to display too. You can copy paste the following code inside an action in your controller and can see the result image. I just tested on my local system, and it is in working, just remember to provide valid path to the $source variable
use yii\imagine\Image;
$source="/path/to/your/image.jpg";
$imagine = new Image();
$options = array(
'resolution-units' => \Imagine\Image\ImageInterface::RESOLUTION_PIXELSPERINCH,
'resolution-x' => 300,
'resolution-y' => 300,
'jpeg_quality' => 100,
);
echo $imagine->getImagine()->open($source)->show('jpg',$options);
Apart from the above solution that displays the image in the browser if you want to display the image inside the img tag you can base64_encode the raw image data returned from the open() method and provide into the tag like below.
echo '<img src="data:image/jpeg;base64,'.base64_encode($imagine->getImagine()->open($source)).'" >';
Hope it helps

how do i load image from resource inside TembeddedWB

iam trying to load image from resource inside image htmlTag as example
<img src="'+ Resourceimage +'">
i tried to do something like this
function getFullHTML(res:string):string;
var
sURL : string;
resorceimg : TResourceStream;
begin
resorceimg := TResourceStream.Create(HInstance, res, RT_RCDATA);
sURL := 'res://'+ resorceimg +'';
end;
then i call the function like this
<img src="'+ getFullHTML('imagename') +'">
but i cannot use a TResourceStream into string i think iam doing it in horrible way how exactly i can load image from resource into html image ?
You can use Data URIs with Base64 encoded images:
Embedding Base64 Images
The image is then embedded like
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
The anwers to the linked question list supported browsers.

How to download image object on page as a file in html?

I am creating an image dynamically on the page using dataURL,
var aImg = document.createElement('img');
aImg.setAttribute('src', dataURL);
aImg.setAttribute('alt', 'pic');
aImg.setAttribute('width', '438px');
aImg.setAttribute('height', '267px');
aImg.onclick = (function() {
//download the image object
})();
I am not sure what to do to download this image object that is a PNG image.
Can someone give hints?
If you want the image to be displayed the follwing should be fine :
aImg.src = YOUR_URL
if you want to save it on to the file , you shoud redirect and let the browser take care of the rest. JS redirect can be done as follows :
window.location.replace(dataURL)
If you want the browser to give a pop-up saying "Save File" check out this link : http://muaz-khan.blogspot.in/2012/10/save-files-on-disk-using-javascript-or.html

Get text from XML file and print it in HTML File

I have a simple XML file created in R that consists of the following lines:
<statistics>
<mean>15.75</mean>
<sd>2.83</sd>
</statistics>
I want to extract the mean and sd to a HTML page, that has a Flash graph and I would like this underneath:
Statistics
Mean = 15.75
Standard Deviation = 2.83
What is the easiest way to achieve this?
Regards,
Anthony.
You should use PHP and SimpleXml.
Just load your xml with simplexml:
$xml = new SimpleXMLElement(file_get_contents("statistics.xml"));
and afterwards echo the desired elements to the page (or add them to your template engine):
echo "Mean: ".$xml->statistics[0]->mean."<br />";
echo "Standard Deviation: ".$xml->statistics[0]->sd."<br />";
If you have more then one statistics element, for example:
<statistics>
<mean>15.75</mean>
<sd>2.83</sd>
</statistics>
<statistics>
<mean>25.75</mean>
<sd>28.3</sd>
</statistics>
Simply use a foreach loop to iterate trough each element:
foreach ($xml->statistiscs as $statistic) {
echo "Mean: ".$statistics->mean."<br />";
echo "Standard Deviation: ".$statistics->sd."<br />";
}
jQuery is one way to do it. This will dynamically load the xml file on the client. The downside is that you have to include the library which you can download here: www.jquery.com. It can be used to so many other things so look into it. Your code would be something like:
$(document).ready(function () {
$.get('address/to/xml.file', function(data) {
var mean = $(data).find('mean').text();
var sd = $(data).find('sd').text();
$("somecontainer").text(mean + "|" + sd);
});
});
Or do you want some server-side language to do your xml parsing and printing?