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
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.
im trying to make a form with php and gd library.
form contains a png logo with transparent packgroung.
now i have 3 problems:
when i make a border then i want to place the logo in top corner of
image, its not transparented background anymore
im using persian font and using imagettftext function. it show the
characters but in persian we have merged words but it show
characters seperated
how can i draw rounded corner borders
here is my code:
$fontSize=4;
$width = imagefontwidth($font) * strlen($string)+10 ;
$height = imagefontheight($font) ;
$handle = ImageCreate (800, 400) or die ("Cannot Create image");
$logo = imagecreatefrompng( 'Logo.png' );
$bg_color = ImageColorAllocate ($handle, 255, 240, 250);
$txt_color = ImageColorAllocate ($handle, 0, 0, 0);
$title="فرم قرارداد";
$font = "IRANSans.ttf";
$title_size = 18;
imagettftext( $handle, $title_size, 0, 620, 100, $txt_color, $font, $title );
$black = imagecolorallocate($handle, 0, 0, 0);
imagerectangle($handle, 20, 20, 780, 380, $black);
imagecopy($handle, $logo, 10, 10, 0, 0, 161, 160);
header('Content-Type: image/png');
imagepng($handle);
See this answer: php GD create a transparent png image
You'll have to use an OpenType (.otf) font, if you want to make use of your language's typographic characteristics.
You might want to take a look at GD's imagearc.
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.
I am creating image using GD library all the functions are working fine. But the main problem where i stucked that i want to merge png image over an other image but after overlapping it cannot merge properly and looking like jpg or other instead of png. I cannot upload my image here due to low reputation so click on these links below to see the image.
The image which i want to merge is this
Png image
The image where i merge above image is:
My code is here:
<?php
$im = imagecreate(288,288);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($im, true);
imagesavealpha($bg, true);
imagecopyresampled($im, $bg, 80, 80, 0, 0, 50, 50, 185, 185);
header("Content-Type: image/png");
$filename = $_SESSION['rand'].'.png';
imagepng($im,$filename);
echo '<img src="'.$filename.'" alt="" />';
?>
Your background image doesn't have an alpha channel. This makes the PHP GD library do all of it's copying operations without using an alpha channel, instead just setting each pixel to be fully opaque or transparent, which is not what you want.
The simplest solution to this is to create a new image of the same size as the background that has an alpha channel, and then copy both the background and face into that one.
$baseImage = imagecreatefrompng("../../var/tmp/background.png");
$topImage = imagecreatefrompng("../../var/tmp/face.png");
// Get image dimensions
$baseWidth = imagesx($baseImage);
$baseHeight = imagesy($baseImage);
$topWidth = imagesx($topImage);
$topHeight = imagesy($topImage);
//Create a new image
$imageOut = imagecreatetruecolor($baseWidth, $baseHeight);
//Make the new image definitely have an alpha channel
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
imagecopy($imageOut, $baseImage, 0, 0, 0, 0, $baseWidth, $baseHeight); //have to play with these
imagecopy($imageOut, $topImage, 0, 0, 0, 0, $topWidth, $topHeight); //have to play with these
//header('Content-Type: image/png');
imagePng($imageOut, "../../var/tmp/output.png");
That code produces this image:
I have an image that contains transparency which I am merging with another image (created in php) along with getting some text added. Currently the transparency DOES seem to work, but it makes the background transparent behind it, leaving a large cutout in the image:
//creates a image handle
$img = imagecreate( 500, 200 );
$logo = imagecreatefrompng('logo.png');// <--- logo with transparent background, png24 from photoshop
imagealphablending($logo, true);
imagesavealpha($logo, true);
//choose a bg color, u can play with the rgb values
$background = imagecolorallocate( $img, 173, 184, 194);
//chooses the text color
$text_colour = imagecolorallocate( $img, 255, 255, 255 );
//sets the thickness/bolness of the line
imagesetthickness ( $img, 3 );
//pulls the value passed in the URL
$text = $_GET['name'];
$pos = $_GET['title'];
// place the font file in the same dir level as the php file
$font = 'NeutraText-BoldAlt.ttf';
//this function sets the font size, places to the co-ords
imagettftext($img, 30, 0, 11, 128, $text_colour, $font, $text);
//places another text with smaller size
imagettftext($img, 16, 0, 10, 155, $text_colour, $font, $pos);
// PUC
imagettftext($img, 16, 0, 10, 180, $text_colour, $font, "My Organization");
// fix trans
imagealphablending($img, false);
imagesavealpha($img, true);
// Merge the images
imagecopyresampled($img, $logo, 10, 10, 0, 0, 150, 78, 150, 78);
//alerts the browser abt the type of content i.e. png image
header( 'Content-type: image/png' );
//now creates the image
imagepng( $img );
//destroys used resources
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $img );
What do I need to do to preserve the transparency of $logo when added to $img?
I would recommend using the phpThumb library, it comes with a lot of nice features including preserving transparency. It also works with GDLib or ImageMagick:
http://phpthumb.sourceforge.net/