帮助修复图片库
我的图像库有问题。我想我知道问题所在,但我对图像知之甚少,希望有人能告诉我到底出了什么问题。
我想做的是调整 .png 的大小并保留透明度。当我调整大小并保存 .png 图像时,它会失去透明度并变成黑色。
我认为问题出在 resize 函数中的 imagecreatetruecolor
函数。文档表明这会返回黑色图像。我不认为这就是我所追求的。
有人可以多管闲事并告诉我问题是否确实在于调整大小功能以及应该如何解决这个问题。
谢谢。
class ResizeImage {
// Load Image
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);
imagealphablending($this->image, true);
imagesavealpha($this->image, true);
}
}
// Resize the image
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;
}
// Save the image
function save($filename, $image_type='', $compression=100, $permissions=null) {
if ($image_type != '') {
$this->image_type = $image_type;
}
if( $this->image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
I have a problem with an image library. I think I know the problem but I know very little about images and was hoping that someone could tell me what precisely is going wrong.
What I am trying to do is resize a .png and preserve the transparency. When I resize and save a .png image it looses its transparency and turns black.
I believe that the problem is with the imagecreatetruecolor
function in the resize function. The documentation suggests this returns a black image. I don't think this is what I am after.
Could someone have a nosey and tell me if the problem does in fact lie with the resize function and how this should be fixed.
Thanks.
class ResizeImage {
// Load Image
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);
imagealphablending($this->image, true);
imagesavealpha($this->image, true);
}
}
// Resize the image
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;
}
// Save the image
function save($filename, $image_type='', $compression=100, $permissions=null) {
if ($image_type != '') {
$this->image_type = $image_type;
}
if( $this->image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 Priimage 类。查看示例。
Try to use Primage class. Check example.
尝试使用 imagesavealpha,例如:
try using imagesavealpha, for example: