我使用Imagemagick制作了一个自定义验证码,它停止了工作

Im not sure what the issue is, it was working fine before I moved my files to a new directory now it doesn't seem to want to load/create the image.

<?php
session_start();
header('Content-type: image/png');

$text = $_SESSION['secure'];
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#444444');

$image->newImage(320, 40, new ImagickPixel('#0d0d0d'));
$image->setImageFormat('png');
$draw->setFont("fonts/UbuntuMono-B.ttf");
$draw->setFontSize(30);
$draw->setFillColor($color);
$image->annotateImage($draw, 100, 30, 0, $text);

$image->sketchImage (1, 10, 0);

echo $image;

?>

Not entirely sure how Imagick works, but are you sure you have the appropriate file permissions in the directory you moved it to?

Are you sure you didn't change something?

And lastly, if you put it back, does it still work as normal?

(I would reply to your question but I don't have the necessary reputation)

You can check for errors by adding this at the top of your file:

ini_set('display_errors',1); 
error_reporting(E_ALL);

or from the command line:

php -l filepath.php

Setting the owner of the folder to apache (if you're using apache) may solve your issue if it's permissions.

chown apache:apache filepath.php

Google has a thorough tutorial on the subject.

We need to generate a random string (text), store it in a session for checking on $_POST, create an image with the string, display the image on a form and then check it on submission.

Step 1. Create a random string and save it in a $_SESSION This step is very easy as PHP has all the functions we need. A quick and effective way of generating a random string is to use the rand function, md5 encypt it and then just extract the amount of characters you like.

CODE:

<?php
session_start(); // Very important.
$captcha = md5(uniqid(rand(), true)); // Generate our random string.
$captcha = substr($captcha,0,6);
$_SESSION['captchaCode'] = $captcha; // Save the captch code to a session.
echo $captcha; // echo a generated code for testing purposes
?>

Save the above contents to "captcha.php" and then see what it generates. Resources: http://www.php.net/md5, http://www.php.net/rand, http://www.php.net/substr, http://www.php.net/uniqid

Step 2. Creating an image from the generated string There is many different ways of doing this such as using GD library (gd libs), however I prefer doing image manipulation with ImageMagick - personal choice.

Please note: ImageMagick and GD Libs do not come with PHP by default, please check they are installed on your server.

First we need background images (attached to this thread - please store them in images/ folder) - "bg1.png", "bg2.png" and "bg3.png" each time the image is generated it will randomly choose a background image. We store the filenames in an easy to access array. When using ImageMagick you should upload the desired font to use, this means on different servers it will always be able to use your font.

So we now have the following code. CODE:

<?php
session_start(); // Very important.
$captcha = md5(uniqid(rand(), true)); // Generate our random string.
$captcha = substr($captcha,0,6);
$_SESSION['captchaCode'] = $captcha; // Save the captch code to a session.
//echo $captcha; // echo a generated code for testing purposes

$imageDirectory = 'images/';  // The relative or absolute path to where images are stored.

$bgImages = array('bg1.png', 'bg2.png', 'bg3.png');

$backgroundImage = $imageDirectory.$bgImages[rand(0, count($bgImages)-1)]; // This chooses an image.

?>

Now comes the complicated part, actually putting it all together - but don't worry I will comment everything as much as I can.

CODE:

<?php
session_start(); // Very important.
$captcha = md5(uniqid(rand(), true)); // Generate our random string.
$captcha = substr($captcha,0,6);
$_SESSION['captchaCode'] = $captcha; // Save the captch code to a session.
//echo $captcha; // echo a generated code for testing purposes

$imageDirectory = 'images/';  // The relative or absolute path to where images are stored.

$bgImages = array('bg1.png', 'bg2.png', 'bg3.png');

$backgroundImage = $imageDirectory.$bgImages[rand(0, count($bgImages)-1)]; // This chooses an image.

$tmpFilename = $captcha . '.png'; // Use the captcha code to create a random filename - Please note this filename is not shown in the user's web browser.
$imageTmpDirectory = 'images/tmp/'; //Directory to store all tmp generated images.

$font = 'arial.ttf'; // The chosen font - this font sits in the same folder as this script.
$FontSize = rand(24, 36); // Random select a font size between 24 and 36.
$hexValues = array('0','1','2','3','4'); // Hex values, we always want dark colours hence 0 to 5.
$numHex = count($hexValues); // Count how many hex values.
$GeneratedHex = ''; // Set the variable.

for ($i = 0; $i < 6; $i++) {
$GeneratedHex .= $hexValues[rand(0, $numHex-1)]; // Generate the end hex colour.
}

$gravities = array('West', 'Center', 'East'); // ImageMagicks gravity function.
$gravity = $gravities[rand(0, count($gravities)-1)]; // Choose a gravity.

$angle = rand(-5, 5); // Generate an angle for the text.

$cmd  = '/usr/bin/convert'; // Path to ImageMagick on the server.
$cmd .= ' -font '.$font; // The generated colour.
$cmd .= ' -fill "#'.$GeneratedHex.'"'; // The generated colour.
$cmd .= ' -pointsize '.$FontSize ; // The size.
$cmd .= ' -gravity "'.$gravity.'"'; // Select generated gravity.
$cmd .= ' -draw \'text 0,0 "'.$captcha.'"\''; // Draw the captcha.
$cmd .= ' -rotate '.$angle; // Rotate the text to the generated angle.
$cmd .= ' ./'.$backgroundImage.' ./'.$imageTmpDirectory.$tmpFilename; // Assign background image and then save output.

exec($cmd); // Run the command!

header('Content-Type: image/png');
print fread(fopen($imageTmpDirectory.$tmpFilename, 'r'), filesize($imageTmpDirectory.$tmpFilename));

unlink($imageTmpDirectory.$tmpFilename); // Delete the tmp image.

?>

When you run the above code you should see something like this: Image

Step 3. Using the captcha code in a form. Well the hard part is now over! It just gets easier from here!

All we need to do is load the captcha script in an img tag, for example

Here is the form page. CODE:

<?php
session_start(); // Very important to be the first statement. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="capturePost.php" method="post">
<?php
if(!empty($_SESSION['ERROR'])) { // We have an error from capturePost.php
   echo '<p>' . $_SESSION['ERROR'] . '</p>'; // Display error.
   unset($_SESSION['ERROR']); // Clear error.
}?>

<img src="captcha.php" alt="Captcha Code"><br />

Please enter the security code <input type="text" name="captcha">
<br /><input type="submit">
</form>
</body>
</html>

Here is the submission page (capturePost.php) CODE:

<?php
session_start();

if(!empty($_POST['captcha'])) {
   if($_POST['captcha'] != $_SESSION['captchaCode']) { // see if the code is incorrect.

      $_SESSION['ERROR'] = '<strong>Code did not match, please try again.</stong>'; // Our error message.
      header('Location: ' . $_SERVER['HTTP_REFERER']); // Take them back to form page.
   } else {
      echo 'Well done, you entered the correct code';    // Its correct!

      // DO FORM PROCESSING HERE
   }

}
?>

I really hope this helps.