I am trying to add following HTML table to an image and then download it as an .jpg file but instead it giving me white image which is given in $dir
. Please have a look and guide what i am missing or what is wrong with this code
$dir = "../images/white.jpg";
$text = "tableHTML";
imagettftext($dir, 20, 0, 10, 20, $text);
imagepng($dir);
//$name = './img/ok.png';
header('Pragma: public');
header('Cache-Control: public, no-cache');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($dir));
header('Content-Disposition: attachment; filename="' . basename($dir) . '"');
header('Content-Transfer-Encoding: binary');
readfile($dir);
the output image.jpg would be like this
Sr # | ID | Bar Code | Notes
1 | 1261 | |||||||| | Test
2 | 6781 | |||||||| | test
3 | 8895 | |||||||| | test
4 | 5578 | |||||||| | test
5 | 1123 | |||||||| | test
*test data
You're missing one parameter. Following the documentation -> http://php.net/manual/en/function.imagettftext.php
imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
you're using
imagettftext((image)$dir, (size)20, (angle)0, (x)10, (t)20, (color)$text);
maybe you're missing the color before the text ;)
First of all you have a jpg file and you are using imagepng()
function.
Your code could have been like ---
$file = 'images/white.jpg';
$text = "tableHTML";
header("Content-type: image/jpeg");
$file = ImageCreateFromJPEG($file);
$font = 'YOUR_FONT_FILE_PATH';
$black = ImageColorAllocate($file, 255, 255, 255);
Imagettftext($file, 20, 0, 10, 20, $black, $font, "text to write");
imagejpeg($file);