【发布时间】:2014-06-30 20:17:51
【问题描述】:
上传 PNG 文件时的透明度似乎有问题。为了解决这个问题,我需要添加什么?
<?php
include 'thumbnailer_func.php';
$image_file = str_replace ('..', '', $_SERVER['QUERY_STRING']);
$image_path = '' . $_REQUEST['image'];
$img = null;
$ext = strtolower (end (explode ('.', $image_path)));
if (($ext == 'jpg' OR $ext == 'jpeg')) {
$img = @imagecreatefromjpeg ($image_path);
} else {
if ($ext == 'png') {
$img = @imagecreatefrompng ($image_path);
} else {
if ($ext == 'gif') {
$img = @imagecreatefromgif ($image_path);
}
}
}
if ($img) {
$width = imagesx ($img);
$height = imagesy ($img);
$scale = min (MAX_WIDTH / $width, MAX_HEIGHT / $height);
if ($scale < 1) {
$new_width = floor ($scale * $width);
$new_height = floor ($scale * $height);
$tmp_img = imagecreatetruecolor ($new_width, $new_height);
imagecopyresized ($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy ($img);
$img = $tmp_img;
}
}
if (!$img) {
$img = imagecreate (MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate ($img, 0, 0, 0);
$c = imagecolorallocate ($img, 255, 255, 255);
imageline ($img, 0, 0, MAX_WIDTH, MAX_HEIGHT, $c2);
imageline ($img, MAX_WIDTH, 0, 0, MAX_HEIGHT, $c2);
}
header ('Content-type: image/jpeg');
imagejpeg ($img, null, $image_quality);
imagedestroy ($img);
?>
【问题讨论】:
-
你不上传它,你转换它。
-
这段代码将图片转换为
jpeg格式(header ('Content-type: image/jpeg')); jpeg 无法支持透明度。 -
那么我可以通过更改那段代码将其更改为 png 吗?
标签: php png transparency