php提示Fatal error: Call to undefined function imagecreate()

 更新时间:2016年11月25日 17:37  点击:1563
在php中imagecreate函数是一个图形处理函数,主要用于新建一个基于调色板的图像了,然后在这个基础上我们可以创建一些图形数字字符之类的,但这个函数需要GD库支持,如果没有开启GD库使用时会提示Call to undefined function imagecreate()错误。

例,我在测试一个简单生成图形时实例

Example #1 新建一个新的 GD 图像流并输出图像

 代码如下 复制代码

<?php
header("Content-type: image/png");
$im = @imagecreate(100, 50)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

运行机制时提示

Fatal error: Call to undefined function imagecreate() .....

后面突然想起没打开gd库了

这一问题是:GD库没有正确安装或配置,解决方案:

在php.ini中找到
;extension=php_gd2.dll
去掉前边的;
然后将php目录中的ext下的php_gd2.dll拷入c:/windows/system32和c:/windows目录下,重启IIS或者apache就OK了.

centos中gd库开启

具体操作只有2个命令如下:
运行在线安装带GD库的PHP的命令:

 代码如下 复制代码
yum -y install php-gd

重新启动apachce服务以使安装后的GD库生效

 代码如下 复制代码
/etc/rc.d/init.d/httpd restart(service httpd restart)

gd的安装路径

/etc/php.d/gd.ini                 <---这个是让php支持gd的哦,这个是CentOS的特色之一,不用把全部东西都写在php.ini这个文件里,只是需要把*.ini文件写在/etc/php.d/文件夹就可以了,系统会自动把这个目录下的*.ini读入php.ini
/usr/lib/php/modules/gd.so   <----这个就是gd的文件啦

关于需要使用GD库的图形处理函数还有如下

gd_​info
 
getimagesize
 
getimagesizefromstring
 
image_​type_​to_​extension
 
image_​type_​to_​mime_​type
 
image2wbmp
 
imageaffine
 
imageaffinematrixconcat
 
imageaffinematrixget
 
imagealphablending
 
imageantialias
 
imagearc
 
imagechar
 
imagecharup
 
imagecolorallocate
 
imagecolorallocatealpha
 
imagecolorat
 
imagecolorclosest
 
imagecolorclosestalpha
 
imagecolorclosesthwb
 
imagecolordeallocate
 
imagecolorexact
 
imagecolorexactalpha
 
imagecolormatch
 
imagecolorresolve
 
imagecolorresolvealpha
 
imagecolorset
 
imagecolorsforindex
 
imagecolorstotal
 
imagecolortransparent
 
imageconvolution
 
imagecopy
 
imagecopymerge
 
imagecopymergegray
 
imagecopyresampled
 
imagecopyresized
 
» imagecreate
 
imagecreatefromgd2
 
imagecreatefromgd2part
 
imagecreatefromgd
 
imagecreatefromgif
 
imagecreatefromjpeg
 
imagecreatefrompng
 
imagecreatefromstring
 
imagecreatefromwbmp
 
imagecreatefromwebp
 
imagecreatefromxbm
 
imagecreatefromxpm
 
imagecreatetruecolor
 
imagecrop
 
imagecropauto
 
imagedashedline
 
imagedestroy
 
imageellipse
 
imagefill
 
imagefilledarc
 
imagefilledellipse
 
imagefilledpolygon
 
imagefilledrectangle
 
imagefilltoborder
 
imagefilter
 
imageflip
 
imagefontheight
 
imagefontwidth
 
imageftbbox
 
imagefttext
 
imagegammacorrect
 
imagegd2
 
imagegd
 
imagegif
 
imagegrabscreen
 
imagegrabwindow
 
imageinterlace
 
imageistruecolor
 
imagejpeg
 
imagelayereffect
 
imageline
 
imageloadfont
 
imagepalettecopy
 
imagepalettetotruecolor
 
imagepng
 
imagepolygon
 
imagepsbbox
 
imagepsencodefont
 
imagepsextendfont
 
imagepsfreefont
 
imagepsloadfont
 
imagepsslantfont
 
imagepstext
 
imagerectangle
 
imagerotate
 
imagesavealpha
 
imagescale
 
imagesetbrush
 
imagesetinterpolation
 
imagesetpixel
 
imagesetstyle
 
imagesetthickness
 
imagesettile
 
imagestring
 
imagestringup
 
imagesx
 
imagesy
 
imagetruecolortopalette
 
imagettfbbox
 
imagettftext
 
imagetypes
 
imagewbmp
 
imagewebp
 
imagexbm
 
iptcembed
 
iptcparse
 
jpeg2wbmp
 
png2wbmp

今天在利用empty()函数判断一个变量是否为null是发现提示Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work:empty(trim($name)).错误了,后整一半天找到问题所在了。


你使用empty检查一个函数返回的结果时会报如下的一个致命错误:

Fatal error: Can't use function return value in write context in : ..................

例如:

echo empty(yourfunction(xx, oo));

到PHP手册里面查看,在empty函数描述的地方有以下文字:

Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work:empty(trim($name)).

empty() 只检测变量,检测任何非变量的东西都将导致解析错误!

因此,我们不能拿empty来直接检测函数返回的值,需要先把函数的返回值赋给某个变量,然后去用empty检测这个变量。

所以,我们可以写成如下的形式:

$return= yourfunction(xx, oo);

echo empty(return);

