How to hide an url with base64 like this?
<img src="show_image.php?url=aHR0cDovL2RvbWFpbi5jb20vaW1hZ2UuanBn">
In php you can encode in base64 with base64_encode(), and decode with base64_decode().
So, in your html page script you can do:
<?php
$imageUrl = 'the image url';
echo '<img src="show_image.php?url=' . base64_encode($imageUrl) . '">';
And of course, you'll need to decode it on your show_image script:
<?php
$imageUrl = base64_decode($_GET['url']);
$image = imagecreatefromstring(file_get_contents($imageUrl));
header('Content-Type: image/png');
imagepng($image);
Related
I'm trying to convert an image to base64 from an external source, below is my attempt
$imgsrc = file_get_contents('mydomain.com/assets/img/logo.png');
return response()->json([ 'img' => base64_encode($imgsrc) ]);
and below is the output,
data:image/png;base64,PCFET0NUWVBFIGh0bWw+DQo8aHRtbCBsYW5nPSJlbiI+DQogIDxoZWFkPg0KICAgICAgPG1ldGEgY2hhcnNldD0idXRmLTgiPg0KDQogICAgICA8dGl0bGU+Q29ubmVjdG5pZ2VyaWEgfCBFcnJvciA0MDQ8L3RpdGxlPg0KDQogICAgICA8bWV0YSBuYW1lPSJyb2JvdHMiIGNvbnRlbnQ9Ik5PSU5ERVgsIE5PRk9MTE9XLCBOT0FSQ0hJVkUsIE5PU05JUFBFVCIgLz4NCiAgICAgIDxsaW5rIHJlbD0ic2hvcnRjdXQgaWNvbiIgaHJlZj0iaHR0cHM6Ly9kZXZjbm1haW4uY29ubmVjdG5pZ2VyaWEuY29tL2Fzc2V0cy9pY28vZmF2aWNvbi5pY28iIHR5cGU9ImltYWdlL2ljbyI+DQoNCiAgICAgIDxtZXRhIG5hbWU9InZpZXdwb3J0IiBjb250ZW50PSJ3aWR0aD1kZXZpY2Utd2lkdGgsIGluaXRpYWwtc2NhbGU9MS4wLCBtYXhpbXVtLXNjYWxlPTEuMCwgdXNlci1zY2FsYWJsZT1ubyIgLz4NCiAgICAgIDxsaW5rIHJlbD0ibWFuaWZlc3QiIGhyZWY9Imh0dHBzOi8vd3d3LmNvbm5lY3RuaWdlcmlhLmNvbS9hc3NldHMvbWF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ZWxwLWFsdCI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktY3Jvc3MiPjwvaT4NCiAgICAgICAgICAgICAgPGkgY2xhc3M9InRpLXN1cHBvcnQiPjwvaT4NCiAgICAgICAgICAgICAgPGkgY2xhc3M9InRpLWFubm91bmNlbWVudCI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktc2lnbmFsIj48L2k+DQogICAgICAgICAgICAgIDxpIGNsYXNzPSJ0aS1wdWxzZSI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktbWFya2VyIj48L2k+DQogICAgICAgICAgICAgIDxpIGNsYXNzPSJ0aS1wdWxzZSI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktYWxlcnQiPjwvaT4NCiAgICAgICAgICAgICAgPGkgY2xhc3M9InRpLWhlbHAtYWx0Ij48L2k+DQogICAgICAgICAgICAgIDxpIGNsYXNzPSJ0aS1hbGVydCI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktcHVsc2UiPjwvaT4NCiAgICAgICAgICA8L2Rpdj4NCiAgICAgIDwvZGl2Pg0KICAgICAgPCEtLWVuZCBvZiBjb250YWluZXItLT4NCiAgPC9zZWN0aW9uPg0KICAgICAgICAgIDxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij4gDQoNCiAgICAgICAgICAgIHdpbmRvdy5jbl91cmwgPSAnaHR0cHM6Ly93d3cuY29ubmVjdG5pZ2VyaWEuY29tLyc7DQoNCiAgICAgICAgICAgIHdpbmRvdy5iYXNlX3VybCA9ICdodHRwczovL3d3dy5jb25uZWN0bmlnZXJpYS5jb20vJzsNCg0KICAgICAgICAgICAgd2luZG93LmNhcnNfdXJsID0gJ2h0dHBzOi8vY2Fycy5jb25uZWN0bmlnZXJpYS5jb20vJzsNCg0KICAgICAgICAgICAgd2luZG93LmRlYWxzX3VybCA9ICdodHRwczovL2RlYWxzLmNvbm5lY3RuaWdlcmlhLmNvbS8nOw0KDQogICAgICAgICAgICB3aW5kb3cuZXZlbnRzX3VybCA9ICdodHRwczovL2V2ZW50cy5jb25uZWN0bmlnZXJpYS5jb20vJzsNCg0KICAgICAgICAgICAgd2luZG93LnJlYWxlc3RhdGVfdXJsID0gJ2h0dHBzOi8vcmVhbGVzdGF0ZS5jb25uZWN0bmlnZXJpYS5jb20vJzsNCg0KICAgICAgICAgICAgd2luZG93LmpvYnNfdXJsID0gJ2h0dHBzOi8vam9icy5jb25uZWN0bmlnZXJpYS5jb20vJzsNCg0KDQogICAgICAgICAgICAvLyBjb25zdGFudCBnbG9iYWwgc291cmNlcyByZWZlcmVuY2UNCg0KICAgICAgICAgICAgIHdpbmRvdy5nbG9iYWxfc3JjID0gImh0dHBzOi8vZGV2Y25tYWluLmNvbm5lY3RuaWdlcmlhLmNvbS8iOw0KDQogICAgICAgICAgICAgd2luZG93LmxvY2FsX3NyYyA9ICJodHRwczovL3d3dy5jb25uZWN0bmlnZXJpYS5jb20vYXNzZXRzLyI7DQoNCiAgICAgICAgICAgICANCiAgICAgICAgICAgIHdpbmRvdy5wYWdlID0gJyc7DQogICAgICAgICAgICB3aW5kb3cuY3RybCA9ICcnOw0KDQogICAgICAgIDwvc2NyaXB0Pg0KICAgICAgICA8c2NyaXB0IGRlZmVyIHNyYz0iaHR0cHM6Ly93d3cuY29ubmVjdG5pZ2VyaWEuY29tL2Fzc2V0cy9hc3NldHMvanMvYm9vdC5taW4uanMiPjwvc2NyaXB0Pg0KICA8L2JvZHk+DQo8L2h0bWw+DQoNCg==
but on <img> tag, its not rendering.
If you want to display the image, try this
$imageData = "/9j/4AAQSkZJRgABAQEAYABgAAD........";//base64_encode strong
echo '<img src="data:image/gif;base64,' . $imageData . '" />';
OR
$code_base64 = $row['content'];//base64_encode strong
$code_base64 = str_replace('data:image/jpeg;base64,','',$code_base64);
$code_binary = base64_decode($code_base64);
$image= imagecreatefromstring($code_binary);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
I have sent a base64 encoded string via AJAX to PHP and created an image resource with imagecreatefromstring - all is fine.
Now I want to get the base64 encoded string after resizing te image, but i CANT find a function to get the base64encoded string.
Taken from http://www.php.net/manual/en/book.image.php#93393
$image = imagecreatefromstring($file);
// start buffering
ob_start();
imagepng($image);
$contents = ob_get_clean();
echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";
imagedestroy($image);
Let's say I have the code that looks something like:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";
//
// more stuff here
//
?>
What do I replace /* what goes here? */ with so my image data will display?
Thank you.
What do I replace /* what goes here? */ with so my image data will display?
The location you highlighted is the so called src attribute of the img HTML-tagDocs. The value is a so called URIDocs.
In your case you want that URI to point to the image data in question. You have not specified which type the image should be output as, so I will assume it's a PNG image in the following example.
You now need to convert your image data into an URI. The most straight forward URI to create from the image data is a so called data: URIWikipedia:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);
echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";
//
// more stuff here
//
?>
Even this is the most straight forward way, it is not always recommended to do so because the image data will be returned with the HTML to the browser. If the image is large, this is commonly considered an overhead.
Instead of using the data: URI you can place any other URI in there as well, for example a HTTP URI that is pointing to a PHP script on your server that is returning the image. Such a script can be very simple:
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);
This is comparable to what Marc B suggested, see his answer as well.
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/jpeg');
imagejpeg($img);
should be all you need. Doing it with the image tag as you are, you'd need to output the image to a temporary file and point the image tag at that (which incurs a second HTTP request), or use a data url.
I think you can do something like this...
$src = "data:image/gif;base64," . $imageData ;
echo "<img src=\"$src\" alt=\"the image\" />";
You have to save the resource to a file first or output it using something like imagepng() in a separate request.
See imagecreatefromstring() documentation for more information.
If you want to use a Data URI scheme, you can try this instead:
<?php
// If your image is binary data. use `base64_encode($imageData)`.
$imageData = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
echo '<img src="data:image/png;base64,'. $imageData .'" />';
I have php code which create pdf thumbnail as follows;
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
?>
Which is working well. But if I want to display the image in a web page, I have to use <img src=""> tag. Is there any way to remove header("Content-Type: image/jpeg");
from the syntax and echo image using <img src="">..? Or anybody tell me how to use the syntax to display the image inside a web page.
I am running apache with php5 in my Windows Vista PC..
With Imagick, you could use base64 encoding:
echo '<img src="data:image/jpg;base64,'.base64_encode($img->getImageBlob()).'" alt="" />';`
However, this method is kind a slow and therefore I recommend generating and saving the image earlier $img->writeImage($path).
you can try to display the image by this way:
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";
Embedding an image using base64 is a COMPLETELY wrong way to go about the problem esp. with something stateless like a php web script.
You should instead use http parameters to have a single php file which can perform two tasks - the default will send html , and the parameter will instruct the php file to print the image. Below is the "standard" way to do it -
<?php
if (!array_key_exists('display',$_GET))
{
print('<html><head></head><body><img src="'.$_SERVER['PHP_SELF'].'?display=image"></body></html>');
} else
{
// The display key exists which means we want to display an image
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
}
?>
You can embed the raw image in you page, see the blog entry below for an example in page syntax.
http://www.sveinbjorn.org/news/2005-11-28-02-39-23
But i think it would be more productive to save the thumbnail on the filesystem and serve it as normal file. Otherwise you will be generating the thumbnail each time the page is accessed. Someone possibly uploaded this PDF file, so you may as well generate the thumbnail on upload time.
As I can see there are too many answers which are not accurate enough, so here goes mine:
This will print the image as you are doing it now(by the time of asking this question). As alternative to answer by #Vasil Dakov you should modify the snippet i gave you like this:
<?php
// ... Image generation goes here
header("Content-Type: image/jpeg");
ob_start();
print $im->getImageBlob();
$the_outputted_image = ob_get_flush();
?>
// Assuming that you use MVC approach and you are storing $the_outputted_image in a object and passing it to the view(ie. index.html or the HTML below the code).
//... Html code of index.html
<img src="data:image/jpg;base64 <?php print $the_outputted_image; ?>" alt="image" title="IMagick Generated Image" />
As another alternative is creating a script to generate the image, save it in some folder ( assuming img/ is the folder) and return only the path+filename+ extension to the file:
<?php
// ... Image generation goes here
header("Content-Type: image/jpeg");
$filename = 'img/' . md5(microtime()) . '.jpg'// Microtime is just as an example, you should use your own method.
$fp = fopen($filename, "x"); //Creating and opening the file for write-only
$im->writeImageFile($fp); //Writing the image to the file pointer (I would recommend writing it using, fwrite(), because it is binary-safe writing method)
fclose($fp);
?>
// Html
<img src="<?php print $filename; ?>" alt="image" title="IMagick Generated Image" />
documentation for Imagick::writeImageFile
In my case I found out a solution like this:
$im = new Imagick("http://www.yourserver.com/upload/file_name.pdf");
$im->setResolution(300, 300); // if higher image will be good to read
$im->setIteratorIndex(0); // read first page
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
ob_start();
print $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />"; //output as image
good luck.
The only solution would be to convert your image to base64 and include it as an embedded base64 image (data:image/png;base64, ). Further reference.
But this isn't supported in IE 6 and 7.
How can I put image from the memory to the browser, without saving.
For example:
function getImage()
{
$imageFile = imagecreatefromjpeg('Map.jpg');
$imageObject = imagecreatefrompng('image2.png');
imagealphablending($imageFile, true);
imagecopy(....);
$ret = array($imageFile, $imageObject) ;
return $ret
}
<?php $ret = getImage(); ?>
<img src = <?php $ret[0];? alt=''>
Is this possible, without saving?
Yes,
Just try imagejpeg($img);
and put into <img src= path to the PHP script which render the image
See sample at: http://php.net/manual/en/function.imagecreatefromjpeg.php
Maybe if you would code your image to base64 and use it like that, it would work:
<?php
$img_str = base64_encode($imgbinary);
echo '<img src="data:image/jpg;base64,'.$img_str.'" />';
?>
HTML:
<img src="data:image/jpg;base64,R0lGODlhCgAKAJEAAAAAAP///81Wv81WvyH5BAEAAAMALAAAAAAKAAoAAAIUjIViq+x7QpunwXoZ lXFu/mjIUgAAOw==" alt="image" />
I infered that you want to do this in one request.
You should have a script which sends proper headers and then it should be recognized as an image by the browser. Something like:
<?php
ob_start();
// assuming you have image data in $imagedata
$length = strlen($imagedata);
header('Last-Modified: '.date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: '.$length);
header('Content-Type: image/jpeg');
print($imagedata);
ob_end_flush();
?>