我需要一个PHP网站的通用图像上传。照片和徽标应该在一定程度上重新调整大小,以确保它们不会太大,并且适合设计。
我试着用下面的代码:
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == PNG or $this->image_type == GIF) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $nWidth, $nHeight, $transparent);
}
imagecopyresized($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}然而,当我上传一个包含alpha值在0到255之间的区域的图像时,它们会被完全的黑色替换,将抗锯齿区域变成黑色边框。
全透明对于PNG和GIF来说工作得很好,只是半透明区域是个问题。
如果我没有使用正确的术语来解释我的问题,我道歉,也许这就是为什么我在上面几乎找不到任何东西。
发布于 2012-04-03 17:22:16
尝试:
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == PNG or $this->image_type == GIF) {
imagefill($new_image, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($new_image,true);
imagealphablending($new_image, true);
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}基于this (我知道它是有效的)。
https://stackoverflow.com/questions/9545600
复制相似问题