I have GD library installed as you can see.(image)
but when i try to create a image it shows special characters instead of the image.(image)
I'm using laravel and Xampp.
Anyone Know what is the problem ?
<?php
// create a blank image
$image = imagecreatetruecolor(400, 300);
// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);
// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);
// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);
// output the picture
header("Content-type: image/png");
imagepng($image); ?>
If you want your image to show as an image, you need to remove any output before sending a header. Remove var_dump(gd_info()); from your code to see the actual image:
<?php
// create a blank image
$image = imagecreatetruecolor(400, 300);
// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);
// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);
// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);
// output the picture
header("Content-type: image/png");
imagepng($image); ?>
From https://www.php.net/manual/en/function.header.php:
"Remember that header() must be called before any actual output is sent"
Related
I'm trying to overlap a transparent png image over another png. I followed other answers but I didn't manage to solve my problem.
Here it is what I've done so far:
header("Content-type: image/png");
$im = imagecreatefrompng("image/orange.png");
$trans_background = imagecolorallocate($im, 0, 0, 0, 127);
$logo = imagecreatefrompng($data["badgeUrls"]["medium"]);
imagesavealpha($logo, true);
imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, 200, 200);
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
and this is the result:
result
EDIT:
logo.png it's transparent. If I try this code on html using the same link, it works:
<!DOCTYPE html>
<html>
<body style="background-color:powderblue;">
<img src="https://api-assets.clashofclans.com/badges/200/lTvtX122PSoz5wXzrzp5mFlw0y-72zVviKvuy9cXOFs.png">
</body>
</html>
Result 2: pic
I have modified your code by commenting some lines and adjusting the dimensions to prevent the black background:
<?php
header("Content-type: image/png");
$im = imagecreatefrompng("orange.png");
//$trans_background = imagecolorallocate($im, 0, 0, 0, 127);
$logo = imagecreatefrompng('logo.png'); //logo image dimensions = 64 x 64 px
//imagesavealpha($logo, true);
//imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, 64, 64); // dimensions of the logo image 64 * 64 px
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
The envelope image in the screen shot is transparent png image of dimensions 64 * 64 px.
Update:
If you want to add a custom background to the logo image and you want to determine its dimensions dynamically, you will have to use imagesx() for getting width and imagesy() for getting height. Also, you have to use imagecolorallocatealpha() in your code instead of imagecolorallocate()
<?php
header("Content-type: image/png");
$im = imagecreatefrompng("orange.png");
$trans_background = imagecolorallocatealpha($im, 0, 0, 100, 50);
// Blue background // to be transparent imagecolorallocatealpha ($im, 0,0,0,127)
$logo = imagecreatefrompng('logo2.png');
imagesavealpha($logo, true);
imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, imagesx($logo), imagesy($logo));
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
Transparent Example:
I have an transparent image with a bar through the middle...
http://i66.tinypic.com/2zyeg4h.png
Using php I would like to remove the middle part on the bar, leaving a transparent space in the middle of the bar. I try this code...
$im = imagecreatefrompng('****root****/image.png');
//make a yellow box
$transparent = imagecolorallocate($im, 255, 255, 0);
//make the yellow box transparent
imagecolortransparent($im, $transparent);
imagefilledrectangle($im, 200, 115, 300, 137, $transparent);
imagealphablending($im, false);
imagesavealpha($im, true);
header ('Content-Type: image/png');
imagepng($im);
$save = "****root****/test.png";
imagepng($im, $save);
imagedestroy($im);
?>
But the output image has the rectangle retain the yellow colour, rather than turn it transparent.
http://i67.tinypic.com/1ny6j4.png
Where am I going wrong? If I remove...
imagealphablending($im, false);
imagesavealpha($im, true);
I get the transparent box in the center (in the browser), but the rest of the image loses it's transparency and has a white background instead, also when I download that image and open in a photo editor, the transparency box in the center is gone. I have GD libraries set up on my server.
You need to use the gd function imagecolorallocatealpha to create a transparent color.
You could generate the whole image with PHP.
e.g.
<?php
// generate empty image
$image = imagecreate(500, 262);
// define black
$black = imagecolorallocate($image, 0, 0, 0);
// create left part of the bar
imagefilledrectangle($image, 0, 110, 200, 162, $black);
// create right part of the bar
imagefilledrectangle($image, 300, 110, 500, 162, $black);
// save image
imagepng($image, 'outimage.png');
If you want to stick with an existing image
<?php
// generate empty image
$image = imagecreatefrompng('input.png');
// define transparent
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
// create the transparent area
imagefilledrectangle($image, 200, 110, 300, 162, $transparent);
// save image
imagepng($image, 'output.png');
input.png
output.png
You don't need to send a header if you're not submitting the image as a result to a browser.
e.g.
header('Content-Type: image/png');
imagepng($image);
Would display the image in the requesting browser rather than saving it to a file.
I am executing my following code for creating a transparent image but everytime it shows me black background.
kindly tell me my my fault in the code.
<?php
//set the content type
header('Content-type: image/jpeg');
//create the image
$im = imagecreatetruecolor(250, 200);
$black = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
// Make the background transparent
imagecolortransparent($im, $black);
//text to draw
$text=$_POST['text'];
//font path
$font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
// Add the text
imagettftext($im, 15, 0, 50, 50, -$blue, $font, $text);
//view the image
imagejpeg($im);
imagedestroy($im);
?>
You cannot make jpeg images transparent. Use png instead
Change below 2 lines:
header('Content-type: image/png');
imagepng($im);
Update
Reference Link: create transparent png image
I'm trying to create a URL, that spits back a 1x1px png with a certain alpha value e.g. www.mysite/com/png.php?alpha=50 which will output a 1x1 png, with the background colour set to black, and having the opacity at 50%.
I've searched through lots of tutorials and posts trying to figure out how to do this, but I can't find anything that works. Is this possible to do with PHP alone?
Here's the closest I've gotten
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(500, 300);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocatealpha($im, 255, 255, 255, 50);
imagefilledrectangle($im, 0, 0, 500, 300, $black);
// Save the image
imagepng($im);
imagedestroy($im);
?>
However, that seems to overlay a 50% black colour on top of a white background.
Set the white to transparent, by adding these lines after $black = ...
$colourWhite = imagecolorallocate($im,255,255,255);
imagecolortransparent($im,$colourWhite);
I need to change the color of the background of the ImageCreateTrueColor to white and then put an image on it
elseif(($height>50)&&($width<50))
{
$img_r = imagecreatefromjpeg($new_img_path);
$source = ImageCreateTrueColor(50, 50);
imagetruecolortopalette($source, FALSE, 2);
$bg = imagecolorat($source, 0, 0);
imagecolorset($source, $bg, 0, 0, 255);
// $white = imagecolorallocate($source,255,255,255);
// imagefilledrectangle($source, 0, 0, 50, 50, $white);
imagecopy($source, $img_r,0,0,0,0,$width,50);
header('Content-type: image/jpeg');
imagejpeg($source, $small_new_img_path);
here is the blue, but it doesn't matter, it doesn't put the image on the blue background
You want to create a png not a JPEG. Use imagepng and imagesavealpha.
See full example.