2013-02-12 15 views
0

這裏是我的PHP代碼:圖像裁切系統不具有或PNG工作,但以JPG工作

<?php 

$dst_x = 0; 
$dst_y = 0; 
$src_x = $_POST['left']; 
$src_y = $_POST['top']; 
$dst_w = $_POST['width']; 
$dst_h = $_POST['height']; 
$src_w = $_POST['width']; 
$src_h = $_POST['height']; 

$file_name = 'test.gif'; 

$allowed = array('jpg','jpeg','gif','png'); 
$file_extension= explode('.', $file_name); 
$file_extn= strtolower(end($file_extension)); 

$dst_image = imagecreatetruecolor($dst_w,$dst_h); 

if(in_array($file_extn, $allowed) == 'jpg, jpeg'){ 
    $src_image = imagecreatefromjpeg($file_name); 
} 
else if(in_array($file_extn, $allowed) == 'gif') { 
    $src_image = imagecreatefromgif($file_name); 
} 
else if(in_array($file_extn, $allowed) == 'png') { 
    $src_image = imagecreatefrompng($file_name); 
} 

imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); 
imagejpeg($dst_image, "new.jpg"); 

?> 

當我裁剪圖像,它的作品以JPG,但不與或PNG。它顯示的是一張空白圖片。我確定if語句有問題。這裏有什麼問題?如果我單獨做,沒有if語句,它沒有問題,是的,我希望我的輸出是jpeg。

回答

0

首先你在這裏犯了一個錯誤。

else if(in_array($file_extn, $allowed) == 'png') { 
    $src_image = imagecreatefromgif($file_name); 
} 

應該是:

else if(in_array($file_extn, $allowed) == 'png') { 
    $src_image = imagecreatefrompng($file_name); 
} 

並必須有一個獨立的if語句。

if(in_array($file_extn, $allowed) == 'jpg, jpeg'){ 
    $src_image = imagecreatefromjpeg($file_name); 
} 
+0

雅,我只是意識到並改變它,它仍然無法工作:( – Cole 2013-02-12 06:29:13