Create image in gd library - php

I am copying image over an image using GD library But it is not showing properly. This is my code:
<?php
$im = imagecreate(288,288);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($bg, true);
imagesavealpha($bg, true);
imagecopymerge($im, $bg, 10, 10, 0, 0, 100, 47, 25);
?>
i cannot upload image due to not enough reputation but i can explain that when i have a smiley where i have to overlap over an other image but when i run this function a grey color box overlap on the image instead of smiley. Smiley is also in grey color.
Please help...
This is the link for the image create after function runs
http://classicsouls.com/main/9695.png
I want to overlap this image
http://classicsouls.com/main/smile7.png

Try to set imagealphablending also for $im:
$im = imagecreate(288,288);
imagealphablending($im, true);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($bg, true);
imagesavealpha($bg, true);
imagecopymerge($im, $bg, 10, 10, 0, 0, 100, 47, 25);

Related

I have code that works, with picture uploaded from gimp. But image created with gd not

My problem is this, I want to create image thats combination of 3 image, first is yellow background, then I use PHP and GD to write some text on it, text is black, so I made it transparent in next step, then I want to put that picture over background picture, so text has texture on it. It works fine, if I upload PNG from my computer created in Gimp, but picture created with gd has transparency on it but result is again yellow background with black letters.
how it should be
good result
what I get now from code:
enter link description here
how it gets now with image created from gd
<?php
header('Content-Type: image/png');
$title = "PULEŽANI";
$im = imagecreatetruecolor(1200, 320);
//$im = imagecreatetruecolor(1200, 320);
$white = imagecolorallocate($im, 255, 255, 255);
$crna = imagecolorallocate($im, 0, 0, 0);
$black = imagecolorallocatealpha($im, 255, 255, 255, 127);
$yellow = imagecolorallocate($im, 251, 189, 8);
// kreiram kvadrat sa žutom pozadinom
imagefill($im, 0, 0, $yellow);
$font = "/AlrightSans-Ultra-v3.ttf";
//dodajem text na žutu pozadinu
imagettftext($im, 122, 0, 40, 160, $crna, $font, $title);
//kreiram sliku crni tekst na žutoj pozadini
imagepng($im, 'sl.png');
imagedestroy($im);
//ovaj dio bi trebao napraviti da crna slova postanu prozirna
$image = imagecreatefrompng('sl.png');
$odabirprozirne = imagecolorallocatealpha($image, 0, 0, 0,127);
imagealphablending($image, true);
imagecolortransparent($image,$odabirprozirne);
imagepng($image, 'sl114.png');
imagedestroy($image);
/* dodaj zvijezde odispod */
$image_1 = imagecreatefrompng('TexturaZvijezde.png');
$image_2 = imagecreatefrompng('sl114.png');
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 1200, 120);
imagepng($image_1);
imagedestroy($image_1);
I haven't tested this, but according to the manual for imagesavealpha function,
You have to unset alphablending (imagealphablending($im, false)), to use it.
Example:
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);

Copy one image on top of the next, while retaining transparency

I'm struggling with PHP's GDLib just a little bit here. I'm trying to overlay two .PNG images on top of eachother, which so far works fine.
The one problem I run into is that sometimes, the overlay image comes with a white background. I can make this transparent (using the imagecolortransparent function), but this transparency isn't saved when I copy this image onto a new one.
// Load the background image first
$background = imagecreatefrompng($this->background);
// Load the overlaying image next, and set white as a transparent color
$overlay = imagecreatefrompng($this->image);
imagecolortransparent($overlay, imagecolorallocate($overlay, 255, 255, 255));
// So far, this all works. But when I create a new image,
// and paste both $background and $overlay into it,
// $overlay loses transparency and reverts to a white fill.
$image = imagecreatetruecolor(16, 16);
imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $trans_colour);
imagecopyresampled($image, $background, 0, 0, 0, 0, 16, 16, 16, 16);
imagecopyresampled($image, $overlay, 0, 0, 0, 0, 16, 16, 16, 16);
#mkdir(dirname($file), 0777, true);
imagepng($image, $file);
// The new $image is now mostly white. The transparency on $overlay
// was lost, meaning that the $background image is completely invisible.
How can I keep the transparency when copying $overlay into a new image?
$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";
$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);
imagecopy($im, $im2, (imagesx($im) / 2) - (imagesx($im2) / 2), (imagesy($im) / 2) - (imagesy($im2) / 2), 0, 0, imagesx($im2), imagesy($im2));
// Save alpha for the destination image.
imagesavealpha($im, true);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);
// Save final image after placing one on another image
imagepng($im, "output.png", 0);
imagedestroy($im);
imagedestroy($im2);

