I check StackOverflow to see if this question has been answered, but didn't find anything corresponding to this.
Running Wampserver 3/Apache 2.4.18/PHP 7.0.4/MySQL 5.7.11. This simple example that I copied from StackOverflow doesn't work -- no image is displayed. Extension php_gd2 is enabled (get_extension_funcs("gd") shows the list).
I had to comment out the header(); code (even if I put it at line 2), because Firefox complained the code is incorrect with it.
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
//header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
?>
If I try to load in image from a file:
<?php
$thumb = imagecreatefromjpeg("http://localhost/newthumb.jpg");
if( imagejpeg($thumb)){
imagedestroy($thumb);
echo "<br>Image2 created";
}
else {
echo "<br>Image2 not created";
}
?>
I get what is tantamount to a character dump of the file (the # are black diamonds with ? in them):
####JFIF##>CREATOR: gd-jpeg v1.0 ...
Image2 created
What the !##$! happened with the new Wampserver/Apache/PHP/MySQL??? What does it take to get images to display in PHP?
Related
this is the code
<?php
$x = imagecreatetruecolor(250, 250);
$y = imagecolorallocate($x, 120 ,156,100);
header('Content-Type: image/jpeg');
imagejpeg($x);
?>
It is giving output as
The image "http://localhost/firstprogram.php" cannot be displayed because it contains errors.
I also tried Example from https://www.php.net/manual/en/function.imagerectangle.php and it still displayed the same message.
And also can someone tell if header is necessary or not and what exactly is it used for?
you made a image, you allocate it, but you did not draw it.
try this code
<?php
$x = imagecreatetruecolor(250, 250);
$y = imagecolorallocate($x, 120 ,156,100);
imagerectangle($x, 50, 50, 150, 150, $y);// draw the rectange of image
header('Content-Type: image/jpeg');
imagejpeg($x);
?>
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?
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.
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