I have a gd image reference (I've already manipulated the image how I want to), and now I want to store the image. Instead of saving it directly to a file, is there a way I can get the binary data, that way I can convert it to base64 then save the base64 string. The only option I've been able to find is imagejpeg($image), but that either saves it as a file or prints it directly to the browser.
You can output to the buffer, and then capture it in this way.
ob_start();
imagejpeg($image);
$data = ob_get_contents();
ob_end_clean();
Related
Context
I have access to database. That database contains image data something like 0xFFD8FFE....................09090
Problem
I need those cryptic(hex) data to convert into visual image. I am not being able to find any way.
I have tried
$image = base64_decode(file_get_contents('img.txt'));
$db_img = imagecreatefromstring($image);
Header("Content-type: image/jpeg");
imagepng($db_img);
Here img.txt is file name containing the hex data.
It Doesn't work. How can I achieve this?
To turn a string like 0xFFD8FFE...09090 into actual binary data, you need to first get rid of the "0x" prefix, which is not part of the actual data, and then do a simply hex → bin conversion:
$data = '0xFFD8FFE...09090'; // maybe from file_get_contents
$binary = hex2bin(substr($data, 2));
If you don't know what type of image it is then, the most efficient method to find out is probably the use of finfo:
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->buffer($binary);
header('Content-Type: ' . $type);
echo $binary;
There's no need to use gd to reencode the image if $binary already is an image. You may want to inspect $type here a bit to verify that it's really the kind of file you expect before blindly outputting it.
When I take content of a picture I try to dump it like that:
$filename = '(900).jpg';
$im = file_get_contents($filename);
var_dump(serialize($im));
When the picture is under 1mb everything works, but if it is more than 1mb browser crash can you tell me why is that a browser issue or some limitation of file_get_contents() function?
The only limitation of file_get_contents might be the memory which is allowed for PHP to use. And the default is about 128 MB.
It is a browser "issue" if you want to call it that. Outputting so much debug information to the browser is not a good idea as you can see. Additionally there is no benefit in viewing a binary file as text.
If you want to find out if the variable is set, you can use functions to check the size of the (binary) string e.g. mb_strlen().
A better way would be this
$filename = '(900).jpg';
$im = file_get_contents($filename);
// check if the file could be loaded
if ($im !== false) {
// start your processing
}
But this does not check what kind of file you have loaded into the string. If you must store the file into the database - which is considered very evil - you can either store the binary string into a BLOB type row or encode the binary string with base64_encode() and store it into a text type. Both of this solutions are also not recommended!
If you need to store image information into the database, you should think about using references to the files - e.g. the file path. Your primary objective is to secure that the database information and the filesystem information is always synchronized.
I want to open a image url, convert it to an image, so I can use imagecopymerge() on it, then convert that to base64.
I was able to view the image data using file_get_contents, but I'm not sure how to combine that with imagecreatefromstring, imagecopymerge, and base64_encode.
The way I finally did it was to use
$img=imagecreatefrompng('url');
ob_start();
imagepng($img);
$imgString=ob_get_clean();
$imgBase64=base64_encode($imgString);
I found out that I can't convert an image to a string, and have to use the buffer.
I have a php script that generates an image then outputs the image as a png.
$img = imagecreatetruecolor($graphWidth,$graphHeight);
...
*drawing routines*
...
header("Content-type: image/png");
imagepng($img);
What I now need is to get the php script to base64 encode the image data and output that string (so I can eventually pass that encoded data to a java script which decodes and adds this image to a generated pdf).
I have tried many times to get it working myself using other stackoverflow posts/answers etc. but I don't understand enough about this technology to have gotten it to work.
Can someone help me with this please?
Thanks in advance,
Chris
The reason this doesn't work is because the image in $img is a resource, not an actual PNG image. When you call imagepng(), it outputs the image.
What I would do is create an output buffer, and base-64 encode it.
ob_start();
imagepng($img);
$buffer = ob_get_clean();
ob_end_clean();
echo base64_encode($buffer);
Don't forget to change your Content-Type header, as this is no longer image/png.
Can I base64 encode an image that I created on the fly, without first saving it to disk? As far as I know, base64_encode() only accepts strings, and I couldn't find a way to retrive image source object as string without first saving it, and load it with file_get_contents()
GD doesn't provide a method to return an output image as text, but you can fake it with the output buffering functions:
ob_start();
imagejpeg($handle); // no second parameter, will do output instead of writing to file
$img = ob_get_clean();
echo base64_encode($img);