Jump to content

Archived

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

gb115b

GD Transparency - Please Help!

Recommended Posts

hello,

i've been trying to produce nicely graphics with nice anti-aliased edges (provided by a proper alpha channel using .PNG files)

i've got two files which you can see at [a href=\"http://g.is-a-geek.com/camden101\" target=\"_blank\"]http://g.is-a-geek.com/camden101[/a]

my titleimage

[code]<?php

$strText = "Camden 101";
$strFont = "assets/ITCEDSCR.TTF";
$intFontSize = 144;

$arrSize = imageTTFBBox($intFontSize, 0, $strFont, $strText);
$image = imageCreateTrueColor((abs($arrSize[4]) + abs($arrSize[0])), (abs($arrSize[5]) + abs($arrSize[1])));

imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);

$colorTransparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
$colorText   = imageColorAllocate($image, 51, 204, 255);

imagefill($image, 0, 0, $colorTransparent);
imagettftext($image, $intFontSize, 0, 0, abs($arrSize[5]), $colorText, $strFont, $strText);

// create an interlaced image for better loading in the browser
imageInterlace($image, 1);

//header("Content-type:  image/png");
imagePNG($image, "assets/title.png");
imagedestroy($image);

$strUserAgent = $_SERVER['HTTP_USER_AGENT'];

if (strpos($strUserAgent, "MSIE 6") OR strpos($strUserAgent, "MSIE 5.5"))
{
  echo "<img src=\"assets\\spacer.gif\" width=\"".(abs($arrSize[4]) + abs($arrSize[0]))."\" height=\"".(abs($arrSize[5]) + abs($arrSize[1]))."\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader( src='assets/title.png', sizingMethod='image');\" alt=\"".$strText."\" border=\"0\" />";
} else
{
  echo "<img src=\"assets\\title.png\" alt=\"".$strText."\" border=\"0\" />";
}

?>[/code]
and a menu navigation

[code]<?php

$arrayMenuItems['Item Name'][0] = "News";
$arrayMenuItems['Item Link'][0] = "news.php";
$arrayMenuItems['Item Name'][1] = "Events";
$arrayMenuItems['Item Link'][1] = "events.php";
$arrayMenuItems['Item Name'][2] = "Reviews";
$arrayMenuItems['Item Link'][2] = "reviews.php";
$arrayMenuItems['Item Name'][3] = "Forums";
$arrayMenuItems['Item Link'][3] = "forums.php";
$arrayMenuItems['Item Name'][4] = "About";
$arrayMenuItems['Item Link'][4] = "about.php";

