How to remove reference to ttf file after using imagettftext? - php

I'm currently developing a captcha system for the site I'm currently developing for a school project.. here: http://rjtestsystem.atwebpages.com/
Here's the code, err, prototype I came up with so far:
<?php
if (isset($_GET['METHOD']) && $_GET['METHOD']=='captcha')
{ session_start();
$code=rand(1000,9999);
$_SESSION["code"] = $code;
session_write_close();
$font = rand(1,4).".ttf";
if ($font == "1.ttf")
$font_size = 20;
else if ($font == "2.ttf")
$font_size = 25;
else if ($font == "3.ttf")
$font_size = 30;
else if ($font == "4.ttf")
$font_size = 35;
$angle = 0;
$width = 120;
$height = 60;
$bounding_box = imagettfbbox($font_size, $angle, $font, $code);
$textwidth = abs($bounding_box[4] - $bounding_box[0]);
$textheight = abs($bounding_box[5] - $bounding_box[1]);
$x = ($width - $textwidth) / 2;
$y = (($height*3/4) + $textheight) / 2;
$image = imagecreatetruecolor($width, $height);
$text_background = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 230, 230, 230);
imagefill($image, 0, 0, $text_background);
imageline($image, 0, $y-rand(0,10), $width, $y-rand(0,10), $color);
imageline($image, 0, $y, $width, $y, $color);
imageline($image, 0, $y+rand(0,10), $width, $y-rand(0,20), $color);
imageline($image, $x+rand(0,$width/2), 0, $x+rand($width/2,$width), $height, $color);
imageline($image, $x+rand(0,$width/2), $height, $x+rand($width/2,$width), 0, $color);
for($i=0;$i<400;$i++)
{ imagesetpixel($image,rand()%$width,rand()%$height,$color);
}
imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $code);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
exit();
}
?>
<img src = "<?php echo $_SERVER['PHP_SELF']; ?>?METHOD=captcha">
The code seems to be working fine, but when I try to move the files elsewhere, the system would give me a message that the file is being used by another program. It seems that the references to the TTF files are still there even after executing the code. I've tried to look for a way to stop this behavior but I can't seem to get my searches right. Well, the problem goes away once I restart the server, but it's a hassle when debugging. Is there a way or proper method to remove the references / file handle / etc to the TTF files or close this process as soon as it's finished? If possible, I'd like to keep using this code.
I have only a year of experience in Php so please bear with my rudimentary way of coding and asking questions. If anyone can lead me to the right discussions, please do. Thank you.
I'm using WAMP server version 2.4.

Related

Create a word cloud on selected pixels

I am trying to create a word cloud of a person's face. Similar to this
To achieve this I got a black & white image of a person and turned the darkest pixel to black and lightest pixel to white. And here is my result
Now I have got the area where I would like to place word clouds. Now I can't figure out how do I place words inside the face keeping margin/angle between words.
Here's the code what i have done so far
<?php
set_time_limit(0);
$src = 'person.jpeg';
$im = imagecreatefromjpeg($src);
$size = getimagesize($src);
$width = $size[0];
$height = $size[1];
$image_p = imagecreatetruecolor($width, $height);
imagecopyresampled($image_p, $im, 0, 0, 0, 0, $width, $height, $width, $height);
$white_color = imagecolorallocate($im, 255, 255, 255);
$black_color = imagecolorallocate($im, 0, 0, 0);
$font = __DIR__ . "/testfont.ttf";
$font_size = 16;
$text = "Test text";
$skip = true;
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if ($r >= 126) {
imagesetpixel($image_p, $x, $y, $white_color);
} else {
imagesetpixel($image_p, $x, $y, $black_color);
if ($x % 20 == 1) {
imagestring($image_p, 5, $x, $y, 'T', $black_color);
//imagettftext($image_p, 16, 0, $x, $y, $black_color, $font, $text);
}
}
//var_dump($r, $g, $b);
//echo "<br/>";
}
}
imagestring($image_p, 5, 0, 0, 'Hello world!', $black_color);
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
I tried using imagestring & imagettftext
if ($x % 20 == 1) {
imagestring($image_p, 5, $x, $y, 'T', $black_color);
//imagettftext($image_p, 16, 0, $x, $y, $black_color, $font, $text);
}
And got weird output. With imagettftext it takes too long to render and with imagestring this is what I got
Using one of these functions not both of them: imagesetpixel or imagestring
And if you have B/W photo, forget $black_color & $white_color or add them to this codes to customize more. And also add your custom header in the end.
list($w, $h, $type) = getimagesize('person.jpeg');
$resource = imagecreatefromstring(file_get_contents('person.jpeg'));
$img = imagecreatetruecolor($w, $h);
for($y=0; $y<$h; $y+=20)
for($x=0; $x<$w; $x+=20)
imagestring($img, 5, $x, $y, 'Hello world!', imagecolorat($resource, $x, $y));
Here's what works for me. I create a png image template in the Gimp, save it. Trickiest part for me was figuring out the font path aspect, since the font file just had to be in the one directory.
<?php
// Create Image From Existing File
$image = imagecreatefrompng('baseimg.png');
// Allocate A Color For The Text
$black = imagecolorallocate($image, 125, 125, 255);
// Set Path to Font File
$font_path = './FreeSans.ttf';
$text = "Hello World!";
// Print Text On Image
imagettftext($image, 14, 0, 15, 110, $black, $font_path, $text);
//Set the Content Type
header('Content-type: image/png');
// Send Image to Browser
imagepng($image);
// Clear Memory
imagedestroy($image);
exit;
?>

