Jump to content
Fórum Script Brasil
  • 0

Borda no Texto com PHP e GD


LapisziN
 Share

Question

Boa tarde pessoal!

Eu tenho um código que arrumei na internet, dei uma mexida nele conforme a minha necessidade, porém eu não sei como colocar borda nestes textos que posso inserir ..

O código puxa uma imagem que eu posso escrever 4 linhas de texto nela, posso escolher uma fonte, o tamanho dela e sua cor, porém não sei como fazer uma borda neste texto..

Gostaria muito de pedir a ajuda de vocês!

Segue o código:

<?php    
// Set the enviroment variable for GD, useful for preventing GD related errors read Item 4 below.
putenv('GDFONTPATH=' . realpath('.'));
//Credits:
//Original code by Terri Ann "Writing text to Images with PHP found here: http://blog.ninedays.org/2007/11/29/writing-text-to-images-with-php/
//Version 1- Improvement by Codex-m at PHP developer.org to:
//1.) Write more than one text from the user to be parsed to the PHP by two GET statements
//2.) Allows merging of additional image with the ID picture template which is the persons' ID picture
//3.) Assign new variables for the new text and images to be added.
//4.) Added important line:
// putenv('GDFONTPATH=' . realpath('.')); 
// This will prevent "Warning: Could not find/open font problem resulting to Error: The server could not create this image."

// Check if the variables are not empty or else return an error.

// if((empty($_GET['linha01'])) &&(empty($_GET['linha02'])))    fatal_error('Error: No text specified.');

//Parse inputs using two GET statements and assigned to a PHP variable.
//Use HTML_Entity_decode to convert all HTML entities to its applicable characters.
//Use PHP trim command to remove spaces at the beginning and the end of the inputs.

$linha01 = trim(html_entity_decode($_GET['linha01']));
$linha02 = trim(html_entity_decode($_GET['linha02']));
$linha03 = trim(html_entity_decode($_GET['linha03']));
$linha04 = trim(html_entity_decode($_GET['linha04']));

//Validate if name is a string and Id number is numeric.
// if((!is_string($linha01))&&(!(is_numeric($linha02)))&&(!(is_numeric($linha03)))) fatal_error('Error: Text not properly formatted.');

//CUSTOMIZABLE: Declare and define the font file, font size, colors, iamge file name and template name
$font_file         = 'impact.ttf';
$font_size      = 45; // font size in pts
$font_color     = '#FFFFFF';
$image_file     = 'bart0001.png';
$stroke_color = imagecolorallocate($img, 255, 0, 0);
        
//CUSTOMIZABLE: Define x-y coordinates for the Name Text to be write to idtemplate.png
$x_finalpos1     = 550;
$y_finalpos1     = 70;
    
//CUSTOMIZABLE: Define x-y coordinates for the ID number text to be write to template.png
//Note: Coordinates are dependent on the size of the image template.    
$x_finalpos2     = 550;
$y_finalpos2     = 140;

//CUSTOMIZABLE: Define x-y coordinates for the ID number text to be write to template.png
//Note: Coordinates are dependent on the size of the image template.    
$x_finalpos3     = 550;
$y_finalpos3     = 515;
    
//CUSTOMIZABLE: Define x-y coordinates for the ID number text to be write to template.png
//Note: Coordinates are dependent on the size of the image template.    
$x_finalpos4     = 550;
$y_finalpos4     = 586;
    
//Declare image file extensions in JPG format.
$mime_type             = 'image/png';
$extension             = '.png';
    
//Check for server GD support
if(!function_exists('ImageCreate'))
fatal_error('Error: Server does not support PHP image generation');
    
//Check font file availability
if(!is_readable($font_file)) {
fatal_error('Error: The server is missing the specified font.');
}
    
//Define font colors
$font_rgb = hex_to_rgb($font_color);
    
//Define the bounding box coordinates for name text using Truefonttype font file specified    
$box1 = @ImageTTFBBox($font_size,0,$font_file,$linha01);
    
//Define the bounding box coordinates for ID number text using Truefonttype specified.
$box2 = @ImageTTFBBox($font_size,0,$font_file,$linha02);
    
//Define the bounding box coordinates for name text using Truefonttype font file specified    
$box3 = @ImageTTFBBox($font_size,0,$font_file,$linha03);

//Define the bounding box coordinates for name text using Truefonttype font file specified    
$box4 = @ImageTTFBBox($font_size,0,$font_file,$linha04);


//Define name width and height
$name_width1 = abs($box1[2]-$box1[0]);
$name_height1 = abs($box1[5]-$box1[3]);
    
