Location: PHPKode > scripts > text2image > text2image/class.txt2img.php
<?
class txt2img
{
    // default vars
    var $_aFont = array('size' => 3, 'margin' => 2);
    var $_sType = 'jpeg';
    var $_aAvailType = array('jpeg', 'png');
    var $_aColor = array('r' => 0, 'g' => 0, 'b' => 0);
    var $_aCoord = array('x' => 'center', 'y' => 'center');

    var $_sText;

    function txt2img( $data = array() )
    {
    	// set user image filename
    	$this->_sImage = $data['image'];

        // set user image type
        $data['type'] = strtolower( $data['type'] );
        // check type, if none - leave default
        if( in_array($data['type'], $this->_aAvailType) ) $this->_sType = $data['type'];

        // set text
		$this->_sText = ereg_replace( "[[:space:]]{2,}|\n", " ", $data['text'] );

        // set user color, if none - leave default
        if( eregi("^([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$", $data['color'], $rgb) )
        {
            $this->_aColor['r'] = hexdec( $rgb[1] );
            $this->_aColor['g'] = hexdec( $rgb[2] );
            $this->_aColor['b'] = hexdec( $rgb[3] );
        }

        // set font margin, if none - leave default
        if( intval( $data['margin'] ) >= 0 ) $this->_aFont['margin'] = $data['margin'];

        // set user fontsize, if none - leave default
        if( intval( $data['fontsize'] ) ) $this->_aFont['size'] = $data['fontsize'];

        // set graph font width and height
        $this->_aFont['w'] = ImageFontWidth ( $this->_aFont['size'] );
        $this->_aFont['h'] = ImageFontHeight( $this->_aFont['size'] );

        // set graph full text length
        $this->_aFont['len'] = $this->_aFont['w'] * strlen( $this->_sText );

        // set user coords of text, if none - leave default
        if( $data['x'] ) $this->_aCoord['x'] = strtolower( $data['x'] );
        if( $data['y'] ) $this->_aCoord['y'] = strtolower( $data['y'] );
    }

    function render()
    {
        $aSize = GetImageSize ( $this->_sImage );
        $aImage['w'] = $aSize[0];
        $aImage['h'] = $aSize[1];
        
        // create blank true color image for coorect font color control
        $im = imageCreateTrueColor( $aImage['w'], $aImage['h'] );  // if GD < 2 use imageCreate
        $font = ImageColorAllocate($im, $this->_aColor['r'], $this->_aColor['g'], $this->_aColor['b']);
        
    	if( array_pop (explode (".", $this->_sImage) ) == 'png' ) // detecting png input image
	    	$im2 = @ImageCreateFromPNG( $this->_sImage );
      
        elseif( ereg('^jp{1,2}', array_pop(explode(".", $this->_sImage))) ) // detecting jpeg input image
	    	$im2 = @ImageCreateFromJPEG( $this->_sImage );

        // copy data from input to blank
        ImageCopy ($im, $im2, 0, 0, 0, 0, $aImage['w'], $aImage['h']);
        ImageDestroy ( $im2 );

        if( !$im )
        {
            // create simple image with given text
			$im = imagecreate ( $this->_aFont['len'] + $this->_aFont['margin'] * 2 , $this->_aFont['h'] + $this->_aFont['margin'] * 2);
        	$bgc = imagecolorallocate ($im, 220, 220, 220);
        }
        
        if( $this->_aCoord['x'] == 'left' )  // align - left
			$aImage['x'] = $this->_aFont['margin'];
        elseif( $this->_aCoord['x'] == 'right' ) // align - right
            $aImage['x'] = $aImage['w'] - ($this->_aFont['len'] + $this->_aFont['margin']);
        else // center by default
            $aImage['x'] = floor( ($aImage['w'] / 2) - ($this->_aFont['len'] / 2) );

        if( $this->_aCoord['y'] == 'top' )  // valign - top
			$aImage['y'] = $this->_aFont['margin'];
        elseif( $this->_aCoord['y'] == 'bottom' )  // valign - bottom
            $aImage['y'] = $aImage['h'] - ($this->_aFont['h'] + $this->_aFont['margin']);
        else // center by default
            $aImage['y'] = floor( ($aImage['h'] / 2) - ($this->_aFont['h'] / 2) );

        ImageString($im, $this->_aFont['size'], $aImage['x'], $aImage['y'], $this->_sText, $font);
        
        // sending content-type
    	switch( $this->_sType )
        {
        	case 'jpeg':
                header("Content-Type: image/jpeg");
                ImageJPEG( $im );
            break;

            case 'png':
				header("Content-Type: image/png");
				ImagePNG( $im );
            break;
        }

        ImageDestroy ( $im );
	}
}
?>
Return current item: text2image