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/
Related
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.
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
I've tried a few times but I can't seem to get 2 centered lines of text at the bottom of this image on the transparent background
Any suggestions?
<?php
$filePath = "adopt.png"; //full path to your png, including filename and extension
$img = #imagecreatefrompng($filePath);
$width = imagesx($img);
$height = imagesy($img);
//create new image and fill with background color
$backgroundImg = #imagecreatetruecolor($width, $height);
imagecopy($backgroundImg, $img, 0, 0, 0, 0, 100, 130);
$color = imagecolorallocatealpha($backgroundImg, 0, 0, 0, 127); //fill transparent back
imagefill($backgroundImg, 0, 0, $color);
//save as png
header( "Content-type: image/png" );
imagepng( $backgroundImg );
imagedestroy( $backgroundImg );
?>
This process works. Without your code I can only regurgitate the example from the manual to you. http://php.net/manual/en/function.imagestring.php. But it works, I have used the imagestring function. It accepts co-ordinates for the text so I can not see why it would not work for you.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
I want to create a custom captcha.
so i need to convert text and background image as single image; Now i can use only background color in imagecolorallocate()here is my code
<?php
$text = rand (1000, 9999);
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img,255, 255, 255);
$text_colour = imagecolorallocate( $my_img, 0, 0, 255 );
$line_colour = imagecolorallocate( $my_img, 255, 255, 255 );
imagestring( $my_img, 4, 30, 25, $text, $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );
header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>
<?php
function LoadJpeg($imgname)
{
if(!$im)
{
/* Create a black image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/jpeg');
$img = LoadJpeg('bogus.image');
imagejpeg($img);
imagedestroy($img);
?>
You can use the imagecreatefromjpg PHP function, or any of its associated functions based on the image type, like imagecreatefrompng.
This will create an image element in PHP from the selected file that you can use as a background file.
From the PHP manual.
Your next option is to use imagecopymerge() that will use an existing image element and merge it with another image. Info from the PHP manual
For adding a background to your new image you need to use imagecreatefrompng instead of imagecreate.
You can go through another stack overflow link
Below is the code to generate an image dynamically in php. I want to add some image in this dynamically created image. I've already tried my luck with imagecopy but failed because I think it deals with the already created image to copy one onto another.
<?php
$my_img = imagecreate( 1200, 630 );
$background = imagecolorallocate( $my_img, 2,144,162 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$text_colour1 = imagecolorallocate( $my_img, 255, 255, 255 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
$font='C:\Windows\Fonts\ARLRDBD.ttf';
imagettftext($my_img, 48, 0, 450, 100, $text_colour, $font,"Hello World");
$font1='C:\Windows\Fonts\Arial.ttf';
imagettftext($my_img, 32, 0, 650, 160, $text_colour1, $font1,Hello World" );
imagettftext($my_img, 32, 0, 500, 300, $text_colour1, $font1,"Hello World" );
imagettftext($my_img, 32, 0, 560, 440, $text_colour1, $font1,"Hello World" );
header( "Content-type: image/png" );
imagepng( $my_img );
imagepng( $my_img,'Ethers.png' );
imagecolordeallocate( $text_colour );
imagecolordeallocate( $text_colour1 );
imagecolordeallocate( $background );
imagedestroy($my_img);
header('Content-Type: image/png');
imagedestroy($my_img);
?>
There are readymade code available for playing with image edits.
Find the class from the following link.
http://www.phpclasses.org/package/6323-PHP-Manipulate-GIF-JPEG-and-PNG-images.html
This class can be used to manipulate GIF, JPEG and PNG images.
It can load an image in GIF, JPEG and PNG formats and perform several types of operations.
Currently it can resize the image, render a text on the image, render a watermark image and apply several types of effects like blur, edge detection, sharpen, emboss, etc..
The resulting image can be saved to a file or displayed as the current script output also in GIF, PNG and JPEG formats.