Trying to draw some text on an already existing image in php, but get weird results.
I have this image
and I'm trying to draw a number on it with white text, but I get this result
Here is the code:
<?php
$font = "files/fonts/open_sans/OpenSans-Regular-webfont.ttf";
$image = imagecreatefrompng('images/icons/marker_icon.png');
$white = ImageColorAllocate($image, 255,255,255);
imagettftext($image, 1, 1, 1, 1, $white, $font, $_GET['count']);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
First time drawing on an image, so I have no idea what I'm doing wrong.
The issue was your image, I'm not sure how or why but it was messed up. I opened it in a photo editor and resaved it with a different name as a PNG and it worked. Also, your text won't show up because your font size is set to one and it begins at xy 1,1. It should reflect as below:
<?php
$font = "files/fonts/open_sans/OpenSans-Regular-webfont.ttf";
$image = imagecreatefrompng('images/icons/marker_icon.png');
$white = ImageColorAllocate($image, 255,255,255);
imagettftext($image, 15, 0, 10, 35, $white, $font, $_GET['count']);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
PHP Manual imagettftext
Figured it out. Since there is a lot of transparency in my image, I had to set imageAlphaBlending to true:
<?php
$font = "files/fonts/open_sans/OpenSans-Regular-webfont.ttf";
$image = imagecreatefrompng('images/icons/marker_icon.png');
$white = ImageColorAllocate($image, 255,255,255);
imageAlphaBlending($image, true);
imageSaveAlpha($image, true);
imagettftext($image, 15, 0, 10, 35, $white, $font, $_GET['count']);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
Related
I am executing my following code for creating a transparent image but everytime it shows me black background.
kindly tell me my my fault in the code.
<?php
//set the content type
header('Content-type: image/jpeg');
//create the image
$im = imagecreatetruecolor(250, 200);
$black = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
// Make the background transparent
imagecolortransparent($im, $black);
//text to draw
$text=$_POST['text'];
//font path
$font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
// Add the text
imagettftext($im, 15, 0, 50, 50, -$blue, $font, $text);
//view the image
imagejpeg($im);
imagedestroy($im);
?>
You cannot make jpeg images transparent. Use png instead
Change below 2 lines:
header('Content-type: image/png');
imagepng($im);
Update
Reference Link: create transparent png image
I need help in my PHP image code. I'm trying to make Captcha, but all I get is broken image icon ?
This is my code:
<?php
header('Content-type: image/jpeg');
$text = "1111";
$font_size = 30;
$image_width = 110;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpg($image);
?>
"font.ttf" is next to my php file, and names are matching
Use
imagejpeg($image);
Instead of
imagejpg($image);
As imagejpg() doesn't exist.
When debugging you should comment out the header to see the errors.
i have a image and i wrote some text on this image by using php "imagettftext" function. now i don't know that how it will save automatic.
header('Content-Type: image/png');
// Create the image
$im = imagecreatefromjpeg('image-1.jpg');
// 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, 399, 29, $white);
// The text to draw
$text = 'www.blockprintsonline.com';
// Replace path by your own font path
$font = 'arial.ttf';
list($width, $height, $type, $attr) = getimagesize($get_image);
$width1=$width*20/100;
$height1=$height*50/100;
$font_size=$width*4/100;
// Add some shadow to the text
//imagettftext($im, 30, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, $font_size, 0, $width1, $height1, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
If you want to save the image to a file, check out the docs for the imagepng() function here.
By passing a filename as the second argument it will save the image to a file, e.g:
imagepng($im, "path/to/save/image/in.png");
<?php
// Save the image as 'simpletext.png'
imagepng($im, 'simpletext.png');
// Free up memory
imagedestroy($im);
?>
this is an example on how to save an image.
If you check the documentation for imagepng you would see that you can provide a filename. That would save the image to disk
At this moment i'm trying to create a dynamic PHP image, and i'm not really sure if it is even possible to have clickable links in the following piece of code:
<?php
$image = "hotelview_val13.png";
$src = 'pixel.png';
putenv('GDFONTPATH=' . realpath('.'));
$font = 'font.ttf'; //Ubuntu font
$im = imagecreatefrompng($image);
imagealphablending($im, true);
imagesavealpha($im, true);
imagealphablending($src_to_copy, true);
imagesavealpha($src_to_copy, true);
imagealphablending($pg, true);
imagesavealpha($pg, true);
$wc = ImageColorAllocate ($im, 255, 255, 255);
$red = ImageColorAllocate ($im, 255, 0, 0);
$blk = imagecolorallocate($im, 0, 0, 0);
{
imagettftext($im, 12, 0, 45, 310, $blk, $font , "Link");
imagettftext($im, 12, 0, 45, 330, $blk, $font , "Veel plezier ;)");
}
header("Content-Type: image/png");
Imagepng($im);
ImageDestroy ($im);
?>
What i'm trying to do is to make a clickable link. I've tried to just simply put into the code, but that doesn't work because it displays the code as plain text. Is it possible? If it is, how does it work?
Thanks a lot for your time.
You cannot embed links in an image. The only way to make a region of an image clickable is, when viewed in a browser, to use an HTML map.
What is the best way to display underlined text and output the result as image with GD or any other library?
You can try using the Unicode underline combining character U+0332.
<?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);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = "̲U̲d̲e̲r̲l̲i̲n̲e";
// Replace path by your own font path
$font = 'arial.ttf';
// 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);
?>
There are lots of FREE PHP CAPTCHA out there that come with a lot of customization, download one and see what exactly happens behind the scene. Also have a look at this link
HTH
I am using this...
$font = imageloadfont($font_file);
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
$str_width = strlen($text)*$font_width;
ImageLine($image, $left, $top+$font_height, $left+$str_width, $top+$font_height, $color);