Image with text not working and add some Html - php

I want to upload an image with some text on it, but when I'm uploading my PHP script it's not working with an image, it shows error every time. And also i want to add some html in the same file I had tried to put that php In html tag and show that image in the center how to do so
My PHP code is:
result.php
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('image.jpg');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$font_path = 'font.TTF';
$text = $_GET["name"];
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>
$_GET["name"]; is used for getting text form HTML by input tag, and for your information I have used form method to get the text I want an working example.
The image I want to use is http://images.visitcanberra.com.au/images/canberra_hero_image.jpg
So please use that image only while giving answer

this line will describe the name of image $jpg_image = imagecreatefromjpeg('image.jpg'); and image name is image.jpg
as you mentioned The image I want to use is http://images.visitcanberra.com.au/images/canberra_hero_image.jpg
this is image name canberra_hero_image.jpg right, how come php will work, first rename and use single name
better rename you php
this is the reason you see only text and not image

Change your code from '$jpg_image = imagecreatefromjpeg('image.jpg');' to this code
$image_path = ''http://images.visitcanberra.com.au/images/canberra_hero_ima‌​ge.jpg'';
$jpg_image = imagecreatefromjpeg($image_path);

Related

php image upload with adding text

I created an uploader for myself, now what I am trying is adding text on it too. I already created it outside the uploader but I just can't combine them two, already tried multiple sequences but failed :( need help here's the code.!
Here is the upload code.
$file_name = $_FILES[attach][name];
$caption=$_POST["caption"];
$uploaddir = "../photos";
$up_path=$uploaddir."/".$file_name;
move_uploaded_file($_FILES['attach']['tmp_name'], $up_path);
Here's the text on img code:
$get_image = imagecreatefromjpeg('name.jpg');
$white = imagecolorallocate($get_image, 255, 255, 255);
$txt = "Hello World";
$font = "arial.ttf";
imagettftext($get_image, 24, 0, 5, 24, $white, $font, $txt);
header('Content-type: image/jpeg');
imagejpeg($get_image, "name.jpg", 100);
imagedestroy($get_image);
You can achieve watermarking using image libraries.
You can find examples here.
Add 'Watermark' to images with php
An alternative way is to use FFMPEG Library which can be executed through php exec() method. See the link below.
How to add transparent watermark in center of a video with ffmpeg?

Adding/Merging text in an image in the browser with PHP

I would like to do something like http://en.nametests.com/test/what-does-your-name-say-about-you/641/
Although, I couldn't get it to work. What html code do I need? Guide me please.
the code I have right now is this
The problem is it won't give me an option to enter a name.
When the user enters in their name in the text field, it will generate an image with their own name on the image. The text will merge into the image. I don't know a better way to phrase this.
Please any tips, sugguestions, pros, cons?
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('sunset.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'font.TTF';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
First, you have to learn how HTML forms work.
You will need to create HTML page with form like this:
<form method="post" action="your_script.php">
<input type="text" name="your_name" />
<input type="submit" name="submit" value="Generate image!" />
</form>
And then, in your_script.php, you put your prepared code and change line where the text is set to
$text = $_POST['your_name'];
Hope this is sufficient for you - if not, you should probably learn some HTML and PHP basics or find some pre-programmed solution.

PHP function not displaying barcode using TrueType fonts

Hello I am using a function that I found in Internet to display a barCode using a TrueType font, here is the code:
//For displaying barcodes
//Arguments are:
// code Number you want outputted as a barcode
//You can use this script in two ways:
// From a webpage/PHP script <img src='/images/barcode.php?code=12345'/>
// Directly in your web browser http://www.example.com/images/barcode.php?code=12345
//Outputs the code as a barcode, surrounded by an asterisk (as per standard)
//Will only output numbers, text will appear as gaps
//Image width is dynamic, depending on how much data there is
header("Content-type: image/png");
$file = "barcode.png"; // path to base png image
$im = imagecreatefrompng($file); // open the blank image
$string = "123123123"; // get the code from URL
imagealphablending($im, true); // set alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
$black = imagecolorallocate($im, 0, 0, 0); // colour of barcode
$font_height=40; // barcode font size. anything smaller and it will appear jumbled and will not be able to be read by scanners
$newwidth=((strlen($string)*20)+41); // allocate width of barcode. each character is 20px across, plus add in the asterisk's
$thumb = imagecreatetruecolor($newwidth, 40); // generate a new image with correct dimensions
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, 40, 10, 10); // copy image to thumb
imagettftext($thumb, $font_height, 0, 1, 40, $black, 'B2FI25HRc.ttf', '*'.$string.'*'); // add text to image
//show the image
imagepng($thumb);
imagedestroy($thumb);
I cannot find the error why the function doesn't display the image. Any ideas? The font is in the same directory with the php function and I tried relative and absolute paths to the font with no results. Any suggestion?
Thank you very much
You need to check for error messages.
For debugging, comment out the header line and add these lines on the top to show all errors:
ini_set('display_errors',true);
error_reporting(E_ALL);
In many cases the error messages will tell you pretty clear whats wrong.

