Hay, I'm using this code to generate an image and fill it with a gray colour.
$canvas = imagecreatetruecolor(100, 100);
$gray = imagecolorallocate($canvas, 0xEE, 0xEE, 0xEE);
imagefill($canvas, 0, 0, $gray);
imagegif($canvas);
This works fine, but if i change the canvas size to a 'long' image, it fails to fill.
$canvas = imagecreatetruecolor(1, 100);
Is this a common bug? Or do i need some other option? How do i fill the whole canvas?
Looks like a bug. I confirm the same behavior. It fills only top 2 pixels, if you specify the width to be 1. The similar for 2 or 3. 4 seems like a magic value - it begins to work there.
On the other hand this bug doesn't seem to appear if you use a 1px-high image, i.e. I tried this and it worked as expected:
$canvas = imagecreatetruecolor(100, 1);
$red = imagecolorallocate($canvas, 0xEE, 0, 0);
imagefill($canvas, 0, 0, $red);
imagegif($canvas, "output.png");
So this might be a kind of a workaround.
Related
I am using gd lib to merge several images, but I'm having problems with the transparency. First I merge several images which each have a transparent background (works fine). Then I want to put another picture on top which consists of three parts: one part that I want to keep, a transparent part where the images below should show and one part in a color (e.g. green #00ff00) which is supposed to become transparent as well after merging. The images are pixel images, so I only want #0f0 to become transparent, none other color.
Here is an image of the result of the code.
The first picture shows the merged images ("circle" & "cloud").
The second picture shows the "hat" which is put on top of the merged images.
The third picture shows the result I want to achieve.
The fourth picture is what I actually get.
Whatever I do, I can't seem to find the solution. I'd be grateful for your help!
header("Content-type: image/png");
$img = imagecreatetruecolor(200, 200);
//make an image with transparent background
$transparency = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparency);
imagesavealpha($img, true);
$circle = imagecreatefrompng("testcircle.png"); //black circle
$cloud = imagecreatefrompng("testcloud.png"); //brown cloud
$hat = imagecreatefrompng("testhat.png"); //blue hat with green area
imagecopy($img, $circle, 0, 0, 0, 0, 200, 200);
imagealphablending($img,true);
imagecopy($img, $cloud, 0, 0, 0, 0, 200, 200);
imagealphablending($img,true);
$green = imagecolorallocate($hat, 0, 255, 0);
imagecolortransparent($hat, $green);
imagecopy($img, $hat, 0, 0, 0, 0, 200, 200);
imagealphablending($img,true);
/* //With this the green area just keeps being green as in the second picture
$green = imagecolorallocate($img, 0, 255, 0);
imagecolortransparent($img, $green);
imagecopy($img, $hat, 0, 0, 0, 0, 200, 200);
imagealphablending($img,true);
*/
imagepng($img);
imagedestroy($img);
After searching for a long time, I've found the solution for my problem. I hope it will help if someone has the same question as me in future.
$green = imagecolorallocate($img, 0, 255, 0);
imagecopy($img, $hat, 0, 0, 0, 0, 200, 200);
imagecolortransparent($img, $green);
imagefill($img,0,0,imagecolorallocatealpha($img, 0, 0, 0, 127)); //this line does the trick
First of all I define green as a color. Then I copy the "hat" onto the picture and make the color transparent. Then comes the trick about filling the picture and that makes the green area transparent again.
(I found the solution in a german forum).
I noticed that this code only makes the first green area transparent though. If there are two seperated areas (or even more), (at least) one will stay green. So while this is a solution that works for me, there might be one out there that is even better.
I would like to create a script which is generating an image with one fix background image but display a randomly selected sentence on the image from a list which is previously given in the script.
So far I have a base and I've tried many variations for the random text, but none of them worked until this time.
Here is the script itself:
<?php
header('Content-type: image/png');
$text = 'The text which will be displayed';
$background_path = 'assets/bg_image.jpg';
$image = imagecreatetruecolor(1280, 720);
$background = imagecreatefromjpeg($background_path);
$color = imagecolorallocate($image, 255, 255, 255);
$title_sizes = imagettfbbox(24, 0, 'assets/roboto-medium.ttf', $text);
$title_height = $title_sizes[3];
$title_width = $title_sizes[4];
imagecopy($image, $background, 0, 0, 0, 0, 1280, 720);
imagettftext($image, 24, 0, (640 - $title_width / 2), (360 - $title_height / 2), $color, 'assets/roboto-medium.ttf', $text);
imagepng($image);
imagedestroy($image);
imagedestroy($background);
?>
So the thing is that the line "$text" allows only one sentence to be displayed. I would like to have multiple lines (various sentences) instead which will be displayed on the given background image randomly upon refresh.
Can you please help me to solve this problem?
Thank you very much in advance!
Regards,
Adam
I want to do the following in PHP in combination with GD. ImageMagick is not an option, unfortunately, but this seems like such a common problem that there has to be a solution, I just can't seem to find it.
I want to create a PNG with a transparent background. Then I want to draw a rectangle on it, copy an image on it, and add some text. One way of doing this is as follows:
$image = ImageCreateTrueColor (800, 600);
imagecolortransparent ($image, 0); //0 is pure black, the default fill color
imagerectangle (...);
//code to copy an image
imagettftext ($image, ...);
imagepng ($image);
This works fine, except that part of the copied image might be black, and/or the text might be black. This then also becomes transparent, which is something I don't want.
imagefill ($image, 0,0, 0x7FFF0000);
imagetransparent ($image, 0x7FFF0000);
The above code is something I found online, which fills it with red, then makes red transparent. Again, this causes all red in the image to become transparent. I could choose a color that is unlikely to occur, but I can't guarantee this.
Is there something I'm missing? Can this be fixed?
Thanks for your replies!
imagecolortransparent is probably not what you want here if you're merging images, as single-colour transparency is nasty.
Instead, try it with a transparent fill mask like so:
<?php
$image = imagecreatetruecolor(100, 100);
// Transparent Background
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);
// Drawing over
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 25, 25, 75, 75, $black);
header('Content-Type: image/png');
imagepng($image);
There is an image which has transparent area. (png image)
Now, while doing a imagecopy, can we just fill that transparent area?
Imagemagick can do this easily. Is that possible in php gd?
A layered approach via imagecopymerge() is one route. The concept is to merge your source image onto a new image, with a pre-set background image, which will show through the source image's transparency once merged.
//create main image - transparent, with opaque red square in middle
$img = imagecreate(60, 60);
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white); //make background transparent
$red = imagecolorallocate($img, 255, 0, 0);
imagefilledrectangle($img, imagesx($img) / 4, imagesy($img) / 4, imagesx($img) - (imagesx($img) / 4), imagesy($img) - (imagesy($img) / 4), $red);
//create new image, with pre-filled background, then merge first image across
$img2 = imagecreate(60, 60);
$blue = imagecolorallocate($img2, 0, 0, 255);
imagecopymerge($img2, $img, 0, 0, 0, 0, imagesx($img), imagesy($img), 100);
//output
imagepng($img2);
So the first image creates a transparent image (the white) with a red square in the middle. The second image is simply a blue fill.Merge the two, and the blue shows through the transparent part of the first image, so our red square now sits on the blue fill. Effectively, we've filled the transparent part.
Here's the three states in sequence.
There is nothing to 'fill' where there is transparency. Transparency in a png (or gif) is not the absence of a color, but a single color specifically marked to be shown as transparent. Therefore you want to remove that marker.
Take a look at the php gds function 'imagecolortransparent':
Nope, you can't.
Instead that, you can try to create a copy from a colored rectangle. This code worked for me:
$input = imagecreatefrompng($file_input);
list($width, $height) = getimagesize($file_input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagepng($output, $file_output);
Ok, now it's officialy 10 hours that I'm trying to make this work, but by every minute I'm farther and farther from the final goal...
The thing is, I need to create a function, class or whatever that by the selected TTF or truetype font create, properly resize, and fill the image with a text given by the url. The image shouldn't be longer than that text.
I've given up on all my codes, and the best thing I've got so far is taken from the net ...
<?php
if(!isset($_GET['size'])) $_GET['size'] = 20;
if(!isset($_GET['text'])) $_GET['text'] = "Moj tekst";
$get_font = ( isset( $_GET['font'] ) ) ? $_GET['font'] : 'arial';
$size = imagettfbbox($_GET['size'], 0, "font/".$get_font.".ttf", $_GET['text']);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
$image = imagecreate($xsize, $ysize);
imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);
$transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
Imagefill($image, 0, 0, $transparentColor);
$blue = imagecolorallocate($image, 0, 0, 255);
$white = ImageColorAllocate($image, 255,255,255);
$black = ImageColorAllocate($image, 0,0,0); //-($_GET['size']/20)
imagettftext($image, $_GET['size'], 0, abs($size[0]), abs($size[5]), $black, "font/".$get_font.".ttf", $_GET['text']);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
This code is ok, although the edge of the last or first latter is sometimes partly hidden or just it doesn't fit into the created image (depending on font size), which isn't the problem with smaller font size. And the function i really need is by changing image's rotation to change the entire images proportions so the text fits in.
Example : http://img199.imageshack.us/img199/9368/32739521.jpg
Also, if there is finished function or class that does this, it would be great.. :'( :)
If you are always missing one letter of the end of the beginning, why not just add the width of a single letter to your final size?
In my experience, imagettfbbox doesn't do a very good job of deciding what does and doesn't fit within a bounding box - you need to fudge it a little yourself :)
Hope this helps.
Luke Peterson