2012-04-26 49 views
1

我有這樣一段代碼在這裏PHP imagecopyresampled()沒有高度

imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height); 

Baiscally什麼,我試圖做的是上傳圖片並調整寬度和具有基於寬度來調整高度。

我想這也

imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width); 

沒有高度和得到這個錯誤

Warning: Wrong parameter count for imagecopyresampled() in /home/content/44/8713044/html/admin/Categories.php on line 63 

這是當前的代碼,其中$width$height變量從何而來。

if($width> $height) { 
      $x = ceil(($width - $height)/2); 
      $width = $height; 
     } elseif($height> $width) { 
      $y = ceil(($height - $width)/2); 
      $height = $width; 
     } 

任何幫助,將不勝感激,在先進的感謝, Ĵ

這裏是全功能..

function create_thumbnail($source,$destination, $thumb_width) { 
     $percent = 0.5; 
     $size = getimagesize($source); 
     $width = $size[0]; 
     $height = $size[1]; 
     $x = 0; 
     $y = 0; 
     if($width> $height) { 
      $x = ceil(($width - $height)/2); 
      $width = $height; 
     } elseif($height> $width) { 
      $y = ceil(($height - $width)/2); 
      $height = $width; 
     } 
     $new_image = imagecreatetruecolor($thumb_width,$thumb_width)or die('Cannot Initialize new GD image stream'); 
     $extension = get_image_extension($source); 
     if($extension=='jpg' || $extension=='jpeg') 
      $image = imagecreatefromjpeg($source); 
     if($extension=='gif') 
      $image = imagecreatefromgif($source); 
     if($extension=='png') 
      $image = imagecreatefrompng($source); 

     imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height); 
     if($extension=='jpg' || $extension=='jpeg') 
      imagejpeg($new_image,$destination); 
     if($extension=='gif') 
      imagegif($new_image,$destination); 
     if($extension=='png') 
      imagepng($new_image,$destination); 
    } 

$thumb_width是600,這將返回我的圖像600 * 600

+0

你能更多地討論你的意思有點通過具有基於寬度調節高度?你在尋找高度/寬度比例嗎? – 2012-04-26 17:27:50

+0

由於您正在調整'$ height'變量並根據寬度改變它的值,您是不是應該將它傳遞給函數並且不排除它? – Nadh 2012-04-26 17:31:27

回答

0

這個工作完美,當我測試它....除了你想使用固定大小

$filename = "a.jpg" ; 
$percent = 0.5; 
header('Content-Type: image/jpeg'); 
list($width, $height) = getimagesize($filename); 
$new_width = $width * $percent; 
$new_height = $height * $percent; 

// Resample 
$image_p = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
imagejpeg($image_p, null, 100); 

演示

+0

現在試試 – user979331 2012-04-26 18:11:08

+0

這工作!非常感謝爸爸!你真棒! – user979331 2012-04-26 18:39:16

+0

不客氣..問題是'$ x'和'$ y'我猜 – Baba 2012-04-26 19:15:58