2014-05-21 37 views
0

我有一段代码似乎在处理活动服务器上的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); 
+0

什么是您的PHP内存限制?你有没有试图用ini_set('memory_limit','64M')来升级它? – Stefan

+0

@Stefan本地和现场服务器都在128M。 –

+0

你是否在其他地方设置了'$ path_ext'?如果不是,该脚本将始终使用'imagecreatefromjpeg'。 –

回答

0

我已经亲自试过你的代码,没有触及到的功能,但只能修改底部和它工作得很好。 这是我工作的脚本看起来像(我是从通过获取URL获取的文件名,用于调试):我在几个不同的PNG,JPG和GIF文件测试

<?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 = $_GET['img']; // The file name 
$path_ext = substr($file_name, -3); 

$target_file = $file_name; 
$list_file = 'list_' . $file_name; 
$wmax = 400; 
$hmax = 400; 
ak_img_resize($target_file, $list_file, $wmax, $hmax, $path_ext); 
?> 

注意,他们都调整大小就好,应该为你工作,除非原始图像文件本身有问题。

希望这会有所帮助。

+0

对不起。我应该更好地解释$ path_ext来自哪里。我编辑了我的帖子 –

+0

@JohnBowlinger修改后的答案呢?它还会产生黑色的GIF和PNG吗?我真的很好奇。另外,如果仍然如此,您测试了多少个不同的图像文件?我可以从某处下载测试图像来测试自己吗?谢谢。 – CreativeMind