I have this php script http://ift.tt/1Dq6PQ4 ,How to add a random background image while creating a text image using php?
This is the result : [demo][1] , I want to do something like that http://appline.ro Thx for helping
<?php
$font_file = 'franklin.ttf';
$image_file = 'http://ift.tt/1yiyjXI';
// customizable variables
$font_size = 47 ; // font size in pts
$font_color = '#FF0000' ;
// clean up the input
if(empty($_GET['text'])) fatal_error('Error: No text specified.') ;
$text = html_entity_decode($_GET['text']);
//$text = floatval($text);
//$text = number_format($text, 1, '.', '') . '%';
if(empty($text))
fatal_error('Error: Text not properly formatted.') ;
// x and y for the bottom right of the text
// so it expands like right aligned text
$x_finalpos = 247;
$y_finalpos = 137;
// trust me for now...in PNG out PNG
$mime_type = 'image/png' ;
$extension = '.png' ;
$s_end_buffer_size = 4096 ;
// check for GD support
if(!function_exists('ImageCreate'))
fatal_error('Error: Server does not support PHP image generation') ;
// check font availability;
if(!is_readable($font_file)) {
fatal_error('Error: The server is missing the specified font.') ;
}
// create and measure the text
$font_rgb = hex_to_rgb($font_color) ;
$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
$text_width = abs($box[2]-$box[0]);
$text_height = abs($box[5]-$box[3]);
$image = imagecreatefrompng($image_file);
if(!$image || !$box)
{
fatal_error('Error: The server could not create this image.') ;
}
// allocate colors and measure final text position
$font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;
$image_width = imagesx($image);
$put_text_x = $image_width - $text_width - ($image_width - $x_finalpos);
$put_text_y = $y_finalpos;
// Write the text
imagettftext($image, $font_size, 0, $put_text_x, $put_text_y, $font_color, $font_file, $text);
header("Content-type: $mime_type");
ImagePNG($image) ;
ImageDestroy($image) ;
exit ;
/*
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 ;
}
?>
------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Floating window with tabs</title>
<style>
/*
This defines the workspace where i place the demo.
*/
#container {
text-align: left;
background: #FFF;
width: 865px;
margin: 20px auto;
padding: 20px;
border-left: 1px solid #CCC;
border-right: 1px solid #CCC;
-moz-box-shadow: 0px 0px 10px #BBB;
-webkit-box-shadow: 0px 0px 10px #BBB;
box-shadow: 0px 0px 10px #BBB;
}
</style>
</head>
<body>
<div id="container">
<?php
if($_POST["sending"]=="yes"){
if(strlen($_POST["text"]<50)){
echo '
<img id="imgFinal" src="coi.php?size=50&text='.$_POST["text"].'" />
';
}
else{
echo "The text is too large for my demo!! ";
}
}
else{
echo '
<img id="imgFinal" src="coi.php?size=50&text=."/>
';
}
?>
<form name="formulario" action="" method="post" class="contactoFormulario">
<div class="caja"><input type="text" name="text" />Ex:Adrian</div>
<button class="botonFormulario" type="submit" name="Submit" value="Enviar" />Generate image</button>
<input type="hidden" name="sending" value="yes" />
</form>
</div>
</body>
</html>
-------------------------------------------------------------------
<?php
require_once 'scrieinmagine.php';
$x = $_GET["x"];
$y = $_GET["y"];
$R = $_GET["r"];
$G = $_GET["g"];
$B = $_GET["b"];
$size = $_GET["size"];
$text = $_GET["text"];
$img = new textPainter('http://ift.tt/1FHVSZQ', $text, './Franklin.ttf', $size);
if(!empty($x) && !empty($y)){
$img->setPosition($x, $y);
}
if(!empty($R) && !empty($G) && !empty($B)){
$img->setTextColor($R,$G,$B);
}
$img->show();
?>
Aucun commentaire:
Enregistrer un commentaire