2010-02-23 1 views
0

c'est le script que j'ai faitComment générer des formes semi-transparentes en php en utilisant GDLib

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

le problème est que les rectangles sont transparents entre eux, mais pas avec l'arrière-plan

Je veux dire, je dois créer une image transparente avec une forme semi-transparente à l'intérieur

des suggestions?

Merci pour votre aide

Répondre

0

Essayez d'ajouter un en-tête pour spécifier le type de contenu avant la sortie de l'image:

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

Ce que je n'arrive pas à comprendre comment créer des formes semi-transparentes sur un fond transparent, en utilisant imagecolorallocatealpha ne fonctionne pas. – fabbrillo

+0

Avez-vous essayé de sauter la création d'un rectangle blanc? Voyez ce qui se passe si vous utilisez '$ black = imagecolorallocate ($ img, 0,0,0); imagecolortransparent ($ img, $ black); ' – thetaiko

1

Ce code génère une galerie de vignettes avec cliquables pouces. Il crée des images png semi-transparentes. C'est le seul code que j'ai trouvé qui fonctionne. Peut-être d'une certaine utilité à toute personne intéressée. Je suis capable de générer l'image, ce n'est pas le problème.

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

?> 
Questions connexes