3

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.

5
  • Is this meant to produce a random captcha image? Commented Mar 14, 2013 at 18:58
  • Yes sir, it is. I'm writing it all from scratch. Commented Mar 14, 2013 at 19:03
  • I haven't wrote in database stuff yet, this it just demo code to show a random image. Once I get over this hump of actually displaying a random generated image from text I would write in security things. Commented Mar 14, 2013 at 19:10
  • if my post is usefull please vote me.... Commented Mar 14, 2013 at 19:39
  • It wasn't useful to me. Might be of use to someone else. Commented Mar 14, 2013 at 21:22

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

3 Comments

No, it doesn't match my question. Though that code did work, it's saving locally an image file, which was not my desired output, it's suppose to render an image when called then destroy itself, not take up file space.
I added it to my question that it's not suppose to take up space, since apparently how I structured my code wasn't blatantly as obvious as I thought.
Since your code actually helped me determine if I had a faulty GD install or not. I'll vote you up, even if it wasn't your intention that I use it for that. lol
1

I used your code with slight changes and it works perfectly.

My changes were only:

  • replacing the font name with some other .ttf I had around
  • hardcoded the captcha text
  • removed the line with debug text (it can't be there as it destroys the image)
  • adjusted the function calls
  • uncommented the header() line

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

3 Comments

Thanks for the help. I made my framework without any white-spaces in the head of the documents, I made sure of that, and that nothing "extra" is outputted, but apparently the file(captcha output file had a space before <?php, so I goofed it. :) Thank you. As for the comment outs and debug line, obviously that was there to debug, haha.
I would vote you up, but apparently I don't have enough rep. But yeah, sometimes you need someone to test the code on their own server/php installation, see what happens, and if it works, obviously it's something I did wrong. I've had the white-space problem before, and I thought I was getting redundant at not doing that, but I guess it doesn't hurt to check the dumbest of things at times.
@LmOtaku No problem, glad I could help :) I found the best way to debug images it opening them with a text editor. And welcome to StackOverflow ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.