2012-01-17 28 views
0

嗨,我嘗試使用下面的代碼從函數返回兩個變量返回兩個變量:試圖從功能

$img_width = 4000; 
$img_height = 3000; 
function change_large_image_size($img_width, $img_height) 
{ 
    if(($width == $height) && ($width > 790) && ($height > 790)){$case = 1;} 
    if($width < $height && ($width > 790) && ($height > 790)){$case = 2;} 
    if($width > $height && ($width > 790) && ($height > 790)){$case = 3;} 

    switch($case) 
    { 
     case 1: 
      $new_width = 790; 
      $new_height = 790; 

     break; 

     case 2: 
      $new_height = 790; 
      $ratio = $new_height/$height; 
      $new_width = round($width * $ratio); 
     break; 

     case 3: 
      $new_width = 790; 
      $ratio = $new_width/$width; 
      $new_height = round($height * $ratio); 

     break; 

     default: 
      $new_width = $img_width; 
      $new_height = $img_height; 
     break; 

    } 

    return array($new_width, $new_height); 
} 
list($new_width, $new_height) = change_large_image_size($img_width, $img_height); 

     echo '<p>' . $img_width . $img_height . '<br>' . $new_width . $new_height . '</p>'; 

解決了我在最初的問題,但有一個新的 出於某種原因該功能不斷拋出交換機中的默認情況。

+1

在整個函數中使用echo($ new_width)來確定你得到多遠,變量被設置爲...等等等等。如果你仍然無法弄清楚,至少讓我們知道變量在「返回」行之前是什麼。 – Dave 2012-01-17 20:59:22

回答

0

顯然,您的三個條件中沒有一個成立,所以$case結果在switch語句中被評估爲null並且採用默認分支。

+0

感謝這是一個簡單的變量命名問題,dooh – crm 2012-01-17 21:09:37

0

有兩個功能。一個用於高度的寬度爲

+0

在調用程序中更改其名稱後,忘記重命名函數中的某些變量,doh!它現在可以工作 – crm 2012-01-17 21:06:46