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.
Related
I am merging two png images in my php code using php-gd. The images are getting merged, however the problem is that there is extra transparent space in the image which I want to remove.
Here is my php code.
<?php
$image1Url = "/home/sunpure-refined-sunflower-oil-v-5-ltr-1.png";
$image2Url = "/home/hypercity-every-day-sooji-rawa-v-1-kg-6.png";
$dest = imagecreatefrompng($image1Url);
$src = imagecreatefrompng($image2Url);
$offset2x = imagesx($src);
$offset3x = imagesx($dest);
$temp = imagecreatetruecolor($offset2x + $offset3x, 140);
$background = imagecolorallocate($temp, 0, 0, 0);
imagecolortransparent($temp, $background);
imagecopymerge($temp, $src, 0, 0, 0, 0, 180, 180, 100); //have to play with these numbers for it to work for you, etc.
imagecopymerge($temp, $dest, $offset2x-50, 0, 0, 0, 180, 180, 100);
header('Content-Type: image/png');
imagepng($temp, "/home/myImage.png");
imagedestroy($dest);
imagedestroy($src);
In the rightmost image, I was trying to remove the space between them , but due to transparent space, its not happening.
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
Is there a php or javascript method of converting a simple html file with text to a JPEG image and saving it automatically. I would like to be able to pull it into Outlook 2007 as "embedded content" for a company's dynamic disclaimer
Any help would be appreciated
UPDATE
i have found the following script that produces a image with only a string:
<?php
$text = "This is a example";
header("Content-Type: image/png");
$im = #imagecreate(700, 300)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 12, 5, 5, $text, $text_color);
imagepng($im);
imagedestroy($im);
?>
Is there a way to get <br> tags to work in a php string?
use the imageTTFtext() function and concatenation the <br /> tag.
<?php
header(“Content-type: image/gif”);
$string = 'This is another text i want to include';
$im = imagecreate( 400, 200 );
$red = imagecolorallocate($image, 255,0,0);
$blue = imagecolorallocate($image, 0,0,255 );
$font = “/usr/local/jdk121_pre-v1/jre/lib/fonts/LucidaSansRegular.ttf”;
imageTTFtext( $im, 50, 0, 20, 100, $blue, $font, “This is some text!” );
imagegif($im);
?>
I created a canvas with a width of 400 pixels, you'll need a FreeType font in addition to your GD library. with the imageTTFtext() function i defined a width of 50 and angle of 0 of the text. so to make the string more complex, you can place the <br /> tag like this:
imageTTFtext( $im, 50, 0, 20, 100, $blue, $font, “This is some text!<br/>”.$string."some other text" );
Hope you get the picture now...
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/
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);