这是我制作的剧本
// Create image
$img = imagecreatetruecolor($w, $h);
// Transparent image
$white = imagecolorallocate($img,255,255,255);
imagefilledrectangle($img,0,0,$w,$h,$white);
imagecolortransparent($img, $white);
//imagealphablending( $img, true );
//imagesavealpha( $img, true );
// Shape color
$bgColor = imagecolorallocatealpha($img, 100, 250, 250, 70);
imagefilledrectangle($img, 15, 20, 50, 100, $bgColor);
imagefilledrectangle($img, 10, 25, 60, 100, $bgColor);
imagepng($img, 'file.png');
问题是矩形在它们之间是透明的而不是背景
我的意思是,我需要在
中创建一个半透明形状的透明图像有什么建议吗?
感谢您的帮助
答案 0 :(得分:1)
此代码生成带有可点击拇指的缩略图库。它创建半透明的png图像。它一直是我发现的唯一代码。也许对任何有兴趣的人都有用。
<?php
$ak = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
$ak .= "<html><head>";
$ak .= "</head>";
echo $ak;
$Gal = "GALLERY # 1"; //title
$thumb = "thumbs"; //thumbs directory name
$thumbwidth = 250;
$imagequality = 100;
$cols = 3; //number of columns
$vi = "Click for a larger image.";
$sname = "index.php"; //name of this file
$isz = "Size"; //text for "Size"
$msgcp = "Image"; //text for "Image"
$msgof = "of"; //text for "of"
if (isset($_GET['iid'])) {
$_GET['iid'];
} elseif (isset($_POST['iid'])) {
$_POST['iid'];
}
$files = array();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$jpg = ".jpg";
if (preg_match('/.jpg/', $file)
|| preg_match('/.gif/', $file)
|| preg_match('/.png/', $file)){
$files[] = $file;
}
}
}
closedir($handle);
}
sort($files);
if (!is_dir($thumb)) {
mkdir($thumb, 0755);
}
$i = 0;
$th = array();
$iw = array();
$ih = array();
$ifs = array();
foreach ($files as $image) {
$thumbimage = $thumb."/".$image;
$thumb_exists = file_exists($thumbimage);
$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];
$type = $size[2];
if (!$thumb_exists) {
set_time_limit(30);
switch ($type) {
case 1 :
$im = ImageCreateFromGIF($image);
break;
case 2 :
$im = ImageCreateFromJPEG($image);
break;
case 3 :
$im = ImageCreateFromPNG($image);
break;
}
$newwidth = $thumbwidth;
$newheight = ($newwidth / $width) * $height;
$im2 = ImageCreateTrueColor($newwidth,$newheight);
imagesavealpha($im2, true);
$trans_colour = imagecolorallocatealpha($im2, 0, 0, 0, 127);
imagefill($im2, 0, 0, $trans_colour);
ImageCopyResampled($im2,$im,0,0,0,0,$newwidth,$newheight,$width,$height);
switch ($type) {
case 1:
ImageGIF($im2, $thumbimage);
break;
case 2:
ImageJpeg($im2, $thumbimage, $imagequality);
break;
case 3:
ImagePNG($im2, $thumbimage);
break;
imagedestroy($im);
imagedestroy($im2);
}
}
$th[$i] = $thumbimage;
$iw[$i] = $width;
$ih[$i] = $height;
$ifs[$i] = round((@filesize($image)/1024), 1);
$i++;
}
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">";
echo "<tr><td>$Gal</td></tr><br>";
echo "</table>";
if (!isset($iid)) {
$rows = round(count($th)/$cols);
if (($rows * $cols) < (count($th))) {
$rows++;
}
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">";
echo "<tr><td></td></tr><br>";
echo "<tr><td>Click on the thumbnail for a larger image.</td></tr><br>";
echo "</table>";
echo "<center><table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">";
for ($i = 1; $i <= $rows; $i++) {
echo "<tr>";
for ($j = 1; $j <= $cols; $j++) {
$td = (($i - 1) * $cols) + $j;
$iu = ($td - 1);
if (isset($th[$iu])) {
$filebodyname = split("[/\\.]", $files[$iu]) ;
$n = count($filebodyname)-2;
$filebodyname = $filebodyname[$n];
$op = "";
$op .= "<td align=\"center\" valign=\"bottom\">\n<br>\n";
$op .= "<a title=\"$vi\" href=\"$sname?iid=$iu\" target=\"_blank\" >\n";
$op .= "<img src=\"$th[$iu]\" border=\"0\"></a>\n";
$op .= "<br><a title=\"$vi\" href=\"$sname?iid=$iu\" target=\"_blank\" >$filebodyname</a><br>\n";
$op .= "$vi\n";
$op .= "</td>\n";
echo $op;
}
}
echo "</tr>\n";
}
} else {
$filebodyname = split("[/\\.]", $files[$iid]) ;
$n = count($filebodyname)-2;
$filebodyname = $filebodyname[$n];
$iid2 = $iid+1;
$tot = count($th);
$op = "";
$op .= "<center><table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">";
$op .= "<tr>\n<td align=\"center\"valign=\"top\">\n$msgcp $iid2 $msgof $tot </td>\n</tr>\n<tr>\n";
$op .= "<td align=\"center\">\n<br>\n";
$op .= "<img src=\"$files[$iid]\" border=\"0\">\n";
$op .= "<br>$filebodyname\n";
$op .= "<br>\n</td>\n</tr>\n";
echo $op;
echo "</table></center>";
}
echo "</table></center>";
echo "</body></html>";
?>
答案 1 :(得分:0)
在输出图像之前,尝试添加标题以指定内容类型:
header("content-type: image/png");
imagepng($img, 'file.png');