$strFont = "assets/BauHS93.TTF";
$intFontSize = 20;
$intAngleRotation = 30;

   function imageSmoothCircle( &$img, $cx, $cy, $cr, $color ) {
       $ir = $cr;
       $ix = 0;
       $iy = $ir;
       $ig = 2 * $ir - 3;
       $idgr = -6;
       $idgd = 4 * $ir - 10;
       $fill = imageColorExactAlpha( $img, $color[ 'R' ], $color[ 'G' ], $color[ 'B' ], 0 );
       imageLine( $img, $cx + $cr - 1, $cy, $cx, $cy, $fill );
       imageLine( $img, $cx - $cr + 1, $cy, $cx - 1, $cy, $fill );
       imageLine( $img, $cx, $cy + $cr - 1, $cx, $cy + 1, $fill );
       imageLine( $img, $cx, $cy - $cr + 1, $cx, $cy - 1, $fill );
       $draw = imageColorExactAlpha( $img, $color[ 'R' ], $color[ 'G' ], $color[ 'B' ], 42 );
       imageSetPixel( $img, $cx + $cr, $cy, $draw );
       imageSetPixel( $img, $cx - $cr, $cy, $draw );
       imageSetPixel( $img, $cx, $cy + $cr, $draw );
       imageSetPixel( $img, $cx, $cy - $cr, $draw );
       while ( $ix <= $iy - 2 ) {
           if ( $ig < 0 ) {
               $ig += $idgd;
               $idgd -= 8;
               $iy--;
           } else {
               $ig += $idgr;
               $idgd -= 4;
           }
           $idgr -= 4;
           $ix++;
           imageLine( $img, $cx + $ix, $cy + $iy - 1, $cx + $ix, $cy + $ix, $fill );
           imageLine( $img, $cx + $ix, $cy - $iy + 1, $cx + $ix, $cy - $ix, $fill );
           imageLine( $img, $cx - $ix, $cy + $iy - 1, $cx - $ix, $cy + $ix, $fill );
           imageLine( $img, $cx - $ix, $cy - $iy + 1, $cx - $ix, $cy - $ix, $fill );
           imageLine( $img, $cx + $iy - 1, $cy + $ix, $cx + $ix, $cy + $ix, $fill );
           imageLine( $img, $cx + $iy - 1, $cy - $ix, $cx + $ix, $cy - $ix, $fill );
           imageLine( $img, $cx - $iy + 1, $cy + $ix, $cx - $ix, $cy + $ix, $fill );
           imageLine( $img, $cx - $iy + 1, $cy - $ix, $cx - $ix, $cy - $ix, $fill );
           $filled = 0;
           for ( $xx = $ix - 0.45; $xx < $ix + 0.5; $xx += 0.2 ) {
               for ( $yy = $iy - 0.45; $yy < $iy + 0.5; $yy += 0.2 ) {
                   if ( sqrt( pow( $xx, 2 ) + pow( $yy, 2 ) ) < $cr ) $filled += 4;
               }
           }
           $draw = imageColorExactAlpha( $img, $color[ 'R' ], $color[ 'G' ], $color[ 'B' ], ( 100 - $filled ) );
           imageSetPixel( $img, $cx + $ix, $cy + $iy, $draw );
           imageSetPixel( $img, $cx + $ix, $cy - $iy, $draw );
           imageSetPixel( $img, $cx - $ix, $cy + $iy, $draw );
           imageSetPixel( $img, $cx - $ix, $cy - $iy, $draw );
           imageSetPixel( $img, $cx + $iy, $cy + $ix, $draw );
           imageSetPixel( $img, $cx + $iy, $cy - $ix, $draw );
           imageSetPixel( $img, $cx - $iy, $cy + $ix, $draw );
           imageSetPixel( $img, $cx - $iy, $cy - $ix, $draw );
       }
   }



$strTestStringDescenders = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!£$%^&*()-=[];'#,./_+{}:@~<>?\|¬¦`";
$strTestStringNoDescenders = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhiklmnostuvwxz1234567890";
$arrayTotalSize = imageTTFBBox($intFontSize, 0, $strFont, $strTestStringDescenders);

$FontHeight = abs($arrayTotalSize[7]-$arrayTotalSize[1]);
$TotalHeight = $FontHeight * Count($arrayMenuItems['Item Name']);

$arrayTotalSize = imageTTFBBox($intFontSize, 0, $strFont, $strTestStringNoDescenders);

$FontBase = abs($arrayTotalSize[7]-$arrayTotalSize[1]);

$TotalWidth = 0;

for ($i = 0; $i < Count($arrayMenuItems['Item Name']); $i++)
{

  $arrayTotalSize = imageTTFBBox($intFontSize, 0, $strFont, $arrayMenuItems['Item Name'][$i]);
    
    if (abs($arrayTotalSize[2]-$arrayTotalSize[0]) > $TotalWidth)
    {
          $TotalWidth = abs($arrayTotalSize[2]-$arrayTotalSize[0]);
    }
    
}

$ImageSize = 2 * ceil(sqrt(($TotalHeight*$TotalHeight)/4 + ($TotalWidth*$TotalWidth)/4));
$image = imageCreateTrueColor($ImageSize, $ImageSize);

imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);

$colorTransparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
$colorBadge = imagecolorallocate($image, 255, 51, 51);
$colorText   = imagecolorallocate($image, 255, 255, 255);


imagefill($image, 0, 0, $colorTransparent);
imagefilledellipse($image, ($ImageSize/2), ($ImageSize/2), ($ImageSize-1), ($ImageSize-1), $colorBadge);
//imageSmoothCircle( $image, ($ImageSize/2), ($ImageSize/2), ($ImageSize/2)-1, array( 'R' => 0xFF, 'G' => 0x33, 'B' => 0x33 ) );

