Re: [GD-DEVEL] Need help with GD Library
[email protected] (Steven Binion)
| Newsgroups | php.gd.devel |
|---|---|
| Message-ID | <[email protected]> |
Code as requested.
================================================================================
<?
if( ! isset($_POST) && isset($HTTP_POST_VARS) ) $_POST =&
$HTTP_POST_VARS;
if( ! isset($_GET) && isset($HTTP_GET_VARS) ) $_GET =& $HTTP_GET_VARS;
if( ! isset($_COOKIE) && isset($HTTP_COOKIE_VARS) ) $_COOKIE =&
$HTTP_COOKIE_VARS;
if( ! isset($_FILES) && isset($HTTP_POST_FILES) ) $_FILES =&
$HTTP_POST_FILES;
set_magic_quotes_runtime(0);
error_reporting( E_ERROR | E_WARNING | E_PARSE );
//define( "SITE_ROOT", getEnv( "DOCUMENT_ROOT" ) );
define( "SITE_ROOT", dirname(__FILE__) );
define( "PHP_SELF", getEnv( "SCRIPT_NAME" ) );
//define( "PROG_PATH", substr( PHP_SELF, 0, ( strlen( PHP_SELF ) -
strlen( basename( PHP_SELF ) ) ) ) );
define( "PROG_PATH", "/" );
define( "ICON_PATH", "pics/icons/" );
define( "BUTTON_PATH", "pics/buttons/" ) ;
define( "FONT_PATH", "pics/fonts/" ) ;
define( "ICON_FULL_PATH", SITE_ROOT . PROG_PATH . ICON_PATH );
define( "BUTTON_FULL_PATH", SITE_ROOT . PROG_PATH . BUTTON_PATH );
define( "FONT_FULL_PATH", SITE_ROOT . PROG_PATH . FONT_PATH );
$aIcons = getImgArray( ICON_FULL_PATH );
$aButtons = getImgArray( BUTTON_FULL_PATH );
$aFonts = getImgArray( FONT_FULL_PATH );
//----------------------------------------------
function submit( $var ){
global $_POST, $_GET ;
return ( strlen(trim($_POST[ $var ])) ) ? trim( $_POST[ $var ] )
: trim( $_GET[ $var ] );
}
//----------------------------------------------
function txt2img( $text, $buttongif, $lefticon, $righticon,
$font, $fontsize, $fontcolor,$top_margin, $bottom_margin, $left_margin,
$right_margin )
{
$url = "txt=" . urlEncode( $text ) . "&buttongif=" .
urlEncode($buttongif) .
"&lefticon=" . urlEncode($lefticon) . "&righticon=" .
urlEncode($righticon).
"&font=". urlEncode($font) . "&fontsize=".
urlEncode($fontsize) . "&fontcolor=" . urlEncode( $fontcolor ) .
"&top_margin=" . urlEncode( $top_margin ) .
"&bottom_margin=" . urlEncode( $bottom_margin ) .
"&left_margin=" . urlEncode( $left_margin ) .
"&right_margin=" . urlEncode( $right_margin ) ;
$url = ereg_replace( " ", "" , $url );
return "button.php?$url" ;
}
//----------------------------------------------
function getImgArray( $icon_path = "." )
{
$dpath = ereg_replace( SITE_ROOT.PROG_PATH, "", $icon_path );
$d = dir( $icon_path );
while( $entry = $d->read() )
{
if( (".." != $entry) && ("." != $entry) ) $aIcons[] =
$dpath . $entry ;
}
$d->close();
sort( $aIcons );
//for( $i = 0 ; $i < count( $aIcons ); $i++ ) print "$i<img
src='" . $aIcons[ $i ] . "' border=0 > " ;
return $aIcons ;
}
//----------------------------------------------
function displayFiles( $array, $name, $nCol = 6, $bText = False,
$bRnd = False )
{
if( $name ) :
$selvalue = submit($name);
endif;
print "<table border=0>\n" ;
$nCount = count( $array );
//$nCol = 6 ;
$nRow = ( $nCount % $nCol ) ? Floor( $nCount/$nCol ) + 1 :
Floor( $nCount/$nCol );
mt_srand( ( double ) microtime() * 1000000 );
$rnd = mt_rand( 0, $nCount-1 ) ;
for( $row = 0; $row < $nRow; $row ++ ):
$start = $row * $nCol + ( $row > 0 ? -1*$row : 0 ) ;
print "<tr>\n" ;
for( $j = $start ; $j < ($start + $nCol); $j++ ) :
$nIndex = $row + $j ;
if( $selvalue ) :
$checked = ( $selvalue == $array[ $nIndex ] ) ? "
checked " : "" ;
else:
$checked = ( $nIndex == $rnd && $bRnd ) ? " checked
" : "" ;
endif;
if( $array[ $nIndex ] ) :
print "<td><input type='radio' name='$name' value='"
. $array[ $nIndex ] . "' $checked ></td><td>" ;
//print ( !$bText ) ? "<img src='" . $array[ $nIndex
] . "' border=0 >" : "<div class='form_text'>" . $array[ $nIndex ] .
"</div>" ;
print ( !$bText ) ? "<img src='" . $array[ $nIndex ]
. "' border=0 >" : "<img src='showfont.php?font=" . $array[ $nIndex ] .
"' border=0>" ;
print "</td>\n" ;
endif;
endfor;
print "</tr>\n" ;
endfor;
print "</table>\n" ;
}
//----------------------------------------------
/*
array info of return imageTTFText, 4 point( x,y ) as follow:
(6,7) -------- (4,5)
Text Out Put
(0,1) -------- (2,3)
*/
//----------------------------------------------
class TTFButton
{
// image Handles
var $hNew ;
var $hButton;
var $hIconL;
var $hTxt;
var $hIconR;
var $sButton = "pics/buttons/btn_001_on.gif";
var $sIconL = "" ;
var $sIconR = "" ;
var $top_margin = 0 ;
var $bottom_margin = 0 ;
var $left_margin = 15 ;
var $right_margin = 15;
var $text = "Demo Text";
var $ttf_font = "pics/fonts/arial.ttf" ;
var $font_size = 12 ;
var $html_color = "#000000" ;
function TTFButton( $txt = "", $sButton = "" ) //, $sIconL = "",
$sIconR = "", $sTTFFont = "", $nFontSize = 12, $sHtmlColor = "#000000" )
{
if( $txt ) $this->text = ( ord(trim($txt)) > 127 ) ?
$this->gb2utf8($txt) : $txt;
if( $sButton ) $this->sButton = $sButton ;
/*
if( $sTTFFont ) $this->ttf_font = $sTTFFont ;
if( $nFontSize ) $this->font_size = $nFontSize ;
if( $sIconL ) $this->sIconL = $sIconL ;
if( $sIconR ) $this->sIconR = $sIconR ;
if( $sHtmlColor ) $this->html_color = trim($sHtmlColor) ;
*/
}
function ripper()
{
$this->hButton = $this->imageCreateFromImg( $this->sButton );
$this->txt2img() ;
$nLeft = $this->left_margin ? $this->left_margin : 6 ;
$nRight = $this->right_margin ? $this->right_margin : 6 ;
$nTxt = imageSX( $this->hTxt );
$nIconMargin = 4 ;
if( is_file( $this->sIconL ) ) :
$this->hIconL = $this->imageCreateFromImg( $this->sIconL ) ;
$nIconL = imageSX( $this->hIconL ) + $nIconMargin;
endif;
if( is_file( $this->sIconR ) ) :
$this->hIconR = $this->imageCreateFromImg( $this->sIconR ) ;
$nIconR = imageSX( $this->hIconR ) + $nIconMargin;
endif;
$newX = $nLeft + $nIconL + $nTxt + $nIconR + $nRight;
$newY = imageSY($this->hButton) ;
$this->hNew = imageCreate( $newX, $newY );
//int ImageCopy (resource dst_im, resource src_im, int dst_x,
int dst_y, int src_x, int src_y, int src_w, int src_h)
imageCopy( $this->hNew, $this->hButton, 0, 0, 0, 0,
$nLeft, $newY ); // Cut Button Left
imageCopy( $this->hNew, $this->hButton, $nLeft, 0, $nLeft,
0, $nIconL+$nTxt+$nIconR, $newY ); // Background of IconL + Text + IconR
if( $this->hIconL ) :
$dst_y = $this->dst_y( $this->hIconL );
imageCopy( $this->hNew, $this->hIconL, $nLeft,
$dst_y, 0, 0, $nIconL-$nIconMargin, imageSY($this->hIconL)
); // Copy IconL
endif;
$dst_y = $this->dst_y( $this->hTxt );
imageCopy( $this->hNew, $this->hTxt, $nLeft+$nIconL,
$dst_y, 0, 0, $nTxt, imageSY($this->hTxt) ); // Copy Text
if( $this->hIconR ) :
$dst_y = $this->dst_y( $this->hIconR );
imageCopy( $this->hNew, $this->hIconR,
$nLeft+$nIconL+$nTxt+$nIconMargin, $dst_y, 0, 0, $nIconR,
imageSY($this->hIconR) ); // Copy IconR
endif;
imageCopy( $this->hNew, $this->hButton,
$nLeft+$nIconL+$nTxt+$nIconR, 0, imageSX($this->hButton)-$nRight, 0,
$nRight, $newY ); // Cut Button Right
}
function dst_y( $hImg )
{
$valid_height = imageSY( $this->hButton ) - $this->top_margin -
$this->bottom_margin ;
$dst_y = $this->top_margin+Floor( ( $valid_height -
imageSY($hImg) ) / 2 );
return $dst_y ;
}
function imageCreateFromImg( $filename )
{
$split = split( "\.", basename( trim( $filename ) ) );
$ext = $split[ count($split) - 1 ] ;
switch( strtolower( $ext ) )
{
case "gif" :
return imageCreateFromGif( $filename );
break;
case "png" :
return imagecreatefrompng( $filename );
break;
case "jpg" :
case "jpeg" :
return imagecreatefromjpeg( $filename );
break;
}
}
function show()
{
$this->ripper();
$split = split( "\.", basename( trim( $this->sButton ) ) );
$ext = $split[ count($split) - 1 ] ;
switch( strtolower( $ext ) )
{
case "gif" :
Header( "Content-Type: image/gif" );
imageGif( $this->hNew ) ;
break;
case "png" :
Header("Content-type: image/png");
ImagePNG($this->hNew);
break;
case "jpg" :
case "jpeg" :
Header("Content-type: image/jpeg");
ImageJPEG($this->hNew );
break;
}
$this->cleanup();
}
function cleanup()
{
imageDestroy($this->hNew );
imageDestroy( $this->hTxt );
imageDestroy( $this->hButton );
if( $this->hIconL ) imageDestroy( $this->hIconL );
if( $this->hIconR ) imageDestroy( $this->hIconR );
//print "New Y = " . imageSY( $this->hNew ) . " TXT Y = " .
imageSY($this->hTxt) . " /2 = " .
Floor((imageSY($this->hNew)-imageSY($this->hTxt))/2);
}
function txt2img()
{
$hImg_temp = imageCreate( 10, 10 );
$txt_color = $this->txtcolor( $hImg_temp );
$p4 = imageTTFText( $hImg_temp, $this->font_size, 0, 0, 0,
$txt_color, $this->ttf_font, $this->text );
imageDestroy( $hImg_temp );
$margin = 4 ;
$x_size = ($p4[2]-$p4[0]) + $margin ;
$y_size = $p4[1]-$p4[7] + $margin ;
$this->hTxt = imageCreate( $x_size, $y_size );
//$hTxt = imageCreate( 600, 300 );
$bg_color = imageColorAllocate( $this->hTxt, 200, 200, 200 );
imageFill( $this->hTxt, 0, 0, $bg_color );
imageColorTransparent( $this->hTxt, $bg_color );
$x = 0+floor($margin/2) ;
$y = $y_size-floor($margin/2);
$txt_color = $this->txtcolor( $this->hTxt );
imageTTFText( $this->hTxt, $this->font_size, 0, $x, $y,
$txt_color, $this->ttf_font, $this->text );
//$txt_shadow = imageColorAllocate( $hTxt, 100, 100, 100 );
//imageTTFText( $hTxt, $this->font_size, 0, $x-1, $y-1,
$txt_shadow, $this->ttf_font, $this->text );
//return $hTxt ;
}
function txtcolor( $hImg_temp )
{
$hexcolor = ( "#" == substr( $this->html_color, 0, 1 ) ) ?
substr( $this->html_color, 1, strlen($this->html_color)-1 ) . "000000" :
$this->html_color . "000000" ;
return imageColorAllocate( $hImg_temp,
hexdec(substr($hexcolor,0,2)) , hexdec(substr($hexcolor,2,2)),
hexdec(substr($hexcolor,4,2)) );
}
}
function ImageOut( $im = "" )
{
if (function_exists("imagejpeg")) {
$im ? ImageJPEG($im, "", 100) : Header("Content-type: image/jpeg");
}
elseif (function_exists("imagepng")) {
$im ? ImagePNG($im) : Header("Content-type: image/png");
}
elseif (function_exists("imagegif")) {
$im ? ImageGIF($im) : Header("Content-type: image/gif");
}
elseif (function_exists("imagewbmp")) {
$im ? ImageWBMP($im) : Header("Content-type: image/vnd.wap.wbmp");
}
else
die("No image support in this PHP server");
}
?>
======================================================================
Pierre wrote:
> On 3/10/07, Steven Binion <[email protected]> wrote:
>> Well I didn't write the code and it isn't modified in anyway. hope this
>> will be sufficient. https://sourceforge.net/projects/phpbutton/
>
> Not really, I can help if you show me some code. It is also not that
> hard to write such script yourself.
>
> Check out our PHP FAQ here:
>
> http://www.libgd.org/FAQ_PHP, "Where can I find an example of PHP
> programming with gd?" links to some good resources.
>
> There is even some books about PHP and gd, the last one being:
>
> http://www.amazon.com/exec/obidos/ASIN/839166516X
>
>
> --Pierre