What’s wrong with this PNG Transparent image generator? The background is Black

First: I want to create a PNG image and draw a shape inside it.
Second: Overlap the first PNG on another image (for example a jpg image)
The problem is: When I create the first PNG with a shape inside (the background is not transparent and is dark) so this make a black overlap on second image.
How can I fix it?
I don’t work so much with images function so I got trouble.
Notes: I need to create the first transparent PNG and then overlap it on second image. I don’t want to create shape directly on second image.
And The Code:
<?php
define('EXAMPLE_TMP_SERVERPATH', '');
define('EXAMPLE_TMP_URLRELPATH', '');
$tempDir = EXAMPLE_TMP_SERVERPATH;
$fileName = 'test3img.png';
$imgW = 125;
$imgH = 125;
# First
$base_image = imagecreatetruecolor($imgW, $imgH);
$black = imagecolorallocate($base_image, 0, 0, 0);
imagecolortransparent($base_image, $black);
$col[0] = imagecolortransparent($base_image, $black);
imagealphablending($base_image, true);
imagesavealpha($base_image, true);
imagefill($base_image, 0, 0, $col[0]);
imagefilledrectangle($base_image, 4, 4, 50, 25, 255);
imagepng($base_image, $tempDir.$fileName);
# First2
$target_image = imagecreatetruecolor($imgW*5, $imgH*5);
$black2 = imagecolorallocatealpha($base_image, 0, 0, 0, 127);
imagecolortransparent($target_image, $black2);
imagecopyresized($target_image, $base_image, 0, 0, 0, 0,$imgW, $imgH, $imgW, $imgH);
imagedestroy($base_image);
imagepng($target_image, $tempDir.$fileName);
imagedestroy($target_image);
# First2
# Second
$dest = imagecreatefromjpeg('../avatar.jpg');
$src = imagecreatefrompng(EXAMPLE_TMP_URLRELPATH.$fileName);
imagealphablending($dest, true);
imagesavealpha($dest, true);
imagealphablending($src, true);
imagecopyresampled(
$dest,
$src,
0,0,
0,0,
200, 200,
125, 125
);
imagepng($dest, EXAMPLE_TMP_URLRELPATH.'_m.jpeg');
imagedestroy($dest);
imagedestroy($src);
?>
Need to add the alpha channel too:
Alpha Channel

Change the colour of the background with php

I need to change the color of the background of the ImageCreateTrueColor to white and then put an image on it
elseif(($height>50)&&($width<50))
{
$img_r = imagecreatefromjpeg($new_img_path);
$source = ImageCreateTrueColor(50, 50);
imagetruecolortopalette($source, FALSE, 2);
$bg = imagecolorat($source, 0, 0);
imagecolorset($source, $bg, 0, 0, 255);
// $white = imagecolorallocate($source,255,255,255);
// imagefilledrectangle($source, 0, 0, 50, 50, $white);
imagecopy($source, $img_r,0,0,0,0,$width,50);
header('Content-type: image/jpeg');
imagejpeg($source, $small_new_img_path);
here is the blue, but it doesn't matter, it doesn't put the image on the blue background
You want to create a png not a JPEG. Use imagepng and imagesavealpha.
See full example.

Clickable links imagecreatefrompng

At this moment i'm trying to create a dynamic PHP image, and i'm not really sure if it is even possible to have clickable links in the following piece of code:
<?php
$image = "hotelview_val13.png";
$src = 'pixel.png';
putenv('GDFONTPATH=' . realpath('.'));
$font = 'font.ttf'; //Ubuntu font
$im = imagecreatefrompng($image);
imagealphablending($im, true);
imagesavealpha($im, true);
imagealphablending($src_to_copy, true);
imagesavealpha($src_to_copy, true);
imagealphablending($pg, true);
imagesavealpha($pg, true);
$wc = ImageColorAllocate ($im, 255, 255, 255);
$red = ImageColorAllocate ($im, 255, 0, 0);
$blk = imagecolorallocate($im, 0, 0, 0);
{
imagettftext($im, 12, 0, 45, 310, $blk, $font , "Link");
imagettftext($im, 12, 0, 45, 330, $blk, $font , "Veel plezier ;)");
}
header("Content-Type: image/png");
Imagepng($im);
ImageDestroy ($im);
?>
What i'm trying to do is to make a clickable link. I've tried to just simply put into the code, but that doesn't work because it displays the code as plain text. Is it possible? If it is, how does it work?
Thanks a lot for your time.
You cannot embed links in an image. The only way to make a region of an image clickable is, when viewed in a browser, to use an HTML map.

Categories