So I have a PHP program where I need to output the image as a data URI for inclusion in an <img> tag. Here's the relevant code:
... ob_start(); imagepng($img); $data = 'data:image/png;base64,' . base64_encode(ob_get_contents()); ob_end_clean(); ...
$img has already been generated.
The code runs and outputs the data URI, but it seems the URI is invalid - the image displays as the broken image icon. Can anybody help?
Offline
jvvg wrote:
Are you sure it's a valid image?
Just checked, and it seems imagepng isn't working properly. imagegif and imagejpeg work, but imagepng won't even output a valid image straight off. However, the image output by the other two functions is just a black rectangle. Is there something wrong with my code, or the image library?
$img = imagecreatetruecolor(100, 20); $back = imagecolorallocate($image, 255, 255, 255); imagefill($img, 0, 0, $back); $col = imagecolorallocate($image, 255, 0, 0); imagestring($img, 1, 0, 0, 'test', $col); ob_start(); imagegif($img); $data = 'data:image/gif;base64,' . base64_encode(ob_get_contents()); ob_end_clean(); imagedestroy($img); return $data;
Offline
TheSuccessor wrote:
jvvg wrote:
Are you sure it's a valid image?
Just checked, and it seems imagepng isn't working properly. imagegif and imagejpeg work, but imagepng won't even output a valid image straight off. However, the image output by the other two functions is just a black rectangle. Is there something wrong with my code, or the image library?
Code:
$img = imagecreatetruecolor(100, 20); $back = imagecolorallocate($image, 255, 255, 255); imagefill($img, 0, 0, $back); $col = imagecolorallocate($image, 255, 0, 0); imagestring($img, 1, 0, 0, 'test', $col); ob_start(); imagegif($img); $data = 'data:image/gif;base64,' . base64_encode(ob_get_contents()); ob_end_clean(); imagedestroy($img); return $data;
Maybe it's just a problem with the server.
Offline
TheSuccessor wrote:
jvvg wrote:
Are you sure it's a valid image?
Just checked, and it seems imagepng isn't working properly. imagegif and imagejpeg work, but imagepng won't even output a valid image straight off. However, the image output by the other two functions is just a black rectangle. Is there something wrong with my code, or the image library?
Code:
$img = imagecreatetruecolor(100, 20); $back = imagecolorallocate($image, 255, 255, 255); imagefill($img, 0, 0, $back); $col = imagecolorallocate($image, 255, 0, 0); imagestring($img, 1, 0, 0, 'test', $col); ob_start(); imagegif($img); $data = 'data:image/gif;base64,' . base64_encode(ob_get_contents()); ob_end_clean(); imagedestroy($img); return $data;
Just found it. Take a closer look at line 2. You used $image instead of $img.
Offline
GeonoTRON2000 wrote:
Just found it. Take a closer look at line 2. You used $image instead of $img.
Derp
It works now. Thanks to everyone.
Offline