Showing posts with label PHP Captcha. Show all posts
Showing posts with label PHP Captcha. Show all posts

Wednesday, July 24, 2019

Dynamic Image Dimension based on Font size in PHP


Working with images and text, we need to set image size based on font size is very useful for dynamic image processing. like CAPTCHA and text images. if you looking for PHP script to set image size based on font size here is the solution for you.

<?php


if($_GET['fontsize']!="" and is_numeric($_GET['fontsize']))
$size=$_GET['fontsize'];
else
$size=20;


define("F_SIZE", $size);
define("F_ANGLE", 0);
define("F_FONT", "verdana.ttf");


$text = "W3 Lessons";

$Leading=0;
$W=0;
$H=0;
$X=0;
$Y=0;

$_bx = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$s = preg_split("/\n]+/", $text);  // Array of lines
$nL = count($s);  // Number of lines

$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W;
$H = ($H==0)?abs($_bx[5]-$_bx[3])+2+($nL>1?($nL*$Leading):0):$H;

$W*=1.05;
$H*=1.5;

$img = @imagecreate($W+8, $H) or die("Cannot Initialize new GD image stream");

$white = imagecolorallocate($img, 255,255,255);
$txtColor = imagecolorallocate($img, 29,56,131);

// Adjust padding right:
$W+=8;
$bgwhite = imagecolorallocatealpha($img,238,238,238,0);
imagefilledrectangle($img, 0, 0,$W,$H, $bgwhite);

$alpha = "".range("a", "z");
$alpha = $alpha.strtoupper($alpha).range(0, 9);

// Use the string to determine the height of a line
$_b = imageTTFBbox(F_SIZE, F_ANGLE, F_FONT, $alpha);
$_H = abs($_b[5]-$_b[3]);
$__H = 0;

// Use the string to determine the width of a line
$_b = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$_W = abs($_b[2]-$_b[0]);

// Final width and height
$_X = abs($W/2)-abs($_W/2);
$__H += $_H;

imagettftext($img, F_SIZE, F_ANGLE, $_X, $__H, $txtColor, F_FONT, $text);

header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
// END EMAIL IMAGE  
exit();
?>

Monday, October 10, 2011

Simple captcha with PHP

Simple captcha with PHP, We seen how to create a Simple captcha . Now we look how to integrate that simple Captcha with HTML Form. Now I am going to show you a Newsletter Subscriber with our Simple Captcha.

Simple captcha with PHP and Live Example with Newsletter
  • Show an image Captcha without any dependancies, such as Class, Function etc.,
  • Customizable code length, character sets, and Unicode support
  • TTF font support
  • Easily  customization: Background and Foreground colors
Kindly view  How to Create a Simple Captcha in PHP before the Newsletter Integration of this Capcha

Let me create a HTML form look like this