//Define ID number width and height
$name_width2 = abs($box2[2]-$box2[0]);
$name_height2 = abs($box2[5]-$box2[3]);

//Define ID number width and height
$name_width3 = abs($box3[2]-$box3[0]);
$name_height3 = abs($box3[5]-$box3[3]);
    
//Define ID number width and height
$name_width4 = abs($box4[2]-$box4[0]);
$name_height4 = abs($box4[5]-$box4[3]);
    
//Create image from ID template PNG and assign to $image variable
$image =  imagecreatefrompng($image_file);

//Create image from persons ID picture PNG and assign it to $personid variable
$personid = imagecreatefrompng($image_file1);

//CUSTOMIZABLE: Merge the ID picture of the person to the ID template in a specific coordinates.
//You can read about imagecopymerge function manual here: http://php.net/manual/en/function.imagecopymerge.php
imagecopymerge($image, $personid, $stroke_color, 717, 259, 0, 0, 253, 300, 100);

//Check if the server cannot create the image and displays error.
if(!$image || !$box1 || !$box2 || !$box3 || !$box4)
{
fatal_error('Error: The server could not create this image.');
}
    
//Allocate color of the image and then measure the image width
$font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']);
$image_width = imagesx($image);
    
//Finalize the position of the name text on the generated image.
$put_text_x1 = ($image_width /2) - ($name_width1/2);
$put_text_y1 = $y_finalpos1;
    
//Finalize the position of the name text on the generated image.
$put_text_x2 = ($image_width /2) - ($name_width2/2);
$put_text_y2 = $y_finalpos2;

//Finalize the position of the name text on the generated image.
$put_text_x3 = ($image_width /2) - ($name_width3/2);
$put_text_y3 = $y_finalpos3;
    
//Finalize the position of the name text on the generated image.
$put_text_x4 = ($image_width /2) - ($name_width4/2);
$put_text_y4 = $y_finalpos4;
    

    
// Write the NAME text to the image
imagettftext($image, $font_size, 0, $put_text_x1,  $put_text_y1, $font_color, $font_file, $linha01);

//Write the ID NUMBER text to the image
imagettftext($image, $font_size, 0, $put_text_x2,  $put_text_y2, $font_color, $font_file, $linha02);
    
//Write the ID NUMBER text to the image
imagettftext($image, $font_size, 0, $put_text_x3,  $put_text_y3, $font_color, $font_file, $linha03);
    
//Write the ID NUMBER text to the image
imagettftext($image, $font_size, 0, $put_text_x4,  $put_text_y4, $font_color, $font_file, $linha04);
    
//Declare header content type in PNG
header('Content-type: ' . $mime_type);
    
//Output the generated PNG image with the text to the browser
imagepng($image);
    
//Destroy image to prevent memory leak and then exit.
ImageDestroy($image);
exit;

/*
The FATAL error function will 
attempt to create an image containing the error message given. 
if this works, the image is sent to the browser. if not, an error
is logged, and passed back to the browser as a 500 code instead.
*/
function fatal_error($message)
{
// send an image
if(function_exists('ImageCreate'))
{
$width = ImageFontWidth(5) * strlen($message) + 10;
$height = ImageFontHeight(5) + 10;
if($image = ImageCreate($width,$height))
{
$background = ImageColorAllocate($image,255,255,255);
$text_color = ImageColorAllocate($image,0,0,0);
ImageString($image,5,5,5,$message,$text_color);    
header('Content-type: image/png');
imagePNG($image);
ImageDestroy($image);
exit;
}
}
// send 500 code
header("HTTP/1.0 500 Internal Server Error");
print($message);
exit;
}
/* 
Decode an HTML hex-code into an array of R,G, and B values.
accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff 
*/    
function hex_to_rgb($hex) {
// remove '#'
if(substr($hex,0,1) == '#')
$hex = substr($hex,1);
// expand short form ('fff') color to long form ('ffffff')
if(strlen($hex) == 3) {
$hex = substr($hex,0,1) . substr($hex,0,1) .
substr($hex,1,1) . substr($hex,1,1) .
substr($hex,2,1) . substr($hex,2,1);
}
if(strlen($hex) != 6)
fatal_error('Error: Invalid color "'.$hex.'"');
// convert from hexidecimal number systems
$rgb['red'] = hexdec(substr($hex,0,2));
$rgb['green'] = hexdec(substr($hex,2,2));
$rgb['blue'] = hexdec(substr($hex,4,2));
return $rgb;
}
?>

Desde já agradeço!!

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Forum Statistics

    • Total Topics
      150.2k
    • Total Posts
      647.4k
×
×
  • Create New...