php - not sure how to describe - php

I got images in a php includes file.
<img class="imgmove" src='data:image/png;base64,<?php echo base64_encode(file_get_contents("images/form/firstname.png")); ?>'>
code works flawless. I was testing the design and I dragged an image in to an input bar and it spit back the following:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEkAAAAaCAYAAAGMbkETAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77uUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21ZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MjM4OEE4RjdCQzIwMTFFMTk5RjFBMDZBRkUxMTMyMkIiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6MjM4OEE4Rj//z8DMYAJXYCRkRGGz4NonApBoKio6D8QfwCxi4uLwVay4LDJsa+v7wBQsQGIDRIACCBGYtzIguY2kA5GJCEQv5EBZBIyBlqzH+q+BpAbQWIY1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ell me? Thanks -- and should I be securing my php file so that it doesn't show this code?

This is the base64 data uri for the image you dragged in . There is not an issue with you php file from what I understand.

You ran base64_encode on the image. That is what base64_encode does, it encodes the image data as base64. What you pasted is what base64 looks like.

Related

How to display an image stored in SQL Server field using image data type?

Good day
I'm trying to show an image using DATA URIs and encoding a string in base64 as shown below:
<img src="data:image/jpeg;base64,<?php base64_encode($FOTO) ?>" />
The image (JPG) is stored in the database in this way:
The problem is that using that DATA URI, it does not show the image on the screen. I don't know the error or if I have to do something different to be able to show that stored image.
Is there any other way to display these images stored in SQL Server?
Thank you very much for your time.
The data in db is not base64, you need to convert this hexadecimal data to base64. Php, as I know doesn't have a direct function to do that but it can be achieved with pack(). I leave it to you as homework to study this function.
Convert the data from db to base64 like:
$fFoto = base64_encode(pack('H*', $foto));
Then pass this $fFoto to image src like you are already doing.
<IMG src="data:image/jpeg; base64, <?php echo $fFoto; ?>" />

Storing Images In a PHP File

I have been trying to change the images in the WampServer Index.php File... When I looked at the Index.php File I saw something Interesting. The File its self was containing the Image in the RAW format. Latter in the Code the PHP Script Calls the Image using the URL like http://localhost/index.php?img=pngFolder called a Image file stored RAW in the PHP file as a png.
Here is a link to a website that has the index.php code...
Link
I would Like to know how to replicate this same process to work for other images. Granted the File will be larger but its a Price to pay for what I am doing for a project. The Reason I want some help with this is so I can do it correctly. I have Tried 2 times already. I managed to get it to call one image correctly but not some of the others. I'm not sure if the image is just a different encoding or what..... Any Help would be Appreciated.
They are using BASE64 to encode the images into text. You can google for a base64 encoder that will convert your images to text. You can then put the text directly in an <img src="..base64 text.." />
Here's one..
https://www.base64-image.de/
As far as getting the image from the url index.php?img=pngfolder..
You could put this at the top of the file
if(isset($_GET['img'])){
echo "...base64 string.."; exit;
}
Then you can use the index url as the src for your image and it will simply retrieve the base64 image

How to embed a graph (jpgraph) in a web-page