PHP error when creating image using imagecreatetruecolor

I got a simple HTML that calls a image generated from a PHP file but the image is not being displayed. The HTML part is:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="imgcaptcha.php"/>
</body>
</html>
imgcaptcha.php is:
<?php
//error_reporting(E_ALL);
//header('Content-Type: image/png');
//Generates rando string
function geraStringAleatoria($length = 10) {
$charSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$stringAleatoria = '';
for ($i = 0; $i < $length; $i++) {
$stringAleatoria .= $charSet[rand(0, strlen($charSet) - 1)];
}
return $stringAleatoria;
}
//Generate the captcha image
function geraImagemCaptcha($text = 'good'){
// Set the content-type
$width = 200;
$height = 30;
// Create the image
$im = imagecreatetruecolor($width, $height) or die('display nao instalado');;
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $width-1, $height-1, $white);
//ADD NOISE - DRAW background squares
$square_count = 6;
for($i = 0; $i < $square_count; $i++){
$cx = rand(0,$width);
$cy = (int)rand(0, $width/2);
$h = $cy + (int)rand(0, $height/5);
$w = $cx + (int)rand($width/3, $width);
imagefilledrectangle($im, $cx, $cy, $w, $h, $white);
}
//ADD NOISE - DRAW ELLIPSES
$ellipse_count = 5;
for ($i = 0; $i < $ellipse_count; $i++) {
$cx = (int)rand(-1*($width/2), $width + ($width/2));
$cy = (int)rand(-1*($height/2), $height + ($height/2));
$h = (int)rand($height/2, 2*$height);
$w = (int)rand($width/2, 2*$width);
imageellipse($im, $cx, $cy, $w, $h, $grey);
}
// Replace path by your own font path
$font = 'flushout.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
}
geraImagemCaptcha(geraStringAleatoria());
?>
I'm using PHP 7
Tried already commenting out header(); call and add error_reporting(E_ALL); but no error message was displayed. Tried also many other different font.ttf files according to this, also didn't worked, so I guess that the problem is in another part of the code rather than the font because I tried many sites and also some other .ttf files I got from other programs installed. The problem is happening in both all browsers I tested. What could be missing in the code? Btw, I got this generate captcha image from here.

Captcha validation in PHP

I used the following code to create Captcha to my form. Captcha creating well. Now I want to change font-size, and Font character spaces. I don't know how to change in the following code.
<?php
session_start();
$code= substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(150, 35);
$bg = imagecolorallocate($im, 255, 255, 255);
$fg = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 5, 5, $bg);
imagestring($im, 5, 8, 8, $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
You can use the code below to implement a simple captcha using gd library of PHP. As you are beginner, here is a sample code for quick testing, it covers font-sizing also:
<?php
session_start();
header('Content-type: image/jpeg');
$text = rand(1000, 9999);
$font_size = 30;
$image_width = 200;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
for ($x=1; $x<=40; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'FREESCPT.ttf', $text);
imagejpeg($image);
?>
In imagettftext function:
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed
Here is the good tutorial on how to build captcha in php -> link
you can change font using imagestring function

PHP font_size Captcha issue

I tried to change the $font_size but it seems it is not working. It stays the same no matter what. Even if I altered the $font_size up to 130. It stays the same:
<?php
header('Content-type: image/png');
$rand_num = rand(1000, 9999);
$font_size = 130;
$image_width = 110;
$image_height = 20;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$black_color = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $rand_num, $black_color);
for($x=0; $x <= 30; $x++){
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black_color);
}
imagepng($image);
?>
According to PHP Manual, The font parameter
Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where
higher numbers corresponding to larger fonts) or any of your own font
identifiers registered with imageloadfont().
So you have to try 1 2 3 4 5 and not 130.
For built in fonts, font size can be from 1,2,3,4 and 5. Please refer to http://php.net/manual/en/function.imagestring.php
You can change the font size using imagettftext function.
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed

How to create multiple line-breaks in a dynamic image when needed?

Currently I've got the following working just fine without any problems (yet).
header ("Content-type: image/png");
$string = $_REQUEST['text'];
$font = 15;
$width = 300;
$height = 350;
$image = imagecreate($width, $height);
$back = ImageColorAllocate($image, 255, 255, 255);
$border = ImageColorAllocate($image, 0, 0, 0);
ImageFilledRectangle($image, 0, 0, $width, $height, $border);
ImageFilledRectangle($image, 1, 1, $width-2, $height-2, $back);
$text_color = imagecolorallocate($image, 255, 0, 0);
ImageStringWrap($image, $font, 3, 2, $string, $text_color, $width-2 );
imagepng($image);
function ImageStringWrap($image, $font, $x, $y, $text, $color, $maxwidth) {
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);
if ($maxwidth != NULL) {
$maxcharsperline = floor($maxwidth / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", false);
}
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, $x, $y, $line, $color);
$y += $fontheight;
}
}
While the above works great, one thing I've been failing to work is be able to get line breaks like would come out akin to nl2br().
The text that this is pulling from in $_REQUEST['text'] is from the database, which was originally inserted from a textarea and passed to this script via URL. Naturally when people type in a textarea, there are line breaks that come from that. While printing it to the browser via text is easy, I can't seem to get the same result within an image.
I haven't spend a long time working with the GD library, but after searching around I really can't find anything about how to do this. Is it just not possible?

Categories