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');
Related
I have a PHP file which generates an image of a specified size for testing. I guess the browser thinks the script's output would be text/html. So far I have installed php5-gd and saved my php file as UTF-8 without BOM, yet I still have the issue. Using readfile() to send an existing image to the browser works as it should.
My PHP image generator:
header('Content-Type: image/jpeg');
$im = null;
$width = 400;
$height = 300;
if (isset($_GET['w']) && isset($_GET['h'])) {
if (is_numeric($_GET['w']) && is_numeric($_GET['h'])) {
$width = $_GET['w'];
$height = $_GET['h'];
}
}
$im = imagecreatetruecolor($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
$orange = imagecolorallocate($im, 220, 210, 60);
imagestring($im, 3, 5, 5, 'TEST IMAGE', $orange);
imagejpeg($im);
imagedestroy($im);
Example code from PHP.net which also doesn't work:
// 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);
Working readfile() method:
header('Content-Type: image/jpeg');
readfile('./site_top/1.jpg');
UPDATE: PEBKAC Error.
When installing gd I saw that the Apache server restarted during the process, so I didn't think to restart it myself. It turned out I needed to restart Apache a second time in order for the installation to work.
Just copy and pasted your code, works fine, as noted on the PHP page of the imagejpeg() function:
Note: JPEG support is only available if PHP was compiled against
GD-1.8 or later.
Make sure your PHP has at least GD-1.8.
Image shown from your code below:
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);
I'm using Creating image function to create an image with exact text message using the following code
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
The output should be like this
Now my question is there any way i can write image over it not only text
so that if i've flag image at same path (flag.gif ) and i would like to write it just after my $message to be like this
so is this possible and how could be ! ~ thanks a lot
Update
based on #MarcB idea of using imagecopy function
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
the output is not true color WHY :(
ANY help about this new problem ~ thanks
With the help of Surreal Dreams and Marc B
This one works fine
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromjpeg("myimage.jpg"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
Output
I've should have learned the following functions
imagecopy
imagecreatefromjpeg
imagecreatefromgif
imagecopy() should properly deal with differences between images' palettes; however, the GIF format does not support more than 256 colors, and neither does GD when it works with palette-based images. If 256 palette entries already exist when GD tries to use a new color, GD will pick the closest match, which can produce the results you see.
To avoid this problem, you should use imagecreatetruecolor() to create a 24-bit true-color image in memory. You can then use imagecopy() to insert each GIF image (including the background) and imagepng() to generate PNG output, which is better for line art than JPEG, offers better compression than GIF, and can support more than 256 colors.
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
I have already draw a black image i want draw a file suppose 3.png on it ..
How to do that ?
You have to load an image you want to draw and then use imagecopy() to draw it:
// the part you already have; creates 538x616 px black image
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
// load image from file and draw it onto black image;
// for loading PNG, use imagecreatefrompng()
$overlayImage = imagecreatefromjpeg('macro_photo_1.jpg');
imagecopy($image, $overlayImage, 10, 10, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
// send image to the browser
header("Content-Type: image/png");
imagepng($image);
exit;
I would also advise to go through the list of GD and Image functions to see what (and how) can be done with images in PHP.
imagepng($image);
And if you haven't already you need:
header('Content-Type: image/png');
Before you call imagepng to let the browser know it's looking at an image not an HTML page.
Saroj, http://www.php.net/manual/en/function.imagecreate.php and from same page, here is the example snippet
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
HTH!
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