I am using this script which is one of the examples provided by jpgraph itself. When I put this on a web-page (blank) by itself, it's drawing the graph. But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
GD is already enabled according to phpinfo(). Iam using jpgraph 3.5.0b1.
The problem is that you are mixing HTML/text output with image output.
Any time you have a PHP script generate graphical content you have to handle the output differently than normal HTML or text.
There are a few routes, I'll cover them briefly here.
Save the output to a file and use that filename in your HTML
//replace this line:
// Display the graph
//$graph->Stroke();
// with these lines:
// Default is PNG so use ".png" as suffix
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
.. then use $filename in an image tag, like this (for example):
print '<img src="'.$filename.'" />';
Create a standalone PHP script that will output the graphic
You can use the example script as-is, alone in a file called graph_render_script.php. Then, in your HTML, you use that script as a source:
<img src="graph_render_script.php" />
Output base-64 encoded data
Another route is to use base-64 encoded image data. This is relatively simple to do:
print '<img src="data:image/png;base64,'.base64_encode($graph->Stroke()).'" />';
As always, the documentation should be your guide!
Documentation
http://jpgraph.net/download/manuals/chunkhtml/ch05s05.html
base64_encode - http://php.net/manual/en/function.base64-encode.php
This worked for me:
putting the php code that generates the image in a file...Then on my html page I do this:
<img src="graph.php" >
embedding the graph inline is indeed possible. You'll have to use output buffering to capture the image data, then base64 encode that data, then use that base64-encoded string as the source in an <img>.
Try something like this:
$img = $graph->Stroke(_IMG_HANDLER);
ob_start();
imagepng($img);
$imageData = ob_get_contents();
ob_end_clean();
?><html>
<head>
<title>JpGraph Inline Image Example</title>
</head>
<body>
<h1>JpGraph Inline Image Example</h1>
<img src="data:image/png;base64,<?php echo(base64_encode($imageData)); ?>" />
</body>
</html>
ceejayoz made an excellent point in that this method is almost never what you want. I do not recommend embedding the image data like this unless you have a good reason to do so and understand the downsides, i.e. older browsers lack support for it, and the page size is dramatically increased (not only from the image data but the fact the image data is base64 encoded, which balloons the length). I've used this method in the field myself on a project last year, but it was only because the client refused to make a second request for the image.
But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
You can't do that - you can't output an image as raw binary data within a page.
You need to put the code that generates the graph in a separate file, and use an image tag.
<img src="path/to/jpgraph/chart.php" />
The graph needs to be on its own page, you can't embed it. It outputs a raw JPG and you need to have no other content sent and have the proper headers to tell the browser it's a JPG. To embed the graph you'd make a different page called stats.php for example, and on that page you'd make an image tag pointing to the stand alone graph.
<img src=graph.php>
I've had this problem many times, I've noticed it happens when you have require() or include() in your Chart's script and those scripts have Data Base connections or special configurations.
I've solved this problem separating the data retrieving and the Chart drawing, passing parameters to the script or using SESSIONS to get the values.
Example of Embed image Chart in your PHP or HTML file:
<img src="linear_graph_customer.php?values=1,2,3,4|1,2,3,4|1,2,3,4&title=CHART&width=500&height=300" width="500" height="300" class="img" />
Regards.

get base64 php string into img tag

Ive got a php file 'get.php' that echo's a base64 string.
How would i display this as an image in another .php page?
something like this:
<img src="get.php?id=$lastid">
thanks for your help!
You can do something like this:
<img src="data:image/png;base64,BASE64STRING">
but if you BASE64STRING is the output of a php, then something like this would work:
<img src="data:image/png;base64, <?php include 'get.php?id=$lastid' ?>>
I know it may not be exactly but I hope you get the idea
If you want to display that as an image you will need to look into GD Library and generate a run time image using a function like imagettftext() after the image has been generated, your PHP script will send a header saying this is an image something like
header( "Content-type: image/jpeg");
and then echo the binary data of the generate image.
I found this question for you which should help you get started, look at the accepted answer:
Text on a image
No, you need to echo the output of get.php directly. Why don't you just include and call that function on the original page in source? Don't forget that the base64 string needs data:image/png;base64, or similar at the beginning.
<?php
include_once('get.php');
echo '<img src="'.base64img($lastid).'">';

Creating an Image from a Blob

I'd like to store some photos in MySql as blobs and would like to be able to retrieve the binary and recreate images from it to display back to the user.
Everything I've search uses some combination of file_get_contents, Base 64 encoding and the GD library. But every post I come across has a different requirement than what I'm trying to do and so the code examples aren't that helpful.
Can someone tell me what function calls I need to make or give me an order of operations for what needs to be done. Say I have the following code:
$someBlob = getImageBlobByImageId(1203);
Say this blob represents an image named "foo.jpg". How do I go from $someBlob to foo.jpg so I could put it in HTML like
<img src="<?php echo $fooImage; ?>"/>
Any hints or nudges in the right direction are greatly appreciated ;-)
Your HTML needs to point to a PHP script that retrieves the blob and sends it back on request as an image.
For example, in your HTML:
<img src="getImage.php?id=1203" />
And then, for getImage.php:
<?php
$id = $_REQUEST['id'];
header("Content-type: image/jpg");
echo getImageBlobByImageId($id);
exit();
?>
Be sure not to have the script output any content other than the header line and the blob data, or you won't get what you want.

Categories