I need to combine two images in PHP. Master picture is in PNG, secondary in JPG.
First picture:
(source: grafikstudio-m.com)
Secondary picture:
(source: grafikstudio-m.com)
Test:
http://happywin.konektor.grafikstudio-m.com/imageMerge/
<?php
$dest = imagecreatefrompng('muster.png');
$src = imagecreatefromjpeg("test.jpg");
imagecolortransparent($src);
imagesavealpha($dest, false);
imagealphablending($src, true);
imagesavealpha($src, true);
imagecopymerge($dest, $src, 0, 0, 0, -100, 2000, 1300, 50);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
I need the pictures to be connected and with a white background
Create a canvas image with same size as muster.png and fill it with white color.
$muster = imagecreatefrompng('muster.png');
$canvas = imagecreatetruecolor(imagesx($muster), imagesy($muster));
$white = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $white);
Open test.jpg and use the top-left pixel color as the transparent color.
$test = imagecreatefromjpeg('test.jpg');
imagecolortransparent($test, imagecolorallocate($test, 255, 255, 255));
Copy the 2 images to the canvas, the order is optional, notice that copy muster.png (truecolor) with imagecopy but copy test.jpg (pallete) with imagecopymerge.
imagecopy($canvas, $muster, 0, 0, 0, 0, imagesx($muster), imagesy($muster));
imagecopymerge($canvas, $test, 0, 100, 0, 0, imagesx($test), imagesy($test), 100);
header('Content-Type: image/png');
imagepng($canvas);
// ...destroy
Related
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);
?>
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);
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
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.
I tried to duplicate a PNG image, which has drop shadow (i.e. alpha channel) and transparent background. However, the resulting image paints the shadow and transparent background with black. I tried with imagecopy and imagecopymerge; neither yielded to valid results, which aren't the same with the original image.
Preview of the images.
$src = imagecreatefrompng('img_box3-bg.png');
/* Using imagecopy. */
$dest = imagecreatetruecolor(116, 100);
imagecopy($dest, $src, 0, 0, 0, 0, 116, 100);
imagepng($dest, 'img_box3-bg.imagecopy.png');
imagedestroy($dest);
/* Using imagecopymerge. */
$dest2 = imagecreatetruecolor(116, 100);
imagecopymerge($dest2, $src, 0, 0, 0, 0, 116, 100, 100);
imagepng($dest2, 'img_box3-bg.imagecopymerge.png');
imagedestroy($dest2);
imagedestroy($src);
Help? Thanks beforehand.
Something like this:
$src = imagecreatefrompng('img_box3-bg.png');
/* Using imagecopy. */
$dest = imagecreatetruecolor(116, 100);
// this is new
imagesavealpha($dest, true);
$transparent = imagecolorallocatealpha($dest, 0, 0, 0, 127);
imagefill($dest, 0, 0, $transparent);
imagecopy($dest, $src, 0, 0, 0, 0, 116, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);