2010-10-31 17 views
0

林當用戶上傳,並嘗試對裁剪PNG或透明圖像(名爲.gif)PHP:作物PNG,反有問題

我越來越:

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in statusUpFunctions.php on line 100 

Warning: imagecreatefromjpeg(): 'images/status/photo/1-6.jpg' is not a valid JPEG file in statusUpFunctions.php on line 100 

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in statusUpFunctions.php on line 114 

這可能是因爲我的PHP作物功能:

case 'crop': 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

ini_set('memory_limit', '256M'); 
    $jpeg_quality = 90; 

    $src = "images/status/photo/".$_POST['fname']; 
    $img_r = imagecreatefromjpeg($src); 

     if($_POST['fixed'] == 0) { 
      $targ_w = $_POST['w']; 
      $targ_h = $_POST['h']; 
     } 
     else { 
      $targ_h = $_POST['sizeh']; 
    $targ_w = $_POST['sizew']; 
     } 

     $dst_r = ImageCreateTrueColor($targ_w, $targ_h); 

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], 
    $targ_w,$targ_h,$_POST['w'],$_POST['h']); 

$path_thumbs = "images/status/photo"; 
    $filename = $newfilename; 
    $thumb_path = $path_thumbs . '/' . $filename; 

    imagejpeg($dst_r,$thumb_path,$jpeg_quality); 
} 
break; 
} 

從JPG造成的,我不能這樣做從GIF和PNG和BMP創建等..?然後以某種方式轉換它。

這應該如何解決?

回答

2

功能imagecreatefromjpeg()創建了一個從JPEG圖像的通用PHP圖像對象來獲取圖像標識符。然後,您可以對物體做任何你想做的事情。

imagecreatefromjpeg()只會從JPEG創建一個通用圖像對象。如果要從PNG或GIF創建通用圖像對象,則需要使用它們各自的功能:imagecreatefrompng()imagecreatefromgif()。檢測圖像類型的一種快速方法是通過讀取其文件擴展名。

一旦你有了這個通用圖像對象,那麼你可以做它想要的(包括使用imagecopyresampled()),然後使用imagejpeg()從它創建一個jpeg圖像。

編輯:

您可以使用pathinfo()來檢測文件的擴展名:

$filename = pathinfo($_POST['fname']); // Returns an array of file details 
$extension = $filename['extension']; 

如果您是從$_POST值獲取文件名,你需要確保你複製的臨時圖像文件到該文件名。我沒有看到代碼中使用的任何$_FILES值(也許你正在使用另一個腳本?)。如果沒有,here's a good tutorial on handling file uploads。它也包含文件擴展名。

+0

嗨感謝你的答案。我如何檢測文件擴展名? $ _POST [ 'FNAME'];是文件名,例如asddsa.jpg – Johnson 2010-10-31 23:57:29

+0

一個非常簡單的方法是查看strpos($ _ POST ['fname'],'.jpg');返回true。只需將'.jpg'更改爲'.png','。gif','。jpeg'即可測試其他類型。 – Klinky 2010-11-01 00:08:26

0

它無法加載圖像,導致imagecreatefromjpeg()返回false。確保你試圖打開一個有效的圖像。

0

對於GIF和PNG文件,你需要使用imagecreatefromgif和imagecreatefrompng

0
<?php 

    $image = imagecreatefrompng('myPhoto.png'); 

    $crop = imagecreatetruecolor(50,50); 

    $userImage = imagecopyresampled($crop, $image, 0, 0, 0, 0, 50, 50, 8, 8); 

    header('Content-type: image/png'); 

    imagepng($crop); 

?> 

這將返回裁剪圖像。

1

嘗試這...這將工作..

<?php 

$image = imagecreatefrompng('photo.png'); 


$thumb_width = 280; 
$thumb_height = 200; 

$width = imagesx($image); 
$height = imagesy($image); 

$original_aspect = $width/$height; 
$thumb_aspect = $thumb_width/$thumb_height; 

if ($original_aspect >= $thumb_aspect){ 
    // If image is wider than thumbnail (in aspect ratio sense) 
    $new_height = $thumb_height; 
    $new_width = $width/($height/$thumb_height); 
}else{ 
    // If the thumbnail is wider than the image 
    $new_width = $thumb_width; 
    $new_height = $height/($width/$thumb_width); 
} 


$crop = imagecreatetruecolor($thumb_width,$thumb_height); 

imagealphablending($crop, false); 
$white = imagecolorallocatealpha($crop, 0, 0, 0, 127); //FOR WHITE BACKGROUND 
imagefilledrectangle($crop,0,0,$thumb_width,$thumb_height,$white); 
imagesavealpha($crop, true); 
$userImage = imagecopyresampled($crop, $image, 
        0 - ($new_width - $thumb_width)/2, // Center the image horizontally 
        0 - ($new_height - $thumb_height)/2, // Center the image vertically 
        0, 0, $new_width, $new_height, 
        $width, $height); 

header('Content-type: image/png'); 

imagepng($crop); 


?>