admin管理员组文章数量:1621657
用imagejpeg()画不出图片,不是乱码就是红叉.
1.输出结果: 乱码
代码 < html >< head >
</ head >
< body >
<? php
header ( " content-type: image/jpeg " ); // 设置http头部的类型
for ( $i = 0 ; $i < 4 ; $i ++ )
{
$rand .= dechex ( rand ( 1 , 15 )); // 生成随机数
}
$image = imagecreatetruecolor( 100 , 30 ); // 生成图片
$color = imagecolorallocate( $image , 255 , 255 , 255 );
imagestring( $image , 6 , rand ( 0 , 65 ) , rand ( 0 , 15 ) , $rand , $color ); // 把随机数画进图片
imagejpeg( $image );
?>
</ body >
</ html >
原因: http头部的content-type已经被设置为image/jpeg,所以不支持html代码了.
解决方法: 把html的代码去掉.
2.输出结果: 红叉
代码<? php
header ( " content-type: image/jpeg " );
for ( $i = 0 ; $i < 4 ; $i ++ )
{
$rand .= dechex ( rand ( 1 , 15 )); // 生成随机数
}
$image = imagecreatetruecolor( 100 , 30 ); // 生成图片
$color = imagecolorallocate( $image , 255 , 255 , 255 );
imagestring( $image , 6 , rand ( 0 , 65 ) , rand ( 0 , 15 ) , $rand , $color ); // 把随机数画进图片
imagejpeg( $image );
?>
原因: "<?php"前面不能有字符,这里"<?php"上面有一行空行('\n')
解决方法: 去掉"<?php"上面的空行.
注意: "<?php"前面是不能有字符,所以即使不是空行而是空格,也会出红叉.
3.输出结果: 红叉
代码 <? phpheader ( " content-type: image/jpeg " );
for ( $i = 0 ; $i < 4 ; $i ++ )
{
$rand .= dechex ( rand ( 1 , 15 )); // 生成随机数
}
$image = imagecreatetruecolor( 100 , 30 ); // 生成图片
$color = imagecolorallocate( $image , 255 , 255 , 255 );
imagestring( $image , 6 , rand ( 0 , 65 ) , rand ( 0 , 15 ) , $rand , $color ); // 把随机数画进图片
imagejpeg( $image );
?>
原因: 被Notice消息扰乱了,这里$rand没有被预先定义就使用,虽然PHP允许,但会出Notice消息提示.
本来Notice消息应该是输出到页面的,但是这里content-type被设置为image/jpeg,所以显示不出来.
样例: 运行以下代码会得到"Notice: Undefined variable: rand in E:\Website\test.php on line 3"
<? phpfor ( $i = 0 ; $i < 4 ; $i ++ )
$rand .= dechex ( rand ( 1 , 15 ));
echo ( $rand );
?>
解决方法: 在开头加一句"error_reporting(E_ALL ^ E_NOTICE);"(取消Notice提示)或者"$rand=NULL;"(预先定义变量)
4.最后得到运行通过的代码:
代码 <? phpheader ( " content-type: image/jpeg " );
// error_reporting(E_ALL ^ E_NOTICE); //取消Notice消息提示
$rand = null ; //预先 定义了$rand变量就不会被Notice提示了
for ( $i = 0 ; $i < 4 ; $i ++ )
{
$rand .= dechex ( rand ( 1 , 15 )); // 生成随机数
}
$image = imagecreatetruecolor( 100 , 30 ); // 生成图片
$color = imagecolorallocate( $image , 255 , 255 , 255 );
imagestring( $image , 6 , rand ( 0 , 65 ) , rand ( 0 , 15 ) , $rand , $color ); // 把随机数画进图片
imagejpeg( $image );
?>
转载于:https://wwwblogs/live41/archive/2010/07/02/1769754.html
版权声明:本文标题:imagejpeg()输出图片失败的原因与解决方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1728850098a1176565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论