views:

221

answers:

2

Hi,

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

A: 

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

header("content-type: image/png");
imagepng($img, 'file.png');
thetaiko
I'm able to generate the image, that's not the problem.What i can't figure out how is how to create semi trasparent shapes on a transparent background, using imagecolorallocatealpha does not work.
fabbrillo
Have you tried skipping the creation of a white rectangle? See what happens if you use `$black = imagecolorallocate($img,0,0,0); imagecolortransparent($img, $black);`
thetaiko
A: 

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.

"; $ak .= ""; $ak .= ""; 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 ""; echo "$Gal
"; echo "";

if (!isset($iid)) { $rows = round(count($th)/$cols); if (($rows * $cols) < (count($th))) { $rows++; } echo ""; echo "
"; echo "Click on the thumbnail for a larger image.
"; echo "";

echo ""; for ($i = 1; $i <= $rows; $i++) { echo ""; 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>";

?>

Ludwig