How i can generate the below image using php
I used the below php code to generate the image
<?php
header("Content-Type: image/png");
$im = imagecreate(300, 300);
$background_color = imagecolorallocate($im, 21, 125, 126);
$black = ImageColorAllocate($im, 0, 0, 0);
$blue = ImageColorAllocate($im, 0, 0, 255);
$white = ImageColorAllocate($im, 255, 255, 255);
ImageFilledEllipse($im, 50, 120, 75, 75, $white);
ImageFilledEllipse($im, 250, 100, 75, 75, $white);
imagepng($im);
imagedestroy($im);
?>
But i dint get the original image smoothness
PHP Generated Image
Apply filter to your image using imagefilter() with the IMG_FILTER_SMOOTH argument.
imagefilter($im, IMG_FILTER_SMOOTH,100);
imagecreate creates a palette-based image, which does not include anti-aliasing (in particular because you haven't allocated the "in-between" colours). Try imagecreatetruecolor instead.
If you are creating an image of a particular size then no matter how you do it, when you zoom into such an image you are always going to get unsmooth edges.
The problem is you are zooming in on the browser after the PHP image has been rendered. Why not use a vector based library in javascript to draw your images, that way when you zoom in the vector graphic is redrawn for the appropriate scale. Eliminating your jagged edges.
Related
I am starting on php and am trying to create an image using php-gd, but keep getting a weird output.
I am using xampp.
Here's my code: (a black line should be seen into the screen)
<?php
header ("Content-type: image/png");
$image = imagecreate(200, 50);
$black = imagecolorallocate($image, 0, 0, 0);
ImageLine ($image, 30, 30, 120, 120, $black);
imagepng($image);
?>
I tried with many samples found in the internet but i keep getting
, for anything I'm trying to do. (from showing text to creating a watermark). I use examples already tested by others, so the code isn't probably the problem.
Could you please say me what is wrong?
Edit : I am actually on the right window
(my file is stored at C:\xampp\htdocs\projet_30_03_21\template.php)
Edit : Ctrl+F5 doesn't work either. Also tried creating a new window.
You could use imagefill() to define a background before to add a black line, because the background is already black.
It's also recommanded to use imagecreatetruecolor() instead of imagecreate().
$image = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);
imageline($image, 30, 30, 120, 120, $black);
header("Content-type: image/png");
imagepng($image);
Give the following image
Found the problem (https://www.php.net/manual/fr/install.windows.legacy.index.php#install.windows.legacy.building).
I had to uncomment this line of code :
;extension=gd
to
extension=gd.
Here is the code that I am using currently.
$image = imagecreatetruecolor(400, 300);
imagesavealpha($image, true);
imagesetthickness($image, 2);
$red = imagecolorallocate($image, 255, 0, 0);
$green = imagecolorallocate($image, 0, 255, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 0);
imagefill($image, 0, 0, $transparent);
imagerectangle($image, 2, 2, 398, 298, $red);
imagedashedline($image, 0, 150, 400, 150, $blue);
imagedashedline($image, 200, 0, 200, 300, $blue);
imagepolygon($image, array( 10, 180, 10, 250, 110, 250), 3, $red);
// imageflip($image, IMG_FLIP_VERTICAL);
ob_start();
imagepng($image);
printf('<img src="data:image/png;base64,%s"/>', base64_encode(ob_get_clean()));
imagedestroy($image);
The attached image is result of running the above code.
I want to flip the triangle in the bottom right corner and draw it along with the original image. I tried to use imageflip() but it draws over the original image.
I know that imageflip() is flipping the rectangle as well as dotted lines but they are symmetric so it is no big deal.
Setting the fill color to transparent did not work. Any help would be appreciated.
My aim is to take original image, flip it along the horizontal line and then flip the resulting image along vertical line.
Once I know how to flip along the horizontal line without loosing the original image, I can try to do the rest on my own.
if you want you can use javascript if you know it to get the style of it then add a new one but that can be complicated. I can walk you through it, although that will take quite a bit of time. I can help if you have a github
If you want to preserve original image before flip, then use imagecopy() to duplicate image then call imageflip() using new duplicate image.
$flipImage = imagecreatetruecolor(400, 300);
imagecopy($flipImage, $image, 0, 0, 0, 0, 400, 300);
imageflip($flipImage, IMG_FLIP_HORIZONTAL);
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'm trying to play around with GD, and I'm trying to get it to work with large images. I want a image that's originally 640x640 to resize to 130x130 on my image that I'm creating in GD. However, with my code it just crops 130x130 of the image from the upper left corner. In other words, I don't get the whole image in 130x130. I've been trying every snippet I could find, but still no luck in getting a hold of this. This is the code I have;
header ("Content-type: image/jpeg");
$image1Url = "background.jpg";
$image2Url = "image.jpg";
$image1 = imageCreateFromjpeg($image1Url);
$image2 = imageCreateFromjpeg($image2Url);
imagecopymerge($image1, $image2, 10, 10, 0, 0, 130, 130, 100);
$line1 = "This is the first line";
$line2 = "This is the second line";
$font = "./VERDANA.TTF";
$white = imagecolorallocate($image1, 255, 255, 255);
$yellow = imagecolorallocate($image1, 252, 205, 5);
imagefttext($image1, 14, 0, 150, 110, $yellow, $font, $line1);
imagefttext($image1, 14, 0, 150, 135, $white, $font, $line2);
Imagejpeg ($image1, NULL, 100);
ImageDestroy ($image1);
ImageDestroy ($image2);
I want the image specified as $image2Url to be scaled down to 130x130 no matter what size it's originally is. It's important to me that I maintain the aspect ratio though.
I've been trying different snippets I could find, but still no luck... I've been able to resize the original image to the size I want, but not within the final image in my GD script.
If you're using PHP version >= 5.5 you should use imagescale(). If not, use the following right after loading $image2:
$image3 = imagecreatetruecolor(130,130);
list($image2w, $image2h) = getimagesize($image2Url);
imagecopyresampled($image3, $image2, 0, 0, 0, 0, 130, 130, $image2w, $image2h);
// then use $image3 instead of $image2
What is the best way to display underlined text and output the result as image with GD or any other library?
You can try using the Unicode underline combining character U+0332.
<?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);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = "̲U̲d̲e̲r̲l̲i̲n̲e";
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
There are lots of FREE PHP CAPTCHA out there that come with a lot of customization, download one and see what exactly happens behind the scene. Also have a look at this link
HTH
I am using this...
$font = imageloadfont($font_file);
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
$str_width = strlen($text)*$font_width;
ImageLine($image, $left, $top+$font_height, $left+$str_width, $top+$font_height, $color);