windows PHP - ImageCreate() 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5274563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:24:43  来源:igfitidea点击:

PHP - ImageCreate() error

phpwindowsimage

提问by markzzz

I'm trying to making a Captcha code, and I read some tutorial online. So, I have copy/and paste this function :

我正在尝试制作验证码,并在线阅读了一些教程。所以,我有复制/粘贴这个功能:

create_image(); 
exit(); 

function create_image() { 
    //Let's generate a totally random string using md5 
    $md5_hash = md5(rand(0,999)); 
    //We don't need a 32 character long string so we trim it down to 5 
    $security_code = substr($md5_hash, 15, 5); 

    //Set the session to store the security code
    $_SESSION["security_code"] = $security_code;

    //Set the image width and height 
    $width = 100; 
    $height = 20;  

    //Create the image resource 
    $image = ImageCreate($width, $height);  

    //We are making three colors, white, black and gray 
    $white = ImageColorAllocate($image, 255, 255, 255); 
    $black = ImageColorAllocate($image, 0, 0, 0); 
    $grey = ImageColorAllocate($image, 204, 204, 204); 

    //Make the background black 
    ImageFill($image, 0, 0, $black); 

    //Add randomly generated string in white to the image
    ImageString($image, 3, 30, 3, $security_code, $white); 

    //Throw in some lines to make it a little bit harder for any bots to break 
    ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
    imageline($image, 0, $height/2, $width, $height/2, $grey); 
    imageline($image, $width/2, 0, $width/2, $height, $grey); 

    //Tell the browser what kind of file is come in 
    header("Content-Type: image/jpeg"); 

    //Output the newly created image in jpeg format 
    ImageJpeg($image); 

    //Free up resources
    ImageDestroy($image); 
}

But when I try to call it, I get this error :

但是当我尝试调用它时,出现此错误:

Fatal error: Call to undefined function ImageCreate() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\gtw\registration\createCaptcha.php on line 19

致命错误:在第 19 行调用 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\gtw\registration\createCaptcha.php 中未定义的函数 ImageCreate()

What's the problem? Some PHP lib?

有什么问题?一些 PHP 库?

采纳答案by Andrew Moore

The php_gd2extension isn't enabled.

php_gd2未启用扩展。

It is included with the Windows PHP distribution but you have to enable it in php.ini. To do this, simply open php.iniand uncomment (remove the leading ;) the following line:

它包含在 Windows PHP 发行版中,但您必须在php.ini. 为此,只需打开php.ini并取消注释(删除前导;)以下行:

extension=php_gd2.dll

Save, restart Apache, and you should be fine.

保存,重启 Apache,你应该没问题。

回答by Tramov

To be able to use ImageCreate you need to have the GD Library as part of your PHP installation.

为了能够使用 ImageCreate,您需要将 GD 库作为 PHP 安装的一部分。

Check with the phpinfo() function to see if it is installed. If not, you need to add this.

检查 phpinfo() 函数以查看它是否已安装。如果没有,你需要添加这个。

回答by Abuhafs

You are yet to Enable php_gd2 extension

您还没有启用 php_gd2 扩展

It is included with the Windows PHP distribution but you have to enable it in php.ini. To do this, simply open php.ini and uncomment (remove the leading ;) the following line:

它包含在 Windows PHP 发行版中,但您必须在 php.ini 中启用它。为此,只需打开 php.ini 并取消注释(删除前导 ;)以下行:

extension=php_gd2.dll Save, restart Apache, and you should be fine.

extension=php_gd2.dll 保存,重启Apache,应该就可以了。

to locate Php.ini, go to xampp folder

要找到 Php.ini,请转到 xampp 文件夹

open php folder open php.ini-development line 889 depending on the verision of your php It is included with the Windows PHP distribution but you have to enable it in php.ini. To do this, simply open php.ini and uncomment (remove the leading ;) the following line:

打开 php 文件夹 打开 php.ini-development line 889 取决于您的 php 版本 它包含在 Windows PHP 发行版中,但您必须在 php.ini 中启用它。为此,只需打开 php.ini 并取消注释(删除前导 ;)以下行:

extension=php_gd2.dll Save, restart Apache, and you should be fine.

extension=php_gd2.dll 保存,重启Apache,应该就可以了。