2012-02-04 4 views
0

아바타 업로드를 처리하는 기능을 만들려고합니다. 내 문제는 투명 이미지를 업로드 할 때 크기를 조정 한 후에 투명한 이미지가 투명한 검은 색으로 바뀌 었습니다.투명한 이미지를 업로드하면 검은 색 배경이 추가됩니다.

imageavealpha() & imagealphablending() 옵션을 사용해 보았지만 배경은 여전히 ​​검은 색으로 변하고 있습니다.

나는 눈이 멀어서 코드에서 문제가 보이지 않을 수도 있지만, 왜 이런 일을하는지 전혀 모릅니다.

이미지를 업로드 한 직후 이미지/아바타 폴더로 이동 한 후에도 배경이 여전히 투명 함을 확인할 수 있습니다.

이, 내가 .png를 이미지로 내 현재 코드 테스트되고있다 : 지금이보고 너트려고로

function upload_avatar(){ 
    $base_path = SYSTEM_PATH; 

    if($_FILES['avatar_img']['tmp_name'] != '') { 
     $id = md5($this->user_id()); 
     $filename = $_FILES['avatar_img']['name']; 
     $file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention 
     $file_ext = substr($filename, strripos($filename, '.')); // strip name 
     $filesize = $_FILES['avatar_img']['size']; 
     $newfilename = $id . $file_ext; 

     if ($file_ext == ".jpg" || $file_ext == ".JPG" || $file_ext == ".jpeg" || $file_ext == ".png" || $file_ext == ".gif"){ 
      if($filesize <= 153600){ 

       move_uploaded_file($_FILES['avatar_img']['tmp_name'], $base_path."/images/avatars/" . $newfilename); 

       //resize image form 
       list($width, $height) = getimagesize($base_path."/images/avatars/" . $newfilename); 
       $scale_height = $height/$width; 
       $scale_width = $width/$height; 

       //Find height and width of the image 
       if($width > $height && $width > 150){ 
        $width_new = 150; 
        $height_new = round($width_new*$scale_height); 
       }else if($height > $width && $height > 150){ 
        $height_new = 150; 
        $width_new = round($height_new*$scale_width); 
       }else{ 
        $height_new = $height; 
        $width_new = $width; 
       } 

       switch($file_ext) { 
        case ".jpg" : 
         case ".jpeg": 
          $source = imagecreatefromjpeg($base_path."/images/avatars/" . $newfilename); 
        break; 

        case ".png" : 
         $source = imagecreatefrompng($base_path."/images/avatars/" . $newfilename); 
        break; 

        default: 
         $source = imagecreatefromgif($base_path."/images/avatars/" . $newfilename); 
        break; 
       } 

       $destination = imagecreatetruecolor($width_new, $height_new); 

       imagesavealpha($destination, true); 
       imagealphablending($destination, true); 

       imagecopyresized($destination, $source, 0, 0, 0, 0, $width_new, $height_new, $width, $height); 

       switch($file_ext) { 
        case ".jpg": 
        case ".jpeg": 
         imagejpeg($destination, $base_path."/images/avatars/" . $newfilename, 85); 
        break; 

        case ".png": 
         imagepng($destination, $base_path."/images/avatars/" . $newfilename, 8); 
        break; 

        default: 
         imagegif($destination, $base_path."/images/avatars/" . $newfilename, 85); 
        break; 
       } 

       return $newfilename; 
      }else{ 
       $this->upload_avatar = '<br />But the avatar was not updated. The avatar\'s size exceeded the 150kb limit. '; 
       return ''; 
      } 
     }else{ 
      $this->upload_avatar = '<br />But the avatar was not updated. The avatar must be one of the following formats: jpg, jpeg, png or gif. '; 
      return ''; 
     } 
    }else{ 
     return ''; 
    } 
} 

어떤 도움

주시면 감사하겠습니다.

감사합니다.

답변

1

This here 당신이 찾고있는 것일 수도 있습니다. 두 번째 주석에는 투명도를 유지하면서 png 및 gif의 크기를 조정하는 예제가 포함되어 있습니다.

p.s. 나는 이것을 코멘트로 추가 했었지만 아직 그럴 권한이 없다.

+0

환호, 그것을 줄 것이다 :). 이전에이 함수를 사용했지만 위의 방법을 사용하는 것이 가능할 것이라고 생각했습니다. 나는 분명히 틀렸다. 아무도 내 초기 코드에서 문제가 무엇인지 말할 수 없으면 시도해보십시오. 감사합니다. – MrE

+0

링크 된 함수를 사용하여 변환했습니다. 내가 그걸로 갈 것 같아. 감사! – MrE

관련 문제