I'm trying to use the GD graphics library to make this captcha image, and the box shows up how Id like it to, but the text isn't being displayed over the image at all. I can't figure out why it's not showing up.
<?php
session_start();
putenv('GDFONTPATH=' . realpath('.') );
$font = 'Molle';
header('Content-Type: image/png');
$im = imagecreatetruecolor(260, 40);
$white = imagecolorallocate($im, 255, 255,255);
$grey = imagecolorallocate($im, 215, 215, 215);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 3, 3, 255, 34, $grey);
$text = 'Testing';
$_SESSION["captcha"] = $text;
imagettftext( $im, 20, 0, 16, 26, $white, $font, $text );
imagettftext( $im, 20, 0, 15, 25, $black, $font, $text );
imagepng($im);
imagedestroy($im);
?>
Related
this is my code:
header('Content-Type: image/jpeg');
$image_width = 400;
$image_height = 30;
$im = imagecreatetruecolor($image_width, $image_height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = utf8_encode("test 123 abc");
$font = 'myfont.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagejpeg($im);
imagedestroy($im);
can anyone help me find where i am wrong.
background is comming in white i want to remove white and put transparent.please help
this is an image: http://funbusy.com/fbtest/user_image.php
?php
// Create the image
$im = imagecreatetruecolor(400, 25);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 22, 125, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = ucwords('tanuja sree');
// Replace path by your own font path
$font = 'OpenSans-Italic.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()
ob_start();
imagepng($im);
$image_data = ob_get_clean();
$image_code = '<img id="image_code" src="data:image/png;base64,'. base64_encode($image_data) . '">';
imagedestroy($im);
?>
Try adding this piece of code after imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagecolortransparent($im, $white);
imagesavealpha($im,true);
imagealphablending($im, true);
I have this script which generates an image:
$im = imagecreatetruecolor(110, 34);
$red = imagecolorallocate($im, 245, 245, 245);
imagefill($im, 0, 0, $red);
$text_color = imagecolorallocate($im, 80, 80, 80);
imagestring($im, 8, 15, 9, image, $text_color);
header('Content-Type: image/jpeg');
imagejpeg($im);
Now I want to save that image. How can I do that? Here is what I have tried so far:
file_put_contents("../img/name.jpg", file_get_contents($im));
What's wrong?
I have this PHP code:
<?php
//Other part of code
Header("Content-type: image/png");
$im = imagecreatefromPng("./images/signatures/background.png");
$red = ImageColorAllocate($im, 255, 0, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
ImageString($im, 5, 15, 5, "$callsign", $black);
ImageString($im, 5, 15, 20, "$name $surname", $black);
ImageString($im, 5, 15, 35, "Location: $location", $black);
ImageString($im, 5, 15, 50, "HUB: $hub", $black);
ImageString($im, 5, 15, 65, "Hours: $hours", $black);
$font_width = ImageFontWidth(5);
ImagePng($im);
?>
I want to change the font that PHP uses to write in the image. How can i do that?? I try but I canĀ“t.
Assuming that you mean font:
http://www.php.net/manual/en/function.imageloadfont.php
http://php.net/manual/en/function.imagettftext.php this may help if your using ttf fonts
You can use imageloadfont ();
syntax:
int imageloadfont ( string $file )
Example:
// Create a new image instance
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Make the background white
imagefilledrectangle($im, 0, 0, 49, 19, $white);
// Load the gd font and write 'Hello'
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, 'Hello', $black);
// Output to browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
so change your code to:
//Other part of code
Header("Content-type: image/png");
$im = imagecreatefromPng("./images/signatures/background.png");
$font = imageloadfont('./fonts/arial.gdf');//change the parameter based on your font file name
$red = ImageColorAllocate($im, 255, 0, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
ImageString($im, $font, 15, 5, "$callsign", $black);
ImageString($im, $font, 15, 20, "$name $surname", $black);
ImageString($im, $font, 15, 35, "Location: $location", $black);
ImageString($im, $font, 15, 50, "HUB: $hub", $black);
ImageString($im, $font, 15, 65, "Hours: $hours", $black);
$font_width = ImageFontWidth(5);
ImagePng($im);
I'm currently using the following script--
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'My Name';
// Replace path by your own font path
$font = 'AGENCYB.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);
?>
But I want to add a background image too. Please help, I'm new to this function especially.
Would something like the following work for you? You want to open the image you want to use as the background, and then write your text over the top.
<?php
// Set the content-type
header('Content-type: image/png');
/* Attempt to open */
$im = #imagecreatefrompng('backgroundimage.png');
/* See if it failed */
if(!$im)
{
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'My Name';
// Replace path by your own font path
$font = 'AGENCYB.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);
}else
{
//you want to do something here if your image didn't open like maybe fpassthru an alternative image
}
?>