unable to print text on image - php

I am trying to write text on image and displaying that image on browser. I tried below code but it is not working properly. Below code is showing one empty square (no image). Can anyone tell what's wrong I am doing here.
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('images/travel.jpg');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$text = "This is a sunset!";
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>

The problem in your code is this line
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
As mentioned in the doc http://www.php.net/manual/en/function.imagettftext.php
you have to pass font file path too
So just change the line
imagettftext($jpg_image, 15, 0, 15, 15, $white, 'Roboto-Bold.ttf',$text);
and put a font file to the folder and it will work.
If you don't want to give the font file replace the line
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
With
imagestring($jpg_image,15,15,15,$text,$white);
Your code will be
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQf66Ol6NAa6sdNhDJT0z1fVfTmjPjxAHkopPwExZ9AqHSqNzHP');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$text = "This is a sunset!";
imagestring($jpg_image,15,15,15,$text,$white);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>
This will work for you

Related

PHP imagejpeg not displaying image on Browser

I am using imagejpg() to display an image on the browser and place text over the image.
I have used this before and it has worked. Now I switched domain hosts and perhaps the PHP version is different as the image is now not displaying.
It displays a small square in the middle of the screen, like a broken link, but the image is definitely there.
Could someone please assist with this?
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
Thank you in advance.
Rob
I just figured it out. Answer below
I moved the header down and it sorted out the problem.
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
header('Content-type: image/jpeg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
I had the same problem, found out that if there is a blank space before the header, the image doesn't work.
I was able to save the image on the server but not to get the browser to display it.
Removing every blank space did the trick for me.
I think it's the same reason why your script didn't work with the header at the beginning.

Hindi text to Image - words not appearing properly PHP

I want to display hindi words on an image. Here is the code for it, it is working properly but it does not display "chote E ki matra" correctly. Example "अब हिन्दी" is displayed as : http://prntscr.com/8rj8mq
Also hindi.txt file contains these :
अब हिन्दी
मे टाइप
करना बहुत आसान है
Below php script read one line randomly and put it on image and display it. Can someone help me to correct this....
<?php
$word= file("hindi.txt");
$random = rand(0,2);
$text = $word[$random];
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, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
// Replace path by your own font path
$font = 'Mangal.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);
?>

Weird image glitch when using imagepng()

I am trying to make a gamecard for a RSPS. This is my first time using PHP to generate text on images and when trying to apply this weird glitch happens:
But, when the original photo looks like this:
As you can see the corners are not curved like they should be. But, in the original it comes out fine. I am not talking about the location of the numbers.
Here is the code I am using:
<?php
header('Content-Type: image/png');
// Image Creation
$image_file = "SoulSplitCard.png";
$im = imagecreatefrompng($image_file);
//Colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$rscol = imagecolorallocate($im, 23, 113, 183);
// Levels (for now)
$text = '99';
$text2 = '87';
// Font
$font = 'arial.ttf';
//Text Applications
imagettftext($im, 15, 0, 150, 35, $rscol, $font, $text);
imagettftext($im, 15, 0, 150, 81, $rscol, $font, $text2);
// Using imagepng() instead of imagejpeg()
imagepng($im);
imagedestroy($im);
?>
What is causing this to happen?

Move uploaded file php not working

I want to move a file that has been created using imagettftext and saved as a png. as you can see, in the code below, i used the move_uploaded_file but to no avail. please help.
tq
// Set the content-type
header('Content-Type: image/png');
// Create the image from created image
//$im = imagecreatetruecolor(180, 180);
$im = #imagecreatefromjpeg('poloroid.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 = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = '/uploaded_files';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
$name ="$fbid.png";
// Add some shadow to the text
imagettftext($im, 20, 0, 25, 126, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
move_uploaded_file($name,"$uploads_dir/$name");
imagedestroy($im);
You are not uploading a file, you are generating one!
imagepng has filename parameter so you can save it to your drive:
$uploads_dir = '/uploaded_files/';
$name = $uploads_dir.$fbid.'.png';
imagepng($im,$name,9);
imagedestroy($im);
try to use rename instead of move_uploaded_file

How to add a background image while creating a text image using php?

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
}
?>

Categories