php base64 encode imagecreatetruecolor - php

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.

Related

Lost data while converting image png to base64 string

I have an image.png which is generated dynamically (barcode) and which is returned to html as
<img src="data:image/png;base64,B64STR" />
but it sometimes get defects (white dots).
To convert image to base64 string I use this code:
ob_start();
imagepng($img);
$contents = ob_get_contents();
ob_end_clean();
return base64_encode($contents);
Can be the output buffer be the cause of lost data? Can I convert the image to base64 some other way?
imagepng return a bool. Use file_get_contents without ob :
return base64_encode(file_get_contents($img));

GD imagefilter no image output

I'm trying to convert an jpg image to grayscale with GD imagefilter but i can't get the filtered image to output on the browser. I use this code
<?php
$thumb="w=250&q=100";
$imgstring = get_image('cover_image',1,1,0,NULL,$thumb);
$im = imagecreatefromjpeg($imgstring);
imagefilter($im, IMG_FILTER_GRAYSCALE);
header("Content-type: image/jpg");
imagejpeg($im);
imagedestroy($im);
?>
**The $imgstring is the url string of the image ('http://xxx.xx/image.jpg').
With the header content line the result is an error "Warning: Cannot modify header information - headers already sent by..." followed by what appears to be the image code (a lot of question marks characters and other garbage).
Without the header content line the result is just the image code(?) again (symbols and characters)
What's wrong?
The error is exactly what it says - something in your code has caused output to be performed, which kills the header() call, preventing the content-type from being set. In the absence of any valid content-type header, the client browser assumes text/plain, and is showing your JPEG image - as raw "garbage".
You need to figure out WHERE in your code that output is being performed (I'm willing to bet a shiny penny it's in that get_image() function, and eliminate it.

base64 encode an image without saving

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);

PHP: GD reference to binary

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();

How can I convert a large string of base64 image data back to an image with PHP?

I have a large string of base64 image data (about 200K). When I try to convert that data by outputting the decoded data with the correct header, the script dies, as if there isn't enough memory. I get no error in my Apache logs. The example code I have below works with small images. How can I decode a large image?
<?php
// function to display the image
function display_img($imgcode,$type) {
header('Content-type: image/'.$type);
header('Content-length: '.strlen($imgcode));
echo base64_decode($imgcode);
}
$imgcode = file_get_contents("image.txt");
// show the image directly
display_img($imgcode,'jpg');
?>
Since base64-encoded data is cleanly separated every 4 bytes (i.e. 3 bytes of plaintext are encoded into 4 bytes of base64-encoded text), you could split your b64 string into multiples of 4 bytes, and process them separately:
while (not at end of string) {
take next 4096 bytes // for example - 4096 is 2^12, therefore a multiple of 4
// you could use much larger blocks, depends on your memory limits
base64-decode them
append the decoded result to a file, or a string, or send it to the output
}
If you have a valid base64 string, this will work identically to decoding it all at once.
OK, here is a closer resolution. While this seems to decode the base64 data in smaller chunks, I still don't get an image in the browser. If I echo the data before I place a header, I get output. Again, this works with a small image but not a large one. Thoughts?
<?php
// function to display the image
function display_img($file,$type) {
$src = fopen($file, 'r');
$data = "";
while(!feof($src)) {
$data .= base64_decode(fread($src, 4096));
}
$length = strlen($data);
header('Content-type: image/'.$type);
header('Content-length: '.$length);
echo $data;
}
// show the image directly
display_img('image.txt','jpg');
?>
Content-length must specify the actual (decoded) content length not the length of the base64 encoded data.
Though I'm not sure that fixing it would solve this problem...
Save the base64 string to an image file using imagejpeg() or the correct function for the different formats, and then display the image with a simple <img> tag.

Categories