我试图在PHP中调整透明背景的png,我在网上找到的代码示例对我不起作用.这是我正在使用的代码,建议将不胜感激!
$this->image = imagecreatefrompng($filename);
imagesavealpha($this->image, true);
$newImage = imagecreatetruecolor($width, $height);
// Make a new transparent image and turn off alpha blending to keep the alpha channel
$background = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $newImage;
imagepng($this->image,$filename);
Run Code Online (Sandbox Code Playgroud)
更新
"不工作"我的意思是当我调整pngs时,背景颜色会变为黑色.
我对图像缩放的讨论很感兴趣,后来发现我用来从上传的图像创建缩略图的PHP代码遇到了同样的问题.我决定尝试在底部附近发布的PHP修补程序(将伽玛值从2.2转换为1.0,调整图像大小,将伽玛值从1.0转换回2.2).这有助于解决文章中提到的问题,但是对代码的这种修改具有淘汰PNG alpha通道透明度的不幸副作用.
这是我对伽马校正的代码.
<?php
$image = imagecreatefrompng($source_file);
$resized_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($resized_image, false);
imagesavealpha($resized_image, true);
imagegammacorrect($image, 2.2, 1.0);
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagegammacorrect($resized_image, 1.0, 2.2);
imagepng($resized_image, $dest_file);
?>
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何调整图像大小,采用伽马校正技巧,同时保持原始图像的alpha通道透明度?
样本图片:
在尝试更正伽玛之前,您可以看到透明度很好.(查看透明度的最简单方法是检查包裹在图像周围的段落标记,并通过FireBug或类似方法添加背景:black;到其样式属性.)
原始图像http://ender.hosting.emarketsouth.com/images/test-image.png 没有伽马校正http://ender.hosting.emarketsouth.com/images/test-image-resized-no-gamma.png 伽玛纠正 - 没有透明度http://ender.hosting.emarketsouth.com/images/test-image-resized.png
我正在使用此代码调整大小:
<?php
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, …Run Code Online (Sandbox Code Playgroud) 我使用这个类来上传和调整图像大小.问题是图像质量不好!我需要上传图像而不会丢失质量!
class SimpleImage {
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=75, $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 …Run Code Online (Sandbox Code Playgroud)