2015-09-01 93 views
0

我正在使用此功能在圖像的左上角添加水印,jpeg或png。PHP開關案例問題

我需要Switch和Case來區分png和jpeg來分別處理每個圖像類型。

每次運行該功能時,圖像編輯完美,但調試顯示圖像正在以.png和.jpg處理,因此在圖像類型錯誤時發出問題。

function addWatermark($path) 
{ 
    $public_file_path = dirname(__FILE__); 
    $font = $public_file_path . '/src/fonts/Roboto-Light.ttf'; 
    // get fiel type for switch 
    $size = getimagesize($path); 
    // get $from name 
    $path_parts = pathinfo($path); 
    $text= $path_parts['filename']; 
    switch($path_parts['extension']) 
    { 
     case "jpeg" or "jpg": 
      // Copy and resample the imag 
      list($width, $height) = getimagesize($path); 
      $image_p = imagecreatefromjpeg($path); 
      // Prepare font size and colors 
      $text_color = imagecolorallocate($image_p, 0, 0, 0); 
      $bg_color = imagecolorallocate($image_p, 255, 255, 255); 
      $font_size = 17; 
      // Set the offset x and y for the text position 
      $offset_x = 0; 
      $offset_y = 20; 
      // Get the size of the text area 
      $dims = imagettfbbox($font_size, 0, $font, $text); 
      $text_width = $dims[4] - $dims[6] + $offset_x; 
      $text_height = $dims[3] - $dims[5] + $offset_y; 
      // Add text background 
      imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color); 
      // Add text 
      imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text); 
      // Save the picture 
      imagejpeg($image_p, $path, 90); 
      // Clear 
      imagedestroy($image_p); 
     case "png": 
      $im = imagecreatefrompng($path); 
      imagesavealpha($im, true); // important to keep the png's transparency 
      $text_color = imagecolorallocate($im, 0, 0, 0); 
      $width = imagesx($im); // the width of the image 
      $height = imagesy($im);; // the heighst of the image 
      $font_size = 15; // font size 
      $box_color = imagecolorallocate($im, 255, 255, 255); 
      // Set the offset x and y for the text position 
      $offset_x = 0; 
      $offset_y = 20; 
      $dims = imagettfbbox($font_size, 0, $font, $text); 
      $text_width = $dims[4] - $dims[6] + $offset_x; 
      $text_height = $dims[3] - $dims[5] + $offset_y; 
      // Add text background 
      imagefilledrectangle($im, 0, 0, $text_width, $text_height, $box_color); 
      // Add text 
      imagettftext($im, $font_size, 0, $offset_x, $offset_y, $text_color, $font,$text); 
      imagepng($im, $path, 0); 
      imagedestroy($im); 
     case "mp4"; 
      break; 
    } 
    // hasta aca 
} 
+5

你的一些情況下,缺少休息。另外,你可能想要添加一個默認的 - http://php.net/manual/en/control-structures.switch.php –

+0

@GeoffAtkins我編輯了功能 –

+0

@ Fred-ii-確實是案例還是工作?它似乎沒有在我的測試中。 – Devon

回答

1

您需要將break;添加到您的病例陳述中。

switch($path_parts['extension']) 
{ 
    case "jpeg": 
    case "jpg": 
     // Your code 
     break; 
    case "png": 
     // Your code 
     break; 
    case "mp4"; 
     break; 
} 
0

所以它看起來像你可能會丟失休息statments,我不知道的情況下(「JPG」 )或案例('jpeg')將工作。您可以使用此替代:

switch(var) { 
    case('jpg'): 
    case('jpeg'): 
break; 

case('png'): 
break; 

default: 
break; 
}