2012-02-29 20 views
0

當我通過一個jpg文件到imgsize.php?w=100&h=100&img=powered_by.jpg那麼它的工作做好 沒有工作,但我通過一個PNG文件imgsize.php?w=100&h=100&img=mengo.png它不工作我「png格式」圖像大小調整圖像的代碼在PHP

我imgsize.php文件代碼是

$extension = pathinfo($_GET['img']); 
$extension = $extension[extension]; 
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") { 
    header("Content-type: image/jpeg"); 
} 
if ($extension == "png") { 
    header("Content-type: image/png"); 
} 
if ($extension == "gif") { 
    header("Content-type: image/gif"); 
} 
$img  = $_GET['img']; 
$nwidth = $_GET['w']; 
$nheight = $_GET['h']; 
$img2 = imagecreatefromjpeg($_GET['img']); 
$width = imagesx($img2); 
$height = imagesy($img2); 
$ratio = $width/$height; 
$new_nwidth = $nwidth; 
$new_nheight = floor($height * ($nwidth/$width)); 
$im = imagecreatefromjpeg($img) or $im = imagecreatefrompng($img) or $im = imagecreatefromgif($img) or $im = false; 
if (!$im) { 
} else { 
    $thumb = imagecreatetruecolor($new_nwidth, $new_nheight); 
    imagealphablending($thumb, false); 
    imagesavealpha($thumb, true); 
    $transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127); 
    imagefilledrectangle($thumb, 0, 0, $new_nwidth, $new_nheight, $transparent); 
    imagecopyresized($thumb, $im, 0, 0, 0, 0, $new_nwidth, $new_nheight, $width, $height); 
    if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") { 
     imagejpeg($thumb, null, 100); 
    } 
    if ($extension == "png") { 
     imagepng($thumb, null, 9); 
    } 
    if ($extension == "gif") { 
     imagegif($thumb, null, 100); 
    } 
} 

它的任何解決方案嗎?其顯示空白圖像時,我把它傳遞出一個PNG文件

+1

看看函數名稱,然後認爲PNG不是JPEG格式但可能手冊具有相同的功能爲PNG。 – 2012-02-29 05:46:34

回答

2

正如你在,如果其他條件檢查擴展到生成的頭部,你就必須檢查創建$ IMG2像

if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") { 
    $img2 = imagecreatefromjpeg($_GET['img']); 
} 
if ($extension == "png") { 
    $img2 = imagecreatefrompng($_GET['img']); 
} 
if ($extension == "gif") { 
    $img2 = imagecreatefromgif($_GET['img']); 
} 

彼此件事我想補充您的信息以同樣的方式是支票,而該檢查分機圖像MIME類型。爲此,您可以使用getimagesize函數,該函數返回一個數組,您可以通過$ retun_array ['mime']檢查MIME類型。

如果沒有正確創建或存儲,PNG圖像通常會產生問題。損壞的圖像也可能導致此問題。希望這可能適合你。

0

我離開你我的代碼爲縮略圖,並將它們保存到目標文件夾

function thumbail($forcedwidth, $forcedheight, $sourcefile, $destfile){ 
    $fw = $forcedwidth; 
    $fh = $forcedheight; 
    $is = getimagesize($sourcefile); 
    $ancho = $is[0]; 
    $alto = $is[1]; 
    if($ancho > $forcedwidth) { 
     $ancho2 = $forcedwidth; 
     $por = (100 * $forcedwidth)/$ancho; 
     $alto2 = ($alto * $por)/100; 
     $t = 1; 
    }else{ 
     $ancho2 = $ancho; 
     $alto2 = $alto; 
     $t = 2; 
    } 

    if($alto > $forcedheight) { 
     $alto2 = $forcedheight; 
     $por = (100 * $forcedheight)/$alto; 
     $ancho2 = ($ancho * $por)/100; 
     $t = 1; 
    }else{ 
     $ancho2 = $ancho; 
     $alto2 = $alto; 
    } 
    if ($t == 1){ 
     $img_src = imagecreatefromjpeg($sourcefile); 
     $img_dst = imagecreatetruecolor($ancho2, $alto2); 
     imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $ancho2, $alto2, $is[0], $is[1]); 
     if(!imagejpeg($img_dst, $destfile, 90)){ 
      exit(); 
     } 
    }else if ($t == 2){ 
     copy($sourcefile, $destfile); 
    } 
} 
+0

對不起Ernesto我不需要保存圖像,但我需要這樣動態調整它們的大小 - 謝謝 – 2012-02-29 05:51:31

+0

@Toms只是修改該doment,當你做copy()時只返回圖像 – Edig 2012-02-29 23:32:50