convert html image data url to a hosted image - php

I have a page that is generating image data urls from a snapshot tool and inserting the resulting string into a MySQL database via PHP. Later on I have a page that takes and uses those images. This would be fine except I need to save the resulting html to my server for some post processing and the length of the image data urls is giving me a headache and making the html files upwards of 8 to 10 MBs which slows down the entire process. The image looks something like this:
<img src="data:image/png:base64,iVBORw0K43+gAA4u...">
Where there is an extremely long string of characters making the resulting html very large. Is there a way to host this on my server as a png so the link is a normal looking image? Something like this:
<img src="http://www.mysite.com/image1.png">

What about converting base64 to an original image, what you would be doing is saving the image as a actual file to the server.
Php to convert base64 data to image
function toImage($base_code){
$img_file = imagecreatefromstring(base64_decode($base_code));
imagejpeg($img_file, 'new.jpg');
}
Calling the function
echo toImage($encoded_image);
Make sure to only pass the base64 encoded string without the image tag

You can save the files elsewhere, but my guess is that you've got a requirement the images exist in the DB. If you save the files elsewhere, you've just doubled your disk requirements.
In any case, the general fix for this would be to save that image string to disk, and in your DB keep track of the path. Then when you generate the HTML, you'd use the path in the DB, rather than the binary data in the DB.

Related

adding metadata to image's content WITHOUT saving the file. in PHP

given an image file path, I want to get its content - than add metadata to that content (either EXIF, IPTC, XMP etc.) and then display that image's content, with that metadata included (probably in base64 format), in an HTML tag without saving the file.
why without saving the file on server? the action I'm trying to perform is for a case a user saves the image file to his computer, for copyright purposes - I want his IP and the my site's URL to be stored in the metadata of the image he downloaded.
there will be thousands of users viewing the image in a minute so I cannot create temporary copies of this image on server.
I can generate either a JPEG or PNG format image - any format needed in order to make it work.
I currently know how to store IPTC meta data in a JPEG image using PHP's "iptcembed" but of course that stores (saves) the file.
I know you said you cannot create temporary files, but there is a manual temporary file that you delete yourself, and a PHP managed temporary file.
Have a look at tmplfile(), which allows you to save the file on the server but it will be cleared once the request is done.
Then you can do all the things you need, then get the content of the tmpfile as base64 and return that content. The file will be gone afterwards, so more or less it happened in memory per request.
I somehow missed the fact that the third argument of iptcembed, "spool", can do the job:
Spool flag. If the spool flag is less than 2 then the JPEG will be returned as a string. Otherwise the JPEG will be printed to STDOUT.
I tried it with the various options and it worked for me.

Is it possible to translate an image into raw text and then reverse it?

I have a picture. For whatever reason, I need that picture to be sent to an environment that can only receive text and not images. Images and other files must be sent through their filter and I want to get around this. I calculated that there would be 480,000 independent hex values being manipulated but this is really the only option I have. Also, is it possible to compress and uncompress it for less pixels being sent? I will need to send the picture from a PHP web server [lets say, mysite.com/image.php] and receive it in Lua, and my only connection to the server is over a web request. No ftp, no even loading image files. Just setting 480,000 variables to the different id's
Oh, one more thing: it needs to not crash my server when I run it. ;)
Convert your image to base64 (Eg: Can pass to the variable).
Eg: I converted PNG image
Base 64 image will look like this.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAYAAADEUlfTAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAE9JREFUeNpiYMADGLEJKssrCACp+Uw4JPYD8QdGHBIP7j58EMgCFDAAcvqBOBGI64FYAMpmYIFqAilYD6Udgbo+IBvXAMT/gXg9sjUAAQYAG6IS47QjgzEAAAAASUVORK5CYII="
You can use it in image source to display.
Hope this helps!

Figuring out where image data is being stored

Well in a particular website when I upload the image and then try to get the link of the image I get this:
data:image/svg+xml;base64,thebase64encodeOfMyImageCode
I have seen this in many of the website. After decoding the base64 code I find that its the code of my image file (guessed out by uploading svg image file ) . Can any one tell me how this work and where the image is actually being stored . There is no direct URL like website.com/image.svg etc. This is confusing me.
This image is not stored in a file. All the data for the image is stored in the data URL. The server most likely is storing this data in a database, which is then outputted in the webpage inline.
For more information, see MDN
When base64 encoding is used for the image - you simply cannot know where the actual image is stored, if it is available at all. It might not be stored on a file at all, but only on a database, or might be stored outside the domain. On some occasions, such images are generated in real time and not stored on a disk at all.

Displaying BLOB image with other content

Firstly this is for a simple university project, so time is more important than performance of database etc.
I have a database with images stored in blob files. Now some form outputs data from this database, but i want it to also output the image, along with the text.
I realise there is a bit of code to change headers to image, but then the text wont display, so is there a way to display the image and the text on the same page?
thanks a lot,
It is possible to embed images inside HTML, but you usually do it only if you have a really good reason to. Assuming you do, you write the following code, provided that $BLOB is the binary raw data of the image:
<img src="data:image/jpeg;base64,<?php echo base64_encode($BLOB); ?>"/>
Why not use image tags like normal wherever you need the images to be, and in the src attribute, specify a PHP script along with a get argument (for instance, <img src='image.php?name=something.jpg'>). This script then authenticates the request, accesses the database, sends the appropriate headers, followed by the actual BLOB data.

uploading image & getting back from database

Initially i added full source code for viewer. but it has some format problem and all the code was messed with HTML format. So now i am asking step by step.
1) I uploaded an image file to my MySQL database. (it has no error, i.e. loaded successfully)
2) Fetching it back to the browser from MySQL Database. It has also no problem(in some condition)
a) It is printing OK if i am printing it before the HTML Tag.
b) I am not able to see any basic HTML design after printing image on browser.
c) If i am printing it inside html tag. It is showing some special characters and numbers, i.e. value which we see in phpmyadmin if we execute the query (select image from pix;)
So, Can any one help me to print image by fetching the image from MySql database?
Or can any one convert this database saved image from MySQL into real image to store into hard-disk back to see/edit it?
You are making a fundamental mistake here. An image resource is always stored and requested in a separate file, not in the HTML source code of the page you want to embed it in.
Such a separate file could be named, say, getimage.php.
It would fetch the image data from the database (e.g. from the record with the ID 890) and output it like e.g. so:
... code to fetch the image ....
header("Content-type: image/jpeg"); // or image/gif, image/png....
echo $imageData;
In the HTML page, you would embed it using
<img src="getimage.php?id=890">
There is the theoretical possibility to have image data directly in the HTML source but that's not what you want.
The answer is simple:
Do not store images in the database.
Store it on the disk.
It will eliminate all these problems for you.
As with the other answers, the best solution is to separate the image from the html source code.
That being said, if you absolutely require the image to be part of the page, and are not using internet explorer, it is possible to embed the image in the page.
it should be as simple (an example being a png) as:
<img src="data:<?php echo $mimeType ?>;base64,<?php echo chunk_split(base64_encode($imageData)); ?>" />
This only works in non IE browsers (I'm not sure about IE8, as I don't have it around to test at the moment).
Just make sure you get the mime type right (ex, image/png for png).
A little bit more info:
http://www.greywyvern.com/code/php/binary2base64

Categories