如何使用imagettftext()函数将边框添加到文本框(图像框)

时间:2014-05-20 09:54:10

标签: javascript php css imagettftext

这里我尝试为图像添加边框,该图像使用从用户输入文本转换为图像 imagettftext()function.Any身体有任何想法我将如何实现。

演示链接: - Click Here

寻找像波纹管快照一样的预期输出

expected output

Bellow是我用来将文本转换为图像的示例PHP代码。

<?php session_start();
  header("Content-type: image/png");
$myArray = explode(',', $_GET['img']);  
$fontarray = explode(',' , $_GET['fontsize']);   
$align =$_GET['align'];


####################### BEGIN USER EDITS #######################   
$imagewidth = 1000;
$imageheight = 1000;
$fontangle = "0";
$font = "Outlinesten_6.ttf";
$backgroundcolor = "FFFFFF";
$textcolor = "#000000";

### Convert HTML backgound color to RGB
if( @eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $backgroundcolor, $bgrgb ) )
{$bgred = hexdec( $bgrgb[1] );   $bggreen = hexdec( $bgrgb[2] );   $bgblue = hexdec( $bgrgb[3] );}

### Convert HTML text color to RGB
if( @eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
{$textred = hexdec( $textrgb[1] );   $textgreen = hexdec( $textrgb[2] );   $textblue = hexdec( $textrgb[3] );}

### Create image
$im = imagecreate( $imagewidth, $imageheight );

### Declare image's background color
$bgcolor = imagecolorallocate($im, $bgred,$bggreen,$bgblue);

### Declare image's text color
$fontcolor = imagecolorallocate($im, $textred,$textgreen,$textblue);
  if($align=='center'  )
         {
### Get exact dimensions of text string

       $ycord=0;

//$ycord = ($imageheight/2)-($textheight/2)*2 ; 
       $testcount=0;
### Declare completed image with colors, font, text, and text location      
$count=count($myArray); 
       $totalprice=0;
$testnewcount=0;
for($i=0;$i<$count;$i++)
{
        $newcount=count($fontarray); 

        for($j=0;$j<$newcount;$j++)

{


    if($j==$i)
    {



            //$box = imageTTFBbox(50,$fontangle,$font,'test');  
       $box = imageTTFBbox($fontarray[$j],$fontangle,$font,$myArray[$i]);    

### Get width of text from dimensions
$textwidth = abs($box[4] - $box[0]);      

### Get height of text from dimensions
$textheight = abs($box[5] - $box[1]);

### Get x-coordinate of centered text horizontally using length of the image and length of the text
//$xcord = ($imagewidth/2)-($textwidth/2)-2;


        $price= GetPrice($fontarray[$j],$myArray[$i]);


        // $stringnewstr='';
     //   $string=$myArray[$i];

$totalprice=$totalprice+$price;
### Get y-coordinate of centered text vertically using height of the image and height of the text
//$ycord = ($imageheight/2)+($textheight/2); 

        // if($align=='center')
        // {
           $xcord = ($imagewidth/2)-($textwidth/2)-2;   

       // $ycord =(( ($imageheight/2)+($textheight/2))  )+$testcount; 
        // $testcount=$testcount+65;  
        if($i==0)
        {
             $testnewcount= $fontarray[$j];      
        }
              //$testcount=$testcount+$fontarray[$j]/2;    
            if($testnewcount<$fontarray[$j])
            {
                  $testcount=$testcount+$fontarray[$j]/2;

                  $ycord =(( ($imageheight/2)+($textheight/2))  )+$testcount;  

            }
            else
            {
                $ycord =(( ($imageheight/2)+($textheight/2))  )+$testcount;  


            }

         $testcount=$testcount+$fontarray[$j]; 

         $testnewcount= $fontarray[$j];     
//$ycord = ($imageheight/2)+($textheight/2);
/*
for($i=0;$i<$count;$i++)
{
        $newcount=count($fontarray); 

        for($j=0;$j<$newcount;$j++)

{

    if($j==$i)
    {     */
   //  $xcord=$xcord+2;
 //  $ycord=$ycord+100;
  // imagettftext ( $im, $fontarray[$j], $fontangle, $xcord, $ycord, $fontcolor, $font, $myArray[$i] );       
    //    imagettftext ( $im, $fontarray[$j], $fontangle, $xcord, $ycord, $fontcolor, $font, $ycord );   

    imagettftext ( $im, $fontarray[$j], $fontangle, $xcord, $ycord, $fontcolor, $font, $myArray[$i] );   
         }

         }

 } 
   //imagettftext ( $im, 50, $fontangle, $xcord, $ycord, $fontcolor, $font, $totalprice );  
   $totalprice; 

}
$_SESSION['finalprice']=$totalprice;


$html=imagepng($im);
    //$test="saas";
### Close the image
imagedestroy($im);   

?>

0 个答案:

没有答案