imagecopyresampled(): supplied argument is not a valid Image resource in并有给此函数提供一个合适的image资源作为参数。肯定是你传进去的某个参数有问题,应该是$temp_img这个参数有问题,$temp_img又是由imagecreatetruecolor函数所返回的值,所以再去看看imagecreatetruecolor函数哪里有问题,一步步反向的追踪上面的代码。最后发现,你有个变量名写错了。直接告诉你问题所在不如告诉你解决问题的方法,正所谓授人以鱼不如授人以渔
现列出正确的代码:
<?php//自定义创建缩略图函数
function thumb_image($srcfile,$desfile,$width=150,$height=100) {
$data= getimagesize($srcfile,&$info); //得到源图像文件信息
switch ($data[2]) {
case 1:
$im = @imagecreatefromgif($srcfile); //如果值为1 。则是gif格式
break;
case 2:
$im = @imagecreatefromjpeg($srcfile); //如果值为2 则是jpeg格式
break;
case 3:
$im = @imagecreatefrompng($srcfile); //如果值为3 则是png格式
break;
}
$srcW= imagesx($im); //得到原图片的宽度
$srcH= imagesy($im); //得到眼图片的高度 if(($srcW/$width)>=($srcH/$height)) { //判断宽和高哪个超过缩略图的比例
$temp_height =$height; //设置符合比例的高度
$temp_width = $srcW/($srcH/$height); //设置符合比例的宽度
$src_X=abs(($width-$temp_width)/2); //如果宽度超出则按缩略图调整x坐标
$src_Y=0;//源文件y坐标不变
}else{
$temp_width=$width;
$temp_height=$srcH/($srcW/$width);
$src_X=0;
$src_Y=abs(($height-$temp_height)/2);
}
echo '<br />new width:'.$temp_width;
echo '<br />new height:'.$temp_height;
$temp_img= imagecreatetruecolor($temp_width,$temp_height); //创建临时真彩画板
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_width,$temp_height,$srcW,$srcH); //创建缩略图使用的画板
$ni=imagecreatetruecolor($width,$height);
imagecopyresized($ni,$temp_img,0,0,$src_X,$src_Y,$width,$height,$width,$height);
$cr=imagejpeg($ni,$desfile); //输出至目标文件
chmod($desfile,0777); //设置目标文件的属性为可读可写
if($cr) { //如果写入成功
$sm_File= $desfile; //则返回true值
return true;
}else{
$Error=5; //否则设置error变量并返回
return $Error;
}
}
//调用该函数,创建一缩略图
thumb_image("2_abest.jpg","1753.jpg");?>