2013-02-22 27 views
0

我需要上傳一個圖像文件,然後創建一個更大的圖像,其中原來的大小調整。 看互聯網,我發現做這一招的東西:php上傳和信箱

function resize_image($source_image, $destination_width, $destination_height, $type = 2) { 
    // $type (1=crop to fit, 2=letterbox) 
    $source_width = imagesx($source_image); 
    $source_height = imagesy($source_image); 
    $source_ratio = $source_width/$source_height; 
    $destination_ratio = $destination_width/$destination_height; 
    if ($type == 1) { 
     // crop to fit 
     if ($source_ratio > $destination_ratio) { 
      // source has a wider ratio 
      $temp_width = (int)($source_height * $destination_ratio); 
      $temp_height = $source_height; 
      $source_x = (int)(($source_width - $temp_width)/2); 
      $source_y = 0; 
     } else { 
      // source has a taller ratio 
      $temp_width = $source_width; 
      $temp_height = (int)($source_width/$destination_ratio); 
      $source_x = 0; 
      $source_y = (int)(($source_height - $temp_height)/2); 
     } 
     $destination_x = 0; 
     $destination_y = 0; 
     $source_width = $temp_width; 
     $source_height = $temp_height; 
     $new_destination_width = $destination_width; 
     $new_destination_height = $destination_height; 
    } else { 
     // letterbox 
     if ($source_ratio < $destination_ratio) { 
      // source has a taller ratio 
      $temp_width = (int)($destination_height * $source_ratio); 
      $temp_height = $destination_height; 
      $destination_x = (int)(($destination_width - $temp_width)/2); 
      $destination_y = 0; 
     } else { 
      // source has a wider ratio 
      $temp_width = $destination_width; 
      $temp_height = (int)($destination_width/$source_ratio); 
      $destination_x = 0; 
      $destination_y = (int)(($destination_height - $temp_height)/2); 
     } 
     $source_x = 0; 
     $source_y = 0; 
     $new_destination_width = $temp_width; 
     $new_destination_height = $temp_height; 
    } 
    $destination_image = imagecreatetruecolor($destination_width, $destination_height); 
    if ($type > 1) { 
     imagefill($destination_image, 0, 0, imagecolorallocate ($destination_image, 255, 255, 255)); 
    } 
    imagecopyresampled($destination_image, $source_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height); 
    return $destination_image; 
} 

我調用這個函數我上傳的圖像

/// Set the working path 
$destination_path = getcwd().DIRECTORY_SEPARATOR; 
// 
$result = 0; 

/// Upload File 
    $target_path = $destination_path . 'img'.DIRECTORY_SEPARATOR . basename($_FILES['myFile']['name']); 

if(move_uploaded_file($_FILES['myFile']['tmp_name'], $target_path)) { 

// If it's all ok I set the destination path for the wider image 
// 
$pathDestinazione = $destination_path . $_POST['pathRidimensionata'] . basename($_FILES['myFile']['name']); 

// collect some infos about the source file 
list($width, $height, $type, $attr) = getimagesize($target_path); 

// Set the desidered dimension 
$desideredWidth = $_POST['width']; 
$desideredHeight = $_POST['height']; 

//I begin to create the new image 

$thumb = resize_image($target_path,$desideredWidth,$desideredHeight,2); 
imagejpeg($thumb, $pathDestinazione, 60); 

//if everything it's ok 
$result = 1; 
}else{ 
    $result = $_FILES['myFile']['error']; 
    } 

之後,但結果始終是白色圖像... 我可以不明白是錯誤,但我真的很新的PHP!

回答

0

你的函數有參數$ source_image - 這是圖像句柄類型。這應該是函數imagecreatefromjpeg或其他(gif,png)的結果。但是你調用函數並以字符串傳遞給文件名。

您應該從上傳的文件創建圖像,然後調用函數。

+0

謝謝, 我用這種方式修改: $ source = imagecreatefromjpeg($ target_path); $ thumb = resize_image($ source,$ desideredWidth,$ desideredHeight,2); – Ast 2013-02-22 16:34:57