Curl函数在php中一个默认未开启的组件,如果你需要使用Curl我们需要在php.ini中把extension=php_curl.dll前面的;去掉即可。

我先是把php.ini中的extension=php_curl.dll前面的;去掉了,并且重启了apache环境,但调用如下函数时提示

 代码如下 复制代码

<?php
// 初始化一个cURL对象
$curl = curl_init();
// 设置您需要抓取的URL
curl_setopt($curl, CURLOPT_URL, 'http://www.111cn.net'');
// 设置header
curl_setopt($curl, CURLOPT_HEADER, 1);
// 设置cURL参数,要求结果保存到字符串中还是输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 运行cURL,请求网页
$data = curl_exec($curl);
// 关闭URL请求
curl_close($curl);
// 显示获得的数据
var_dump($data);
?>

错误提示

Call to undefined function curl_init()

看了相关文章我又把将PHP的ext目录下的三个文件:php_curl.dll、libeay32.dll和ssleay32.dll复制到系统system32目录中,但还是不行,于是再接google发现win7(x64)有一点区别了自带的php_curl.dll文件在64位windows7环境下并不能运作

具体解决步骤:

1、单击通知栏wamp图标,在php-ext里找到php_curl选项,单击选中,等待wamp重启服务完成。

2、下载修复过的php_curl.dll(php5.X/X64位系统)文件,放置到wampbinphpphp(版本号)ext文件夹下。

3、重新启动wamp服务即可。

提示

找了这么久原来是我们安装的php环境与系统版本不同,我们最好是多少位系统选择多少位的php版本哦。

memory_limit是php.ini中的一个比较重要的参数了,它可以限制你php进行最大使用内存的情况,如果小了会导致运行时提示Fatal Error: Allowed memory size of xxxxxx bytes exhausted错误了,下面我们一起来看看解决办法。


编辑php.ini 在php.ini中,找到“memory_limit”这一项,如果没有,你可以在文件的尾部自己增加这个参数。以下是一些设置范例

memory_limit = 128M ; 可以将128M改为任何你想设置的值

保存文件

重启php web 服务器  web服务器使用Apache, 则执行:

httpd restart

你可能不被允许私修改php.ini。那么,你可以需要考虑用其他方法来增加memory_limit的值。

修改memory_limit大小方法2: .htAccess

说明: 这种方法只有在php以Apache模块来执行时才生效。 在你的网站的根目录下找到“.htaccess”文件,如果没有,可以自己创建一个。然后把以下配置放入其中

php_value memory_limit 128M ; 可以将128M改为任何你想设置的值

修改memory_limit大小方法3: 运行时修改php的内存设置

在你的php代码中增加以下命令行即可。

ini_set(‘memory_limit’,’128M’);

在运行dedecms是有些朋友会发现有些页面会提示Fatal error: Allowed memory size of 134217728 bytes exhauste错误了,下面我们一起来看看解决此问题办法。

报错提示:Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 38218371 bytes) in .....

解决方法:

1.取消PHP的内存限制。
在php程序中添加 ini_set("memory_limit","-1");
2.根据自己的需要及参考本机的内存大小修改php内存限制,如改为1024M。
在php程序中添加 ini_set("memory_limit","1024M"); 或者将php.ini中相应位置改为memory_limit = 1024M;

内存限制的意义


php中的相关文档解释 memory_limit 如下:

memory_limit: integer

该指令设定了一个脚本 所能够申请到的最大内存字节数。这有助于防止写得不好的脚本消耗光服务器上的可用内存。要使用此指令必须在编译的时候激活。因此 configure 一行中应该包括:--enable-memory-limit。如果不需要任何内存上的限制,必须将其设为 -1。 从 php 4.3.2 起,当激活了 memory_limit,PHP 函数 memory_get_usage() 便可以使用了。也就是说,php在一个 page 中的处理内存限制,默认的(/etc/php.ini)定义为 128M (我的系统默认安装后),后来开发组的应用写的越来越复杂,但是在结构上可能还有些欠缺,频繁的对象请求,居然造成了内存不足。

应用层面测试与解决方法

最好的方式应该在应用层面解决,而不是不断的增加内存设置。以下为代码测试:

 代码如下 复制代码

<?
printf(" total run: %.2f s<br>".
"memory usage: %.2f M<br> ",
microtime(true)-$HeaderTime,
memory_get_usage() / 1024 / 1024 );
?>

运行结果显示如下:

total runtime: 1.47 s

memory usage: 77.09 M


一个页面居然会有77M的请求。究其原因,是程序员在编码时,仅仅对变量赋值,却从来没有 unset ($var) 过。试想,如果一个页面请求要处理20个sql查询,每个sql查询返回10个sql结果,而程序员从来都不关心是返回一个row的所有column还是仅仅返回需要的column(实际上当我们采用更common的中间层时,往往会返回全部的column而不是特定的某几个字段,就像在 ORM 中如NHibernate, JBOSS中的那样)如果一条row有10K, 那么这个页面到处理结束时就要增加到 10K*10*20=2M的数组分配,这还不算有时候我们需要最数组进行复制。

所以在php中,合理的方法是变量使用后就 unset($var),最大限度节省内存资源。

经验之谈

本人有一台服务器使用的是apaceh2.3与php5.2.6上面的方法无效,经官方确认是php版本bug了,我们可以换个高版本手php版本即可解决。

[!--infotagslink--]

相关文章