Does html can be use with dynamic generated images in php?

I am using this code to create an image
<?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, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.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()
(A)print ('<div class="test">');
imagepng($im);
print ('</div>');
(B)imagedestroy($im);
?>
The code work fines if i comment the line number 'A' and 'B' and it generates the image on the browser with testing written on it. But i want the image to be in a div. so i uncomment the line (A) and (B) but it is not giving right output. The generated html is also strange generated html is
<img src="http://localhost/php/test92.php" alt="The image “http://localhost/php/test92.php” cannot be displayed, because it contains errors.">
Basically, to create dynamic image in HTML, you will need 2 PHP files:
one for the image itself
another one for PHP to display it.
Let's take a look how to do it:
You create image.php that accept parameter, like: image ID or file name. For security reason, you HAVE to filter whatever parameter it get.
Why you have to do this? because, to generate image, you can't mix it with another HTML output. Let alone a single space or return as this will render the image broken.
You do the HTML thing on another PHP, say test92.php. To the HTML logic here, like:
get image data
loop the data
display image => <img src="image.php?imageID=12" alt="" />
If you want a div around your image you have to do that in the html, you can't do that in the image generation code
<div>
<img src="http://localhost/php/test92.php">
</div>
If you are getting errors regarding the image, try browsing the image url http://localhost/php/test92.php and see what it looks like.
Does it show an image like you are expecting?

php combining text on images more than once?

I am trying to print users personal data like name, email, phone number on screen, using separate images, rather than printing out in clear text on page, thus getting cache possibly by Google. Trying to print like below:
Name - image with name text created on fly
Email - image with email text created on fly
Phoneno - image with number text created on fly
The code I have provided merges text into an image ie name, but only allows me to create one image to send back to browser, how can I try to get my script to send more than one image back to browser?
I have tried adding more parameters to my function function 'create_image($name,$email,$number)' but only prints one field to browser, perhaps something to do with header()?
<?php
//Send a generated image to the browser
$name="Bob";$email="bob#email.co.uk";$number="12345678901";
create_image($name);
function create_image($value)
{
//Set the image width and height
$width = 250;
$height = 20;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
//imagestring ( resource $image , int $font-(font size) , int $x-(from left) , int $y-(from right) , string $string , int $color-(font-colour) )
ImageString($image, 5, 10, 3, $value, $white);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
}
?>
Thanks for any replies
$name="Bob";$email="bob#email.co.uk";$number="12345678901";
switch ($_GET['option']) {
case 'name':
create_image($name);
break;
case 'email':
create_image($email);
break;
case 'number':
create_image($number);
break;
}
And call your script with scriptname.php?option=name
its not really a best practice, but I i would try to change your function, that it saves the images to a file, and returns the file path, when theres is a valid file.
So you don't have to create every call a new image (which is slow), and you get a valid filepath, which means you don't have to care about headers and so on

Categories