PHP GD Image generation broken - php

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.

Related

Creating captcha with GD library, no result,

When I check the index page it does not show anything but a broken image.
These are the files I am using.
index.php
<?php
session_start();
$_SESSION['secure'] = rand(1000, 9999);
?>
<img src="./generate.php" />
generate.php
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$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, $fon_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);

Watermark an Image using Text in PHP

I have an image and I'm trying to watermark the image using text.
I have done with code part, but no image see been viewed with watermark text.
Here is the code below:
$imagetobewatermark="images/muggu.png";
list($width,$height)=getimagesize($imagetobewatermark);
$imagetobewatermark=imagecreatetruecolor($width,$height);
$mark=imagecreatefrompng($imagetobewatermark);
imagecopy($imagetobewatermark,$mark,0,0,0,0,$width,$height);
$wartermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
Tell me If I'm wrong.
Thank you.
One problem I can see straight away is that the $imagetobewatermark variable starts off as a string, then becomes a new blank image object (not an existing image), and when you subsequently create the mark image object, it's not going to work because $imagetobewatermark is no longer a string.
Try:
$imagetobewatermark=imagecreatefrompng("images/muggu.png");
$watermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
EDIT:
I failed to notice a typo in your text variable $wartermarktext, which should be $watermarktext.
Correct this and it should work.
You had some spellings wrong and did not use the ressource. Here corrected:
$imagetobewatermark = "muggu.png";
list ($width, $height) = getimagesize($imagetobewatermark);
$res = imagecreatetruecolor($width, $height);
$mark = imagecreatefrompng($imagetobewatermark);
//make sure here to use the ressource, not the filepath
imagecopy($res, $mark, 0, 0, 0, 0, $width, $height);
$watermarktext = "Muggu";
//$font = "../font/century gothic.ttf";
//I copied it to my local test folder
$font = "GOTHIC.TTF";
$fontsize = "15";
//make sure here to use the ressource, not the filepath
$white = imagecolorallocate($res, 255, 255, 255);
//make sure here to use the ressource, not the filepath
imagettftext($res, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
//make sure here to use the ressource, not the filepath
imagepng($res);
//make sure here to use the ressource, not the filepath
imagedestroy($res);
you have to give Public/Absolute Path for Font arial.ttf.
Solution is tested in Laravel 5.8.
you can use this solution. here [ $SourceFile , $DestinationFile ] are Absolute Path of local directory. $imgUrl is HTTP based URL. Watermark will be placed at the top of the image.
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile,$imgUrl) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 255, 255, 255);
$font = public_path('fonts/arial.ttf');
$font_size = 8;
imagettftext($image_p, $font_size, 0, 10, 20, $black,$font , $WaterMarkText);
if ($DestinationFile <> '') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
return $imgUrl;
}
Try this.
$imagetobewatermark = "images/muggu.png";
$watermarktext = "Muggu";
list($width, $height) = getimagesize($imagetobewatermark);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($imagetobewatermark);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$font = "../font/century gothic.ttf";
$font_size = 15;
$white = imagecolorallocate($image_p, 255, 255, 255);
imagettftext($image_p, $font_size, 0, 20, 20, $white, $font, $watermarktext);
header("Content-Type: image/png");
imagepng($image_p, null, 0);
imagedestroy($image);
imagedestroy($image_p);
Try this code, It helps you to provide the watermark for the images.
<?php
header('Content-type: image/jpeg'); //SET THE FORMATE OF THE IMAGE
$jpg_image = imagecreatefromjpeg('images.jpg'); //GIVE LINK FOR SPECIFIED IMAGE
$color = imagecolorallocate($jpg_image, 500, 500, 500); //GIVE COLOR TO WATERMARK TEXT
$font_location = 'BROADW.TTF'; //WATERMARK FONT
$text = "http://www.sanwebtutorials.blogspot.in/"; //WATERMARK TEXT
$x=200; //X CO-ORDINATE
$y=800; //Y CO-ORDINATE
$size=21; //SIZE OF TEXT
imagettftext($jpg_image,$size, 0, $x, $y, $color, $font_location, $text); //PRINT TEXT ON IMAGE
imagejpeg($jpg_image); //SEND IMAGE TO BROWSER
imagedestroy($jpg_image); //DESTROY THE MEMORY
?>

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

Gd Library Image Style imagettftext

How to make Like This Image?
I just want to learn how do i get output like this using GD library,
Thanks for your help guys..
Here is your code, you could easily find it on google tho.
<?php
header ("Content-type: image/png");
$string = "your text"; // Change this text
$font = 4; // try changing this as well
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
?>
Here's an example:
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
imagepng($image); // outputting the image
For more see imagettftext().
EDIT: An example of using multiple imagettftext():
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text
$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text
imagepng($image); // outputting the image

turning text into image - PHP/GD - save image

I'm using this script to simply create an image from text. What I would like to know is how to save the image instead of printing straight to browser;
// an email address in a string
$string = $post[$key];
// some variables to set
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
// lets begin by creating an image
$im = #imagecreatetruecolor ($width,$height);
//white background
$background_color = imagecolorallocate ($im, 255, 255, 255);
//black text
$text_color = imagecolorallocate ($im, 0, 0, 0);
// put it all together
$image = imagestring ($im, $font, 0, 0, $string, $text_color);
I know its probably just one line of code at the end but im not sure which GD function to use.
Any help would be much appreciated, Regards, Phil.
EDIT:
I have tried the following but just get a balnk black box;
// an email address in a string
$string = $post[$key];
// some variables to set
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
// lets begin by creating an image
$im = #imagecreatetruecolor ($width,$height);
//white background
$background_color = imagecolorallocate ($im, 255, 255, 255);
//black text
$text_color = imagecolorallocate ($im, 0, 0, 0);
// put it all together
imagestring ($im, $font, 0, 0, $string, $text_color);
imagepng($im, 'somefile.png');
imagedestroy($im);
Pass a filename to the appropriate image-generating image*() function:
imagepng($image, 'somefile.png');
Look at here http://www.php.net/manual/en/function.imagepng.php
take a look at imagepng() (or imagegif, imagejpeg...) - if you add a filename as second parameter, the image is saved as file.

Categories