투명 배경을 유지하는 대신 투명 배경을 가진 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
헤이 덕분에 작동하여 생성 된 이미지에서 출력에 PNG 이미지를 사용하는 것입니다; 하지만 저는 뭔가 잘못하고있는 중이거나 잘못 입력 한 것 같아요. 어디에서 말해야할까요? – user1773801
imagecolorallocatealpha() 함수 앞에 넣으십시오. –
내 신이 너무 화가 나, ive가 노력했지만 아무 것도 작동하지 않습니다. @ – user1773801