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?
Related
I am making a dynamic signature generator and I would like to add a avatar image in part of the signature. How would I do this as well as sizing down the image?
Here is an example.
I have 2 images:
https://s-media-cache-ak0.pinimg.com/736x/d1/2e/9e/d12e9ecd9f4c4e45dafa5880c7d99c73.jpg
http://cf.juggle-images.com/matte/white/280x280/php-1-logo-primary.jpg
Steps:
Set the content type header - in this case image/jpg
Create new canvas, width: 720, height: 637
Create two new images from file or URL, $icon1 & $icon2
Copy part of $icon1 to $canvas
Copy and resize part of $icon2 to $can
Output image to browser
<?php
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(720, 637);
$icon1 = imagecreatefromjpeg('https://s-media-cache-ak0.pinimg.com/736x/d1/2e/9e/d12e9ecd9f4c4e45dafa5880c7d99c73.jpg');
$icon2 = imagecreatefromjpeg('http://cf.juggle-images.com/matte/white/280x280/php-1-logo-primary.jpg');
//add 2 source images
imagecopy($canvas, $icon1, 0, 0, 0, 0, 720, 637);
imagecopyresized($canvas, $icon2, 0, 0, 0, 0, 100, 100, 280, 280);
//Output image to browser
imagejpeg($canvas);
?>
You can do that by copying one image on the other, with some resampling (to make the avatar smaller, if desired.).
PHP must have GD libs included, if not, make sure you have support for GD, otherwise you cannot use the functions.
Check your GD support: function.gd-info.php
Then start reading here:
function.imagecopy.php
or here: function.imagecopyresampled.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_image.jpg'';
$jpg_image = imagecreatefromjpeg($image_path);
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.
Basically I have to work with a ID Card PHP thing that requires me to add the name of a participant and upload a picture. I have a basic template for the ID Card and I'm using PHP to add the name. I have to add the picture of the participant on a particular spot. The name is working but I cannot add a picture of the participant over the generated PHP image. Here's my code:
http://chopapp.com/#arcqwgf4
How should I go about adding the picture over the PHP generate image.
Sorry if this is confusing :/
You are already most of the way there using GD, what you need to do is use imagecopymerge to draw one image on top of the other at a specified point on the base image. PHP.net has quite a few examples on the linked page.
Use the Imagick for things like these. There is a method named combineImages() with which you can achieve your goal.
You can do it with imagecopyresampled method in php.
I created an example here: http://artuc.my.phpcloud.com/artuc/ type name, id and select an image to place it.
Here is the code i used:
header('Content-Type: image/jpeg');
$canvasImage = imagecreatefromjpeg('sample.jpg');
$bgcolor = imagecolorallocate($canvasImage, 255, 255, 255);
imagefill($canvasImage, 0, 0, $bgcolor);
//Place person picture
$bgImage = $_POST['idPic'];
$img = imagecreatefromjpeg($bgImage);
$imageSize = getimagesize($bgImage);
imagecopyresampled($canvasImage, $img, 15, 17, 0, 0, $imageSize[0], $imageSize[1], $imageSize[0], $imageSize[1]);
$fontFile = "Arial.ttf";
//Place person name
$black = imagecolorallocate($canvasImage, 0, 0, 0);
imagettftext($canvasImage, 18, 0, 220, 35, $black, $fontFile, $_POST['idName']);
//Place person id
$black = imagecolorallocate($canvasImage, 0, 0, 0);
imagettftext($canvasImage, 18, 0, 220, 75, $black, $fontFile, $_POST['idNum']);
//Save Image
imagejpeg($canvasImage);
imagedestroy($canvasImage);
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