Jump to content

Transparency through GD.


Kairu

Recommended Posts

Is it possible to make images semi transparent through the GD library? Ex: Taking a solid watermark, making it around 50% transparent and then placing it over another image. This will not be the use, but it is the easiest way to explain. The image I need to be transparent is ever changing and I do not have access to it. Plus, it is also made through php.
Link to comment
https://forums.phpfreaks.com/topic/32658-transparency-through-gd/
Share on other sites

Basically I tried to incorporate it into working code. I know this works when it is not using the blanding function. (I copied the code out of the manual since I gave up trying to write some new code. ))[code]<?php

function hide()
{
header("HTTP/1.0 404 Not Found");
echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n";
echo "<html><head>\n";
echo "<title>404 Not Found</title>\n";
echo "</head><body>\n";
echo "<h1>Not Found</h1>\n";
echo "<p>The requested URL " . $_SERVER['REQUEST_URI'] . " was not found on this server.</p>\n";
echo "</body></html>\n";
exit();
}

mysql_connect('localhost:3306', 'U', 'P');

mysql_select_db('gaia_image');

if(is_numeric($_GET['id']))
{
$result = mysql_query('SELECT * FROM image WHERE id = ' . $_GET['id']);
}
else
{
hide();
}

$row = mysql_fetch_array($result);

if ($row[0] == "")
{
hide();
}
elseif(strpos($_SERVER['REQUEST_URI'], 'image.php') != FALSE)
{
hide();
}
elseif($_GET['ext'] == "jpeg" || $_GET['ext'] == "jpg")
{
header('content-type: image/jpeg');
}
elseif($_GET['ext'] == "png")
{
header('content-type: image/png');
}
elseif($_GET['ext'] == "gif")
{
header('content-type: image/gif');
}
else
{
hide();
}


$sig = $row[1];
$n = $row[2];
$image = array(); $avi = array(); $avi_width = array(); $avi_height = array(); $dest_x = array(); $dest_y = array(); $a = array();

for ($i=0; $i<=$n; $i++)
{
array_push($a, $row[(($i * 3) + 3)]);
}

for ($i=0; $i<=$n-1; $i++)
{
array_push($avi, imagecreatefrompng($a[$i]));
array_push($avi_width, imagesx($avi[$i]));
array_push($avi_height, imagesy($avi[$i]));
}

array_push($image, imagecreatefromstring(file_get_contents($sig)));

if ($row[33] != "1")
{
array_push($image, imagecreatefromstring(file_get_contents($sig)));
}

$size = getimagesize($sig);

for ($i=0; $i<=$n; $i++)
{
$temp = "\$temp = " . $row[(($i * 3) + 4)] . ";";
eval($temp);
array_push($dest_x , $temp );
$temp = "\$temp = " . $row[(($i * 3) + 5)] . ";";
eval($temp);
array_push($dest_y , $temp );
}

for ($i=0; $i<=$n-1; $i++)
{
imagecopy($image[0], $avi[$i], $dest_x[$i] , $dest_y[$i], 0, 0, $avi_width[$i], $avi_height[$i]);
imagedestroy($avi[$i]);
}

if ($row[33] != "1")
{
imagecopy($image[0], $image[1], 0, 0, 0, 0, $size[0], $size[1]);
imagedestroy($image[1]);
}

if($_GET['ext'] == "jpeg" || $_GET['ext'] == "jpg")
{
imagejpeg($image[0]);
}
elseif($_GET['ext'] == "png")
{
imagepng($image[0]);
}
elseif($_GET['ext'] == "gif")
{
imagegif($image[0]);
}

imagegif(alphaOverlay($image[0], $image[0], $size[0], $size[1]);
imagedestroy($image[0]);


function alphaOverlay($destImg, $overlayImg, $imgW, $imgH)
{
  for($y=0;$y<$imgH;$y++)
  {
      for($x=0;$x<$imgW;$x++)
      {
          $ovrARGB = imagecolorat($overlayImg, $x, $y);
          $ovrA = ($ovrARGB >> 24) << 1;
          $ovrR = $ovrARGB >> 16 & 0xFF;
          $ovrG = $ovrARGB >> 8 & 0xFF;
          $ovrB = $ovrARGB & 0xFF;
         
          $change = false;
          if($ovrA == 0)
          {
              $dstR = $ovrR;
              $dstG = $ovrG;
              $dstB = $ovrB;
              $change = true;
          }
          elseif($ovrA < 254)
          {
              $dstARGB = imagecolorat($destImg, $x, $y);
              $dstR = $dstARGB >> 16 & 0xFF;
              $dstG = $dstARGB >> 8 & 0xFF;
              $dstB = $dstARGB & 0xFF;
             
              $dstR = (($ovrR * (0xFF-$ovrA)) >> 8) + (($dstR * $ovrA) >> 8);
              $dstG = (($ovrG * (0xFF-$ovrA)) >> 8) + (($dstG * $ovrA) >> 8);
              $dstB = (($ovrB * (0xFF-$ovrA)) >> 8) + (($dstB * $ovrA) >> 8);
              $change = true;
          }
          if($change)
          {
              $dstRGB = imagecolorallocatealpha($destImg, $dstR, $dstG, $dstB, 0);
              imagesetpixel($destImg, $x, $y, $dstRGB);
          }
             
      }
  }
  return $destImg;
}

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.