我的站长站在帝国CMS7.5后台上传一张图片给他自动裁剪时,返回的图片结果总是失效的图片,图片入库成功了,但是目录里面没有,而且没有任何错误提示。

我的站长站一开始以为是程序哪里出错了,检查了代码,检查了目录权限,最后还去检查了php配置和GD库是否安装正常。忙了几天最后发现是图片的问题,图片可以正常显示,也可以正常上传,但是图片在ResizeImage函数裁剪的时候就失效,也不报错。
最后用图片编辑器打开这张图片失败,才发现是图片的问题。可能这张图片经过压缩或者特殊处理过,可以正常显示图片,但是GD库读取不到,所以在裁剪的时候ResizeImage函数读取失败,返回的结果就是入库成功,目录里却没有文件。
我的站长站找到了帝国CMS7.5裁剪图片的ResizeImage函数,发现他没有任何错误提示,所以在发生错误时,根本不知道是哪里的原因。我的站长站为了避免以后还会出现这种问题,就给ResizeImage函数加上了错误提示。
ResizeImage函数优化版
文件在:\e\class\gd.php
function ResizeImage($big_image_name, $new_name, $max_width = 400, $max_height = 400, $resize = 1){
$returnr['file']='';
$returnr['filetype']='';
// 1. 检查文件是否存在
if (!file_exists($big_image_name)) {
// 弹窗提示 + 回到上一页(提交页) + 终止函数执行
echo "<script>alert('图片文件不存在!'); history.back(); </script>";
return $returnr; // 终止函数,不终止页面脚本
}
// 获取图片类型
if($temp_img_type = @getimagesize($big_image_name)) {
preg_match('/\/([a-z]+)$/i', $temp_img_type['mime'], $tpn);
$img_type = $tpn[1];
} else {
preg_match('/\.([a-z]+)$/i', $big_image_name, $tpn);
$img_type = $tpn[1];
}
$all_type = array(
"jpg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"),
"gif" => array("create"=>"ImageCreateFromGIF" , "output"=>"imagegif" , "exn"=>".gif"),
"jpeg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"),
"png" => array("create"=>"imagecreatefrompng" , "output"=>"imagepng" , "exn"=>".png"),
"wbmp" => array("create"=>"imagecreatefromwbmp", "output"=>"image2wbmp" , "exn"=>".wbmp")
);
$func_create = $all_type[$img_type]['create'];
if(empty($func_create) or !function_exists($func_create))
{
// 格式不支持:弹窗 + 返回提交页
echo "<script>alert('不支持该图片格式!'); history.back(); </script>";
return $returnr; // 终止函数
}
// 输出函数配置
$func_output = $all_type[$img_type]['output'];
$func_exname = $all_type[$img_type]['exn'];
if(($func_exname=='.gif'||$func_exname=='.png'||$func_exname=='.wbmp')&&!function_exists($func_output))
{
$func_output='imagejpeg';
$func_exname='.jpg';
}
// 2. 核心检测:GD库创建图片资源(解析图片)
$big_image = $func_create($big_image_name);
// 判断GD库解析图片是否失败(返回false)
if ($big_image === false) {
// 解析失败:弹窗 + 返回提交页
echo "<script>alert('GD库解析图片失败!可能是图片损坏、格式错误或GD库未正确安装。'); history.back(); </script>";
return $returnr; // 终止函数,不终止页面
}
$big_width = imagesx($big_image);
$big_height = imagesy($big_image);
// 图片尺寸未超过限制,直接保存
if($big_width <= $max_width and $big_height <= $max_height)
{
$func_output($big_image, $new_name.$func_exname);
$returnr['file']=$new_name.$func_exname;
$returnr['filetype']=$func_exname;
// 释放资源
ImageDestroy($big_image);
return $returnr;
}
// 计算缩放比例和裁剪参数
$ratiow = $max_width / $big_width;
$ratioh = $max_height / $big_height;
if($resize == 1) {
if($big_width >= $max_width and $big_height >= $max_height)
{
if($big_width > $big_height)
{
$tempx = $max_width / $ratioh;
$tempy = $big_height;
$srcX = ($big_width - $tempx) / 2;
$srcY = 0;
} else {
$tempy = $max_height / $ratiow;
$tempx = $big_width;
$srcY = ($big_height - $tempy) / 2;
$srcX = 0;
}
} else {
if($big_width > $big_height)
{
$tempx = $max_width;
$tempy = $big_height;
$srcX = ($big_width - $tempx) / 2;
$srcY = 0;
} else {
$tempy = $max_height;
$tempx = $big_width;
$srcY = ($big_height - $tempy) / 2;
$srcX = 0;
}
}
} else {
$srcX = 0;
$srcY = 0;
$tempx = $big_width;
$tempy = $big_height;
}
$new_width = ($ratiow > 1) ? $big_width : $max_width;
$new_height = ($ratioh > 1) ? $big_height : $max_height;
// 缩放图片
if(function_exists("imagecopyresampled"))
{
$temp_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
} else {
$temp_image = imagecreate($new_width, $new_height);
imagecopyresized($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
}
// 保存缩放后的图片
$func_output($temp_image, $new_name.$func_exname);
// 释放资源
ImageDestroy($big_image);
ImageDestroy($temp_image);
$returnr['file']=$new_name.$func_exname;
$returnr['filetype']=$func_exname;
return $returnr;
}主要增加了文件不存在提示、GD库读取文件失败提示、当发现错误的时候就停止运行后面的代码,避免生成错误失效图片。
最后还加水印的错误提示优化版代码如下:
function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000",$myfontpath="../data/mask/cour.ttf",$w_pct,$w_quality){
global $fun_r,$editor;
if($editor==1){$a='../';}
elseif($editor==2){$a='../../';}
elseif($editor==3){$a='../../../';}
else{$a='';}
$waterImage=$waterImage?$a.$waterImage:'';
$myfontpath=$myfontpath?$a.$myfontpath:'';
$isWaterImage = FALSE;
// ========== 兼容PHP5.6:替换??为空判断 ==========
$formatMsg = isset($fun_r['synotdotype']) ? $fun_r['synotdotype'] : '不支持的图片格式!';
// ========== GD库基础检测 ==========
if (!function_exists('imagecreatefromjpeg') || !function_exists('imagecopymerge')) {
echo "<script>alert('服务器未启用GD库或GD库功能不全,无法添加水印!'); history.back(); </script>";
return "";
}
// 读取水印文件
if(!empty($waterImage) && file_exists($waterImage))
{
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
// 检测getimagesize解析水印图片失败
if (!$water_info) {
echo "<script>alert('水印图片解析失败!可能是文件损坏或格式错误。'); history.back(); </script>";
return "";
}
$water_w = $water_info[0];//取得水印图片的宽
$water_h = $water_info[1];//取得水印图片的高
// 检测GD库创建水印图片资源失败
switch($water_info[2])//取得水印图片的格式
{
case 1:$water_im = imagecreatefromgif($waterImage);break;
case 2:$water_im = imagecreatefromjpeg($waterImage);break;
case 3:$water_im = imagecreatefrompng($waterImage);break;
default:
echo "<script>alert('{$formatMsg}'); history.back(); </script>";
return "";
}
// 检测GD库解析水印图片失败
if ($water_im === false) {
echo "<script>alert('GD库解析水印图片失败!可能是图片损坏、格式不兼容。'); history.back(); </script>";
return "";
}
}
// 读取背景图片
if(!empty($groundImage) && file_exists($groundImage))
{
$ground_info = getimagesize($groundImage);
// 检测getimagesize解析背景图片失败
if (!$ground_info) {
echo "<script>alert('目标图片解析失败!可能是文件损坏或格式错误。'); history.back(); </script>";
return "";
}
$ground_w = $ground_info[0];//取得背景图片的宽
$ground_h = $ground_info[1];//取得背景图片的高
// 检测GD库创建背景图片资源失败
switch($ground_info[2])//取得背景图片的格式
{
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:
echo "<script>alert('{$formatMsg}'); history.back(); </script>";
return "";
}
// 检测GD库解析背景图片失败
if ($ground_im === false) {
echo "<script>alert('GD库解析目标图片失败!可能是图片损坏、格式不兼容。'); history.back(); </script>";
return "";
}
}
else
{
// ========== 兼容PHP5.6:替换??为空判断 ==========
$noImgMsg = isset($fun_r['synotdoimg']) ? $fun_r['synotdoimg'] : '目标图片不存在!';
echo "<script>alert('{$noImgMsg}'); history.back(); </script>";
return "";
}
// 水印位置
if($isWaterImage)//图片水印
{
$w = $water_w;
$h = $water_h;
$label = "图片的";
}
else//文字水印
{
// 检测字体文件存在性
if (!file_exists($myfontpath)) {
echo "<script>alert('水印字体文件不存在!请检查字体文件路径。'); history.back(); </script>";
return "";
}
// 检测imagettfbbox执行失败
$temp = imagettfbbox(ceil($textFont*2.5),0,$myfontpath,$waterText);//取得使用 TrueType 字体的文本的范围
if ($temp === false) {
echo "<script>alert('解析水印文字失败!可能是字体文件损坏或文字内容非法。'); history.back(); </script>";
return "";
}
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset($temp);
$label = "文字区域";
}
if( ($ground_w<$w) || ($ground_h<$h) )
{
// ========== 兼容PHP5.6:替换??为空判断 ==========
$tooSmallMsg = isset($fun_r['sytoosmall']) ? $fun_r['sytoosmall'] : '目标图片尺寸过小,无法添加水印!';
echo "<script>alert('{$tooSmallMsg}'); history.back(); </script>";
return '';
}
switch($waterPos)
{
case 0://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
case 1://1为顶端居左
$posX = 0;
$posY = 0;
break;
case 2://2为顶端居中
$posX = ($ground_w - $w) / 2;
$posY = 0;
break;
case 3://3为顶端居右
$posX = $ground_w - $w;
$posY = 0;
break;
case 4://4为中部居左
$posX = 0;
$posY = ($ground_h - $h) / 2;
break;
case 5://5为中部居中
$posX = ($ground_w - $w) / 2;
$posY = ($ground_h - $h) / 2;
break;
case 6://6为中部居右
$posX = $ground_w - $w;
$posY = ($ground_h - $h) / 2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $ground_h - $h;
break;
case 8://8为底端居中
$posX = ($ground_w - $w) / 2;
$posY = $ground_h - $h;
break;
case 9://9为底端居右
$posX = $ground_w - $w;
$posY = $ground_h - $h;
break;
default://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
}
// 设定图像的混色模式
imagealphablending($ground_im, true);
if($isWaterImage)//图片水印
{
if($water_info[2]==3)
{
// 检测imagecopy执行失败
$copyResult = imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
if ($copyResult === false) {
echo "<script>alert('添加图片水印失败!GD库拷贝图片资源出错。'); history.back(); </script>";
imagedestroy($ground_im);
if(isset($water_im)) imagedestroy($water_im);
return "";
}
}
else
{
// 检测imagecopymerge执行失败
$copyResult = imagecopymerge($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h,$w_pct);//拷贝水印到目标文件
if ($copyResult === false) {
echo "<script>alert('添加图片水印失败!GD库合并图片资源出错。'); history.back(); </script>";
imagedestroy($ground_im);
if(isset($water_im)) imagedestroy($water_im);
return "";
}
}
}
else//文字水印
{
if( !empty($textColor) && (strlen($textColor)==7) )
{
$R = hexdec(substr($textColor,1,2));
$G = hexdec(substr($textColor,3,2));
$B = hexdec(substr($textColor,5));
}
else
{
// ========== 兼容PHP5.6:替换??为空判断 ==========
$fontColorMsg = isset($fun_r['synotfontcolor']) ? $fun_r['synotfontcolor'] : '水印文字颜色格式错误!';
echo "<script>alert('{$fontColorMsg}'); history.back(); </script>";
return "";
}
// 检测颜色分配失败
$fontColor = imagecolorallocate($ground_im, $R, $G, $B);
if ($fontColor === false) {
echo "<script>alert('分配水印文字颜色失败!GD库颜色解析出错。'); history.back(); </script>";
imagedestroy($ground_im);
return "";
}
// 检测文字写入失败
$writeResult = imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, $fontColor);
if ($writeResult === false) {
echo "<script>alert('添加文字水印失败!GD库写入文字出错。'); history.back(); </script>";
imagedestroy($ground_im);
return "";
}
}
// 生成水印后的图片
@unlink($groundImage);
// 检测图片保存失败
$saveSuccess = true;
switch($ground_info[2])//取得背景图片的格式
{
case 1:$saveSuccess = imagegif($ground_im,$groundImage);break;
case 2:$saveSuccess = imagejpeg($ground_im,$groundImage,$w_quality);break;
case 3:$saveSuccess = imagepng($ground_im,$groundImage);break;
default:
echo "<script>alert('{$formatMsg}'); history.back(); </script>";
$saveSuccess = false;
break;
}
if (!$saveSuccess) {
echo "<script>alert('保存水印后的图片失败!GD库写入文件出错。'); history.back(); </script>";
// 释放资源
if(isset($water_im)) imagedestroy($water_im);
imagedestroy($ground_im);
return "";
}
// 释放内存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
