我有一段代码似乎在处理活动服务器上的jpegs时工作得很好,但处理png或gif时总是会给我黑色图像。有趣的是,在我的测试服务器上,它处理PNG的很好,但没有GIF。这个函数应该适用于所有的MIME类型,但我猜php找到任何文件路径的文件扩展名不是jpg。任何人有任何想法修改我的功能来正确处理PNG和GIF的,除了切换到Python?Php处理的缩略图图像返回黑色
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig/$h_orig;
if (($w/$h) > $scale_ratio) {
$w = $h * $scale_ratio; //if original image width is greater than height
} else {
$h = $w/$scale_ratio; //if original image height is greater than width
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);//gd functions
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);//makes a black rectangle with width and height you specify
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}
}
$file_name = $_FILES["uploaded_file"]["name"]; // The file name
$path_suffix = pathinfo($file_name);
$path_ext = $path_suffix['extension'];
$target_file = "uploads/$file_name";
$list_file = "uploads/list_$file_name";
$wmax = 400;
$hmax = 400;
ak_img_resize($target_file, $list_file, $wmax, $hmax, $path_ext);
什么是您的PHP内存限制?你有没有试图用ini_set('memory_limit','64M')来升级它? – Stefan
@Stefan本地和现场服务器都在128M。 –
你是否在其他地方设置了'$ path_ext'?如果不是,该脚本将始终使用'imagecreatefromjpeg'。 –