Jump to content

how to create image files with php?


WesleyA

Recommended Posts

hello

 

my programming is focused at the image processing library GD and FREE TYPE.

I tried , amongst others, the script at this page : http://code.tutsplus.com/tutorials/build-your-own-captcha-and-contact-form--net-5362

but I cant get it working.

 

I placed the true type font *.ttf file in the right directory but still get an error message saying: The image 'http://localhost/mydirectory/captcha.php' can't be displayed because it contains errors. (not sure this is the correct translation of the errormessage as it was displayed in my own language).

 

I tried many things. like placing it in another directory. I checked phpinfo() to see if GD was installed which it was.

 

Firefox gives an error message, IE and Chrome just dont show anything.

 

This is the code:

    <?php
    session_start();
 
    $string = '';
 
    for ($i = 0; $i < 5; $i++) {
    // this numbers refer to numbers of the ascii table (lower case)
    $string .= chr(rand(97, 122));
    }
 
    $_SESSION['rand_code'] = $string;
 
    // $dir = 'fonts/';
    $dir = 'http://localhost/mydir/fonts/';
 
    $image = imagecreatetruecolor(170, 60);
    $black = imagecolorallocate($image, 0, 0, 0);
    $color = imagecolorallocate($image, 200, 100, 90); // red
    $white = imagecolorallocate($image, 255, 255, 255);
 
    imagefilledrectangle($image,0,0,399,99,$white);

    // imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['rand_code']); 

     imagettftext ($image, 30, 0, 10, 40, $color, "arial.ttf", $_SESSION['rand_code']);
     header("Content-type: image/png");
     imagepng($image);
 
?>

What I want to know first is if I understand fully how it should be done. Maybe the tut I'm working with is not complete.

 

Online I found a few topics with the same error report, they solved it by saving it as another file format (not utf-8).

 

I would like to try that but dont know how. Notepad doesnt seem to have that feature of another file format like UTF-8 NO NOM.

 

And besides that: WHY is file format so important?

 

Please help !

Edited by WesleyA
Link to comment
Share on other sites

If you save the file as UTF with a BOM then the BOM is considered by PHP to be output, so it will be sent at the start of the file and the image data will have a BOM at the start, which will corrupt the rest of the image data. Anything outside of PHP tags gets treated as output by PHP, and the BOM is at the start of the file so it is outside of PHP tags. One editor which will let you save UTF without the BOM is Sublime Text.

 

When you're developing things like that you should use an error log. Add this to the top of your code:

 

ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');
error_reporting(E_ALL);
That will cause PHP to write all error messages to a file called error.log in the same directory as the PHP script. You can use that to check for PHP error messages when you run that code.
Link to comment
Share on other sites

  • 2 weeks later...

<?php
function LoadJpeg($imgname)
{
/* Attempt to open */
$im = @imagecreatefromjpeg($imgname);

/* See if it failed */
if(!$im)
{
/* Create a black image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}

return
$im;
}


header('Content-Type: image/jpeg');

$img = LoadJpeg('bogus.image');

imagejpeg($img);
imagedestroy($img);
?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...