Reputation: 31
I'm using the following function that I slightly tweaked from the php site a user posted.
function createImgText ($string=NULL, $fontsize=0, $marginX=0, $imgH=0, $fontfile=NULL, $imgColorHex=NULL, $txtColorHex=NULL){
if($string != ""){
//header("Content-type: image/png");
//
$spacing = 0;
$line = array("linespacing" => $spacing);
if (file_exists($fontfile)) $box = @imageftbbox($fontsize,0,$fontfile,$string,$line) or die('Box command error');
else die("ERROR - font");
$tw=$box[4]-$box[0]; //image width
$marginY = $imgH - (($imgH - $fontsize) / 2);
$imgWidth = $tw + (2*$marginX);
$im = ImageCreate($imgWidth, $imgH);
$int = hexdec($imgColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$black = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
$int = hexdec($txtColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$white = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
ImageFtText($im, $fontsize, 0, $marginX, $marginY, $white, $fontfile, $string, array());
ImagePng($im);
ImageDestroy($im);
}else{
echo "ERROR - no string";
}
}
I'm using the following in my index.php
error_reporting(-1);
ini_set('display_errors', true);
and the page that outputs the image.
I'm not receiving any errors. :( However, uncommenting the header portion causes Firefox to say "The image contains errors and will not be displayed".
Image php file:
<?php
error_reporting(-1);
ini_set('display_errors', true);
$code = $sys->core->generateRandomString($sys->settings['captchal']);
echo 'Debug: Using string: ' . $code . ' font: fonts/' . $sys->settings['captchaf'] . '<br>';
$sys->core->createImgText($code, 9, 10, 18, 'fonts/' . $sys->settings['captchaf'], "000000", "FFFFFF");
?>
It says:
Debug: Using string: nrcujP font: fonts/airstream.ttf
�PNG IHDR0�H@�PLTE������������???___uV#�pIDAT�c` �E�D�x���*�s�bP �@fR�SVV``�F�0t4UTu6uuh3rl@�+4h3k6 �),6F�!�P�LHPTD8� �c�p�,�8�( �8�Q+|�Q��IEND�B`�
I have a page with
<?php echo phpinfo(); ?>
it says GD is enabled and all addons, including TTF support. Anyone ever have a similar problem before? I've tried other things, and it seems to fail on the second ImageColorAllocate. I don't see any mention of it on google when I search. It's suppose to render an image when called then destroy itself, not take up file space.
Problem solved with the help of NXT. I had a whitespace before
<?php
that I had overlooked.
Upvotes: 3
Views: 1371
Reputation: 2011
I used your code with slight changes and it works perfectly.
My changes were only:
If you still have problems, make sure you only output image contents and open the script URL directly in any available text editor to see the source code. Also check that you don't have any leading and trailing spaces in the output.
function createImgText($string=NULL, $fontsize=0, $marginX=0, $imgH=0, $fontfile=NULL, $imgColorHex=NULL, $txtColorHex=NULL){
if($string != ""){
header("Content-type: image/png");
$spacing = 0;
$line = array("linespacing" => $spacing);
if (file_exists($fontfile)) $box = @imageftbbox($fontsize,0,$fontfile,$string,$line) or die('Box command error');
else die("ERROR - font");
$tw=$box[4]-$box[0]; //image width
$marginY = $imgH - (($imgH - $fontsize) / 2);
$imgWidth = $tw + (2*$marginX);
$im = ImageCreate($imgWidth, $imgH);
$int = hexdec($imgColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$black = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
$int = hexdec($txtColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$white = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
ImageFtText($im, $fontsize, 0, $marginX, $marginY, $white, $fontfile, $string, array());
ImagePng($im);
ImageDestroy($im);
}else{
echo "ERROR - no string";
}
}
error_reporting(-1);
ini_set('display_errors', true);
$code = 'abcdef';
createImgText($code, 9, 10, 18, 'MyFont.ttf', "000000", "FFFFFF");
Upvotes: 1
Reputation: 639
imagepng($im,"new.png",0);
imagedestroy($im);
try this i hope it will work........ https://stackoverflow.com/questions/13619834/upload-png-image-with-transparency-code-but-it-not-working-and-also-showing-the/13621103#13621103->this link matchs your questions.
Upvotes: 1