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?
Related
I am using SimpleSoftwareIO (https://www.simplesoftware.io/simple-qrcode/) to generate QR Code in Laravel but I need to add text under QR code and when we download, it should download with text.
By using the below code I am able to create QR code:
In controller
$data="Hello";
$path = "logo.png";
$png = QrCode::format('png')->merge($path, .17, true)->size(300)->errorCorrection('H')->generate($data);
$png = base64_encode($png);
In blade file
<img src='data:image/png;base64,{{ $png }}'>
looking for something like this and it should be image
Your text could be another Image. In Intervention Package you can merge two images vertically to achieve this after the QR code generates.
see: http://image.intervention.io/api/insert
or you can use PHP default function imagecopymerge
see: https://www.php.net/manual/en/function.imagecopymerge.php
example:(from official docs)
<?php
// Create image instances
$dest = imagecreatefromgif('php.gif');
$src = imagecreatefromgif('php.gif');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 47, 75);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
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 make a generator to output user data from my gaming website and I've never really looked into the conversion to images in PHP, however I've managed to get the basics of it working now from other questions on this site and elsewhere, anyway I was wondering whether it is possible to make my PHP file that generates the image, parse HTML tags in its' output? Here is my script:
<?php
$username = $_GET['username'];
$guild = $_GET['guild'];
$create = "Username: " . $username . " Guild: " . $guild . "";
$im = imagecreate(300, 30);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
imagestring($im, 5, 0, 0, $create, $textcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
In my $create variable, I wish to use HTML tags but they aren't parsed as html and it outputs the tags in plain text.
Sorry, but PHP can't parse HTML before rendering it on an image. However, it does include various functions to change fonts and apply filters to the text. See http://php.net/manual/en/ref.image.php.
Is there a specific reason you need the information to be output as an image? Why not simply output the information using HTML?
Your browser parses HTML, php does nothing with it. You'll have to use stuff like \n in your string.
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?