Here are two more scripts that take advantage of GD, I've posted these before but I figured i'll post them here to keep track of them.
Captcha Generator
Code:
<?php
function RandomStringGenerator($length){
global $string;
$pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
if(empty($length)){
$length = "10";
}
for($i=0; $i<$length; $i++){
$string .= $pattern{rand(0,61)};
}
return $string;
}
function CaptchaGenerator(){
global $string;
RandomStringGenerator();
header("Content-type: image/png");
$im = @imagecreate(100, 50)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 255, 0, 0);
imagestring($im, 10, 5, 5, $string, $text_color);
imagepng($im);
imagedestroy($im);
}
CaptchaGenerator();
?>
Signature Rotator/Banner Rotator:
Code:
<?php
$folder = './';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}
if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
file_exists( $folder.$imageInfo['basename'] )
) {
$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}
if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}
?>
To add a border to text:
Code:
/**
* Writes the given text with a border into the image using TrueType fonts.
* @author John Ciacia < John@extreme-hq.com >
* @param image An image resource.
* @param size The font size.
* @param angle The angle in degrees to rotate the text.
* @param x Upper left corner of the text.
* @param y Lower left corner of the text.
* @param textcolor This is the color of the main text.
* @param strokecolor This is the color of the text border.
* @param fontfile The path to the TrueType font you wish to use.
* @param text The text string in UTF-8 encoding.
* @param px Number of pixels the text border will be.
* @return Returns an array with 8 elements representing four points making the bounding
* box of the text. The order of the points is lower left, lower right, upper right,
* upper left. The points are relative to the text regardless of the angle, so
* "upper left" means in the top left-hand corner when you see the text horizontally.
* @see http://us.php.net/manual/en/function.imagettftext.php
* @see http://forum.codecall.net
* @see http://www.extreme-hq.com
*/
function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {
for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
$bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}
Usage:
Code:
$img = imagecreatefrompng("/home/john/Desktop/test.png");
$font_color = imagecolorallocate($img, 0, 0, 0);
$stroke_color = imagecolorallocate($img, 255, 0, 0);
$font = "/home/john/Desktop/ABSTRACT.TTF";
$txt = "This is a test..";
$px = 2;
imagettfstroketext($img, 10, 0, 10, 50, $font_color, $stroke_color, $font, $txt, $px);
Enjoy!
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum