PHP Overlay GIF on JPEG - php

At the moment I am generating a barcode using Shay Anderson's class (http://www.shayanderson.com/php/php-barcode-generator-class-code-39.htm) and I am able to successfully display the generated barcode in the browser as follows:
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($src);
but now I am trying to modify my script to overlay the barcode on top of another image to create a voucher but I can't seem to get it to work, I just get the broken image icon in Chrome and the following warning in the console:
Resource interpreted as Document but transferred with MIME type image/jpeg
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
header('Content-Type: image/jpeg');
// create actual voucher with barcode overlayed on voucher background
$bg = imagecreatefromjpeg('images/voucher.jpg');
imagecopymerge($bg, $src, 0, 0, 0, 0, imagesx($bg), imagesy($bg), 75);
imagejpeg($bg, null, 100);
imagedestroy($bg);
Error reporting is on and I'm getting no wanrings, notices or fatal errors. Any help appreciated.
The only thing I can think of is that from the docs of the barcode class, it generates the barcode as a gif so not sure if I am missing a few steps.

Turns out the problem was to do with the image I was using as the basis of the merge wasn't quite right so I re-converted it from a png to jpg properly (first time I downloaded the png I simply did a save as all files to jpeg) using photoshop and it was fine, to clarify, here's the code:
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
header('Content-Type: image/jpeg');
$bg = imagecreatefromjpeg('images/voucher.jpg');
imagecopymerge($bg, $src, 40, 380, 0, 0, imagesx($bg), imagesy($bg), 100);
imagejpeg($bg, null, 100);
imagedestroy($src);
imagedestroy($bg);

Related

Cannot display JPEG image in PHP Laravel project

Does anybody know what can call these files properly?
I'm trying to put text on a JPEG image, and apparently the way I'm calling those 2 files is not working
It is a PHP 7.4 Laravel 8 project and the script is located in a blade view.
<?php
// C:\xampp\htdocs\solidcadclass\public\storage\resources\arial.ttf
// C:\xampp\htdocs\solidcadclass\public\storage\resources\template.jpeg
$img = imagecreatefromjpeg('C:\xampp\htdocs\solidcadclass\public\storage\resources\template.jpeg');
$black = imagecolorallocate($img, 0, 0, 0); // OBJ, RGB
$txt = Auth::user()->name;
$font = 'C:\xampp\htdocs\solidcadclass\public\storage\resources\arial.ttf';
imagettftext(
$img, // Image object
24, // Font Size
0, // Angle
5, 24, // x,y
$black, // Color
$font, // Font to use
$txt // Text to write
);
// OUTPUT IMAGE
header("Content-type: image/jpeg");
imagejpeg($img);
I am getting bunch of weird symbols like:
����JFIF ...
Any advice on how to call files in functions like these would be really useful!
You can use Laravel response function with passing image in response parameter.
return response($img)->header('Content-type','image/jpeg');

php imagegif() direct output fails but indirect output works

I have a php script that is supposed to create and display an image.
The image is displayed properly on the webpage when using this code:
header('Content-Type: image/gif');
readfile('picture.gif');
//rest of code creating the image
imagegif($im,'picture.gif');
But, the image is not displayed when using this code:
header('Content-Type: image/gif');
//rest of code creating the image
imagegif($im);
In the php documentation for imagegif, this code is given as an example:
<?php
// Create a new image instance
$im = imagecreatetruecolor(100, 100);
// Make the background white
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
// Draw a text string on the image
imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);
// Output the image to browser
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
?>
What am I doing wrong?

Multiple Images and HTML in one File using GD Library

