Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我用Imagmagick做了一个自定义验证码,它停止了工作_Php_Imagemagick_Captcha - Fatal编程技术网

Php 我用Imagmagick做了一个自定义验证码,它停止了工作

Php 我用Imagmagick做了一个自定义验证码,它停止了工作,php,imagemagick,captcha,Php,Imagemagick,Captcha,我不确定问题是什么,在我将文件移动到新目录之前,它工作正常,现在它似乎不想加载/创建映像 <?php session_start(); header('Content-type: image/png'); $text = $_SESSION['secure']; $image = new Imagick(); $draw = new ImagickDraw(); $color = new ImagickPixel('#444444'); $image->newImage(320,

我不确定问题是什么,在我将文件移动到新目录之前,它工作正常,现在它似乎不想加载/创建映像

<?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;

?>

不完全确定Imagick是如何工作的,但您确定您在将其移动到的目录中具有相应的文件权限吗

你确定你没有改变什么吗

最后,如果你把它放回去,它还能正常工作吗


(我会回答您的问题,但我没有必要的声誉)

您可以通过在文件顶部添加以下内容来检查错误:

ini_set('display_errors',1); 
error_reporting(E_ALL);
或从命令行:

php -l filepath.php
将文件夹的所有者设置为apache(如果您使用的是apache)可以解决您的问题,如果它具有权限的话

chown apache:apache filepath.php

我们需要生成一个随机字符串(文本),将其存储在会话中以检查$\u POST,使用该字符串创建一个图像,在表单上显示图像,然后在提交时检查它

第一步。创建一个随机字符串并将其保存在$\u会话中 这一步非常简单,因为PHP具有我们需要的所有函数。生成随机字符串的一种快速有效的方法是使用rand函数,md5对其进行加密,然后只提取所需的字符数

代码:


将上述内容保存到“captcha.php”中,然后查看生成的内容。 资源:

第二步。从生成的字符串创建图像 有很多不同的方法可以做到这一点,比如使用GD库(GD libs),但是我更喜欢使用ImageMagick进行图像处理——这是个人的选择

请注意:默认情况下,ImageMagick和GD LIB不随PHP提供,请检查它们是否安装在您的服务器上

首先,我们需要背景图像(附加到此线程-请将它们存储在图像/文件夹中)-“bg1.png”、“bg2.png”和“bg3.png”每次生成图像时,它都会随机选择一个背景图像。我们将文件名存储在一个易于访问的数组中。使用ImageMagick时,您应该上传所需的字体以供使用,这意味着在不同的服务器上,它将始终能够使用您的字体

现在我们有了下面的代码。 代码:


现在是复杂的部分,实际上是把它们放在一起——但别担心,我会尽可能多地评论一切

代码:


是$image->sketchImage(1,10,0)中的空格;在你的代码里?如果你在Linux上,你可以尝试“chown apache:apache folderpath”,看看它是否有权限。看起来像,但不知道为什么。我删除了它,它没有什么区别。同样,我使用的是共享网络主机,perm是755,因此它应该在编辑时间之外更精细;更改$text=$\u会话['secure'];给一个固定的文本一个可读的图像。发帖时的提示:尽量使用固定设置,不要依赖外部数据。正如我上面所说的,我有一个股票托管帐户,我没有访问命令行或类似命令行的权限。哪个托管公司?您可能仍然需要设置文件夹的权限。我使用versobit,我可以通过cpanel的文件管理器更改权限,它设置为755,这是它应该需要的全部权限
<?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
?>
<?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.

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

?>
<?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>
<?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
   }

}
?>