Как генерировать полупрозрачные фигуры в php с помощью gdlib - PullRequest
0 голосов
/ 23 февраля 2010

это скрипт, который я сделал

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

проблема в том, что прямоугольники прозрачны среди них, но не с фоном

я имею в виду, мне нужно создать прозрачное изображение с полупрозрачной формой внутри

есть предложения?

спасибо за вашу помощь

Ответы [ 2 ]

1 голос
/ 17 октября 2010

Этот код создает галерею миниатюр с кликабельными пальцами. Это создает полупрозрачные изображения 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>";

?>
0 голосов
/ 23 февраля 2010

Попробуйте добавить заголовок, чтобы указать тип содержимого перед выводом изображения:

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