如何使用 gdlib 在 php 中生成半透明形状

发布于 2024-08-22 05:31:47 字数 632 浏览 6 评论 0原文

这是我制作的脚本,

    // 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');

问题是矩形在其中是透明的,但与背景不透明,

内创建一个具有半透明形状的透明图像

我的意思是,我需要在任何建议

?感谢您的帮助

this is the script i made

    // 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');

the problem is the rectangles are transparent among them but not with the background

i mean, i need to create a transparent image with a semi transparent shape inside

any suggestions?

thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2024-08-29 05:31:48

此代码生成一个带有可点击缩略图的缩略图库。它创建半透明的 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>";

?>

This code generates a thumbnails gallery with clickable thumbs. It creates semi-transparent png images. It has been the ONLY CODE THAT I HAVE FOUND THAT WORKS. Maybe of some use to anyone interested.

<?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>";

?>
留一抹残留的笑 2024-08-29 05:31:48

尝试在输出图像之前添加标头来指定内容类型:

header("content-type: image/png");
imagepng($img, 'file.png');

Try adding a header to specify the content-type before outputting the image:

header("content-type: image/png");
imagepng($img, 'file.png');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文