Good day everyone.
Long story short, I'm trying to make an online card game much like urban rivals.
To do so, I'm using mainly PHP and SQL.
Right now, I'm trying to generate the png file of a card based on a database.
I've already managed to merge images with alpha channel, to add text to an image, but my real problem is : I cannot seem to be able to output this image along with other and the basic layout of a HTML document. Here is my test code :
<?php
include 'phpGDtools.php';
$image1path = 'monstercard.png';
$image2path = 'monsterimage.png';
$xdim = 220;
$ydim = 301;
//$image = mergeTwoPNGAndReturn($image1path, $image2path, $xdim, $ydim);
mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim);
echo('Hello !');
?>
Sorry for the bad formatting, I haven't been able to make it better :/
Here is my function in phpGDTools.php:
function mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim)
{
//TEST FUNCTION
//Both paths to the images, x and y dimensions of the image you're about to fuse.
$final_img = imagecreate($xdim, $ydim);
//Step 1 : Create the objects from the PNGs
$image_1 = imagecreatefrompng($image1path);
$image_2 = imagecreatefrompng($image2path);
//Step 1.2 : Allow transparency of bases.
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagealphablending($image_2, true);
imagesavealpha($image_2, true);
//Step 2 : Merge the images into one.
imagecopy($image_1, $image_2, 0, 0, 0, 0, $xdim, $ydim);
imagecopy($final_img, $image_1, 0, 0, 0, 0, $xdim, $ydim);
//Step 3 : Allow transparency of final image.
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
//Step 4 : Output image to browser.
header('Content-Type: image/png;');
imagepng($image_1);
}
This outputs very well the image, as intended, but the text "Hello !" is nowhere to be found.
Also, please ignore the part about "$final_image" it's just for testing.
You may have noticed I commented this line :
//$image = mergeTwoPNGAndReturn($image1path, $image2path, $xdim, $ydim);
This is the function I want to use : hopefully, my goal would be to include this php file into an html page, and then being able to print the image object between two tags.
Technically, I could save it as afile and then output the file, but that would be extremely inefficient considering my project.
Thank you for helping me out.
~Jules Courtois
You're setting the content type to an image, so anything you send back will be interpreted as an image. When you're echoing HTML you're adding to the image data which is resulting in the browser not being able to read the image.
You need to create a HTML page then reference images within the HTML file with a or tag with the src to the URL of the image or you can base64 an image to use it inline.
You could replace
imagepng($image_1);
with
ob_start();
imagejpeg($image_1);
$image_data = ob_get_contents ();
ob_end_clean ();
$image_data_base64 = base64_encode($image_data);
echo '<img src="data:image/png;base64,' . $image_data_base64 . '" />';
You should avoid processing images on the fly and try to store them but this should achieve what you asked in the question.
It looks like you are trying to output image data and html data from the same php script.
You call the function
mergeTwoPNGAndOutputInBrowser();
which outputs the image header to the browser.
And then you
echo('Hello !');
Where 'hello' is text data and not image data... So its not displayed in the image.
You could do something like this.
<?php
include 'phpGDtools.php';
$image1path = 'monstercard.png';
$image2path = 'monsterimage.png';
$xdim = 220;
$ydim = 301;
if(isset($_GET['display_image'])){
//Output image data
mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim);
}
else{
//Output page data
echo 'Hello !';
echo "<img src=\"?display_image\">";
}
?>
The Text is not displaying since you have set the header
header('Content-Type: image/png;');
Why don't you use the imagestring() of the same PHP GD Library. It let's you write text on the image.
Here's a cool example. You can embed this same code to your mergeTwoPNGAndOutputInBrowser() function.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Source

Draw image in PHP and no image being displayed

Folks,I've some 288 points with their X,Y co-ordinates and a value assigned to them. I need to show this figuratively. I tried gd and imagettftext but simple code to draw a blank image isn't working even when I've installed and configured gd.
header('Content-Type: image/png');
$image = imagecreatetruecolor(400, 300);
// Allocate a color for the polygon
$col_poly = imagecolorallocate($image, 255, 255, 255);
// Draw the polygon
imagepolygon($image, array(
0, 0,
100, 200,
300, 200
),
3,
$col_poly);
// Output the picture to the browser
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
The output in the browser is
Try to set encoding of php file with your code to UTF-8 without BOM (e.g. in Notepad++).
Your code works right and image shows. Most probably your GD library is not working as expected. Try this
$image = imagecreatetruecolor(400, 300) or die('Cannot Initialize new GD image stream');

Output PNG images with transparency in PHP

How do you correctly output PNG images with PHP so their shading, and other transparent effects don't fail.
seems to be outputting as
...is there a way so this doesn't happen?
I merged two images together.
<?php
// Create image instances
$dest = imagecreatefrompng('vinyl.png');
$src = imagecreatefromjpeg('cover2.jpg');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 180, 180, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
imagealphablending and imagesavealpha.
See this post: PNG Image Transparency in PHP GD

Categories