2012-10-25 3 views
4

투명 배경을 유지하는 대신 투명 배경을 가진 PNG 파일을 사용하여 PHP로 그림의 크기를 조정할 때 문제가 발생합니다. 그러면 검정 배경이됩니다. 이 문제를 어떻게 해결할 수 있습니까? PHP로 크기를 조정할 때 PNG에서 투명한 배경을 유지하는 방법은 무엇입니까?

는 크기 조정을위한 스크립트입니다

<?php   
class resize{ 

    var $image; 
    var $image_type; 

    function load($filename) { 

     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 
     if($this->image_type == IMAGETYPE_JPEG) { 

     $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 

     $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 

     $this->image = imagecreatefrompng($filename); 
     } 
    } 
    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=85, $permissions=null) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image,$filename,$compression); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image,$filename); 
     } elseif($image_type == IMAGETYPE_PNG) { 




     imagepng($this->image,$filename); 
     } 
     if($permissions != null) { 

     chmod($filename,$permissions); 
     } 
    } 
    function output($image_type=IMAGETYPE_JPEG) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     imagepng($this->image); 
     } 
    } 
    function getWidth() { 

     return imagesx($this->image); 
    } 
    function getHeight() { 

     return imagesy($this->image); 
    } 
    function resizeToHeight($height) { 

     $ratio = $height/$this->getHeight(); 
     $width = $this->getWidth() * $ratio; 
     $this->resize($width,$height); 
    } 

    function resizeToWidth($width) { 
     $ratio = $width/$this->getWidth(); 
     $height = $this->getheight() * $ratio; 
     $this->resize($width,$height); 
    } 

    function scale($scale) { 
     $width = $this->getWidth() * $scale/100; 
     $height = $this->getheight() * $scale/100; 
     $this->resize($width,$height); 
    } 

    function resize($width,$height) { 

     $new_image = imagecreatetruecolor($width, $height); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    }  

} 
?> 

그리고 이것은 내가 전화하는 방법입니다

$picture_directory="images/" . $_FILES["file"]["name"]; 

    include('arqinc/resizing.php'); 
    $image = new resize(); 
    $image->load($picture_directory); 
    $image->resize(660,780); 
    $image->save($picture_directory); 

편집 :

필자는이 내 크기 조정 기능을 변경 :

function resize($width,$height) { 

     $new_image = imagecreatetruecolor($width, $height); 
     $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127)); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    } 

하지만 이제는 흰색을 유지하고 투명하지 않습니다.

편집 2 :

해결이 크기 조정 기능이 고장이 하나가 완벽하게 작동 :

http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/

감사를 연결하는 Tahir Yasin 에 : D

답변

6

내가 찾은를 동일한 질문을 가진 또 다른 게시물은 다음 코드에 따라 투명성을 유지하는 데 사용될 수 있습니다.

imagealphablending($targetImage, false); 
imagesavealpha($targetImage, true); 

참조 : Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

투표 업하십시오 당신을 도움이된다면. :)

+0

헤이 덕분에 작동하여 생성 된 이미지에서 출력에 PNG 이미지를 사용하는 것입니다; 하지만 저는 뭔가 잘못하고있는 중이거나 잘못 입력 한 것 같아요. 어디에서 말해야할까요? – user1773801

+1

imagecolorallocatealpha() 함수 앞에 넣으십시오. –

+0

내 신이 너무 화가 나, ive가 노력했지만 아무 것도 작동하지 않습니다. @ – user1773801

0

imagecopyresampled() 코드 행 뒤에 imagepng($new_image,$file_name)을 사용해야합니다. $ file_name을 원하는 이름으로 바꿉니다. 이미지를 저장하십시오.

PHP imagepng()은 -

0

이 기능은 aswner에 대한

function resize($width,$height) { 
    $new_image = imagecreatetruecolor($width, $height); 
    $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127)); 
    imagealphablending($new_image, false); 
    imagesavealpha($new_image, true); 
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
    $this->image = $new_image; 
} 
+0

이 기능이 작동합니다 .... 기능 크기 조정 ($ 폭, $ 높이) { $ new_image = imagecreatetruecolor ($ 폭, $ 높이) 도움을 당신이 너무 & 행복 사랑; $ transparent = imagefill ($ new_image, 0, 0, imagecolorallocatealpha ($ new_image, 255, 255, 255, 127)); imagealphablending ($ new_image, false); imagesavealpha ($ new_image, true); imagecopyresampled ($ new_image, $ this-> image, 0, 0, 0, $ width, $ height, $ this-> getWidth(), $ this-> getHeight()); $ this-> image = $ new_image; } –

관련 문제