Tutorials: Creating Images with PHP

Status
Not open for further replies.

Cooper

Banned
Banned
1,614
2009
191
0
The fundamentals of creating an image in PHP using GD Library support. This tutorial shows you how to create a simple image with welcome text and a name to follow.

[youtube]YGHL5i0Fnnw[/youtube]
 
2 comments
I made this for Hawk a few months ago. Some may find it useful

PHP:
<?php
/* 
Download for instructions and how to use...
http://rapidshare.com/files/400777995/php-image.zip
*/

// lets get the text to display
if (isset($_GET['id'])) {
	$text = $_GET['id'];
}
// here you can put the default message if their's an error displaying the one in the url.
else { $text = "Default message here";}

// This is the default background image. Like a template without the text showing on it.
$bg = "bg/default.jpg";
// obvious enough. Change the size. It's height is in pixels.
$size = "30";
// just enter the hex color you want. You can use something like http://www.2createawebsite.com/build/hex-colors.html to get the color you want.
$color = "A03B3B";
// change the path to where you upload the font. It has to be .ttf but you can google and find which ever suits.
$font = "font/arial.ttf";


$im = imagecreatefromjpeg($bg);
Header("Content-type: image/png");

// This convert the hex color to RGB
$color = imagecolorallocate($im, hexdec('0x' . $color{0} . $color{1}), hexdec('0x' . $color{2} . $color{3}), hexdec('0x' . $color{4} . $color{5}));

//------ this is a fancy bit you can use if you want the text centered --------
for(;;){
	// just change the figures below to where you want the exact center point of the text to be from the top left corner
	$image_width = '400';
	$image_height = '62';
	list($left_x, $bottom_y, $right_x, , , $top_y) = imagettfbbox($size, 0, $font, $text); 
	$text_width = $right_x - $left_x;
	$text_height = $top_y - $bottom_y; 
	if($image_width > $text_width+5){
		break;
	}

	$size = $size - .5; 
	if($size == 1){ 
		die('Script not responding to decreasing font size, in other words: try using less letters.'); 
	} 
} 
$hpadding = ($image_width - $text_width)/2; 
$vpadding = ($image_height - $text_height)/2;

//-------- End of fancy centered text bit, Delete it all if you don't want it --------

// If you not using and have deleted the fancy centered bit above you can uncomment the below two lines to put where you want to give the location of the text.
// $hpadding = '200';
// $vpadding = '100';

// Next we add the text
// FORMAT IS: image, size, angle, x distance, y distance, color, font, text string
imagettftext($im, $size, 0, $hpadding, $vpadding, $color, $font, $text);

imagepng($im);
imagedestroy($im);
?>
It might by an idea to stripslashes() or mysql_real_escape_string() the $_GET for security depending on how or where your using it.

Download it all here http://rapidshare.com/files/400777995/php-image.zip
 
Status
Not open for further replies.
Back
Top