echo "<map name=\"navigation\">";

for ($i = 0; $i < Count($arrayMenuItems['Item Name']); $i++)
{

    $arrayTextSize = imageTTFBBox($intFontSize, 0, $strFont, $arrayMenuItems['Item Name'][$i]);

    $radAngleRotation = deg2rad($intAngleRotation);
    
    $xOffset = ($ImageSize - abs($arrayTextSize[2]-$arrayTextSize[0]))/2;
    $yOffset = ($ImageSize - $TotalHeight)/2 + $FontBase + $i*$FontHeight;
    
    $xRotatedOffset = ($ImageSize/2) + (cos($radAngleRotation) * ($xOffset-($ImageSize/2))) - (sin($radAngleRotation) * (($ImageSize/2)-$yOffset));
    $yRotatedOffset = ($ImageSize/2) - (sin($radAngleRotation) * ($xOffset-($ImageSize/2))) - (cos($radAngleRotation) * (($ImageSize/2)-$yOffset));
    
  $arrayTextSize = imagettftext($image, $intFontSize, $intAngleRotation, $xRotatedOffset, $yRotatedOffset, $colorText, $strFont, $arrayMenuItems['Item Name'][$i]);
    
  $arrayMenuItems['Lower Left X'][$i] = $arrayTextSize[0];
  $arrayMenuItems['Lower Left Y'][$i] = $arrayTextSize[1];
  $arrayMenuItems['Lower Right X'][$i] = $arrayTextSize[2];
  $arrayMenuItems['Lower Right Y'][$i] = $arrayTextSize[3];
  $arrayMenuItems['Upper Right X'][$i] = $arrayTextSize[4];
  $arrayMenuItems['Upper Right Y'][$i] = $arrayTextSize[5];
  $arrayMenuItems['Upper Left X'][$i] = $arrayTextSize[6];
  $arrayMenuItems['Upper Left Y'][$i] = $arrayTextSize[7];

    echo "<area shape=\"poly\" coords=\"";
    echo $arrayMenuItems['Lower Left X'][$i];
    echo ",";
  echo $arrayMenuItems['Lower Left Y'][$i];
    echo ",";
  echo $arrayMenuItems['Lower Right X'][$i];
    echo ",";
  echo $arrayMenuItems['Lower Right Y'][$i];
    echo ",";
  echo $arrayMenuItems['Upper Right X'][$i];
    echo ",";
  echo $arrayMenuItems['Upper Right Y'][$i];
    echo ",";
  echo $arrayMenuItems['Upper Left X'][$i];
    echo ",";
  echo $arrayMenuItems['Upper Left Y'][$i];    
    echo "\" href=\"";
    echo $arrayMenuItems['Item Link'][$i];
    echo "\" alt=\"\">";
    
}
echo "</map>";

// create an interlaced image for better loading in the browser
imageInterlace($image, 1);

//header("Content-type:  image/png");
imagePNG($image, "assets/navigation.png");

imagedestroy($image);

$strUserAgent = $_SERVER['HTTP_USER_AGENT'];

if (strpos($strUserAgent, "MSIE 6") OR strpos($strUserAgent, "MSIE 5.5"))
{
  echo "<img src=\"assets\\spacer.gif\" width=\"".$ImageSize."\" height=\"".$ImageSize."\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader( src='assets/navigation.png', sizingMethod='image');\" alt=\"\" usemap=\"#navigation\" ismap border=\"0\" />";
} else
{
  echo "<img src=\"assets\\navigation.png\" alt=\"\" usemap=\"#navigation\" ismap border=\"0\" />";
}

?>[/code]

the transparency seems to work nicely at the edges...but not where letters collide....

can anyone help me figure out whats happening???

maybe my config is wrong [a href=\"http://g.is-a-geek.com/phpinfo.php\" target=\"_blank\"]http://g.is-a-geek.com/phpinfo.php[/a]

Share this post


Link to post
Share on other sites

×

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.