I'm trying to overlap a transparent png image over another png. I followed other answers but I didn't manage to solve my problem.
Here it is what I've done so far:
header("Content-type: image/png");
$im = imagecreatefrompng("image/orange.png");
$trans_background = imagecolorallocate($im, 0, 0, 0, 127);
$logo = imagecreatefrompng($data["badgeUrls"]["medium"]);
imagesavealpha($logo, true);
imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, 200, 200);
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
and this is the result:
result
EDIT:
logo.png it's transparent. If I try this code on html using the same link, it works:
<!DOCTYPE html>
<html>
<body style="background-color:powderblue;">
<img src="https://api-assets.clashofclans.com/badges/200/lTvtX122PSoz5wXzrzp5mFlw0y-72zVviKvuy9cXOFs.png">
</body>
</html>
Result 2: pic
I have modified your code by commenting some lines and adjusting the dimensions to prevent the black background:
<?php
header("Content-type: image/png");
$im = imagecreatefrompng("orange.png");
//$trans_background = imagecolorallocate($im, 0, 0, 0, 127);
$logo = imagecreatefrompng('logo.png'); //logo image dimensions = 64 x 64 px
//imagesavealpha($logo, true);
//imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, 64, 64); // dimensions of the logo image 64 * 64 px
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
The envelope image in the screen shot is transparent png image of dimensions 64 * 64 px.
Update:
If you want to add a custom background to the logo image and you want to determine its dimensions dynamically, you will have to use imagesx() for getting width and imagesy() for getting height. Also, you have to use imagecolorallocatealpha() in your code instead of imagecolorallocate()
<?php
header("Content-type: image/png");
$im = imagecreatefrompng("orange.png");
$trans_background = imagecolorallocatealpha($im, 0, 0, 100, 50);
// Blue background // to be transparent imagecolorallocatealpha ($im, 0,0,0,127)
$logo = imagecreatefrompng('logo2.png');
imagesavealpha($logo, true);
imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, imagesx($logo), imagesy($logo));
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
Transparent Example:
Related
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
I need to place an image to the center of another image(both horizontally and vertically) with dimension 700*350. I'm trying with the following code. But I'm getting image as stretched.
#header("Content-Type: image/png");
$imageURL = "flower.jpg";
// create a transparent background image for placing the $imageURL image
$imageResource = imagecreatetruecolor(700, 350);
imagesavealpha($imageResource, true);
$transparentColor = imagecolorallocatealpha($imageResource, 0, 0, 0, 127);
imagefill($imageResource, 0, 0, $transparentColor);
$backgroundImage = imagecreatefromjpeg($imageURL);
list($width, $height) = getimagesize($imageURL);
imagecopyresampled($imageResource, $backgroundImage, 350, 175, 0, 0, 700, 350, $width, $height);
imagepng($imageResource, "newimage.jpg");
This is not centering the image and also the file flower.jpg is getting deleted when I run this code. What I'm doing wrong in this?
Can anyone please help me to fix this? Thanks in advance.
So you need something like this?
#header("Content-Type: image/png");
$imageURL = "flower.jpg";
// create a transparent background image for placing the $imageURL image
$imageResource = imagecreatetruecolor(700, 350);
imagesavealpha($imageResource, true);
$transparentColor = imagecolorallocatealpha($imageResource, 0, 0, 0, 127);
imagefill($imageResource, 0, 0, $transparentColor);
$backgroundImage = imagecreatefromjpeg($imageURL);
list($width, $height) = getimagesize($imageURL);
imagecopyresampled($imageResource, $backgroundImage, 175, 85, 0, 0, 350, 175, $width, $height);
imagepng($imageResource, "newimage.jpg");
imagedestroy($imageResource);
imagedestroy($backgroundImage);
You had specified the center of the destination image as the destination coordinates and the whole destination image size instead of needed dimentions of the center rectangle into which the source image would be resized.
Also you didn't do imagedestroy, which you totally should.
I want to resize my images to a square. Say I want a squared image of 500x500 and I have an image of 300x600
I want to resize that image down to 200x500 and then add a white background to it to make it 500x500
I got something working good by doing this:
$TargetImage = imagecreatetruecolor(300, 600);
imagecopyresampled(
$TargetImage, $SourceImage,
0, 0,
0, 0,
300, 600,
500, 500
);
$final = imagecreatetruecolor(500, 500);
$bg_color = imagecolorallocate ($final, 255, 255, 255)
imagefill($final, 0, 0, $bg_color);
imagecopyresampled(
$final, $TargetImage,
0, 0,
($x_mid - (500/ 2)), ($y_mid - (500/ 2)),
500, 500,
500, 500
);
It's doing almost EVERYTHING right. The picture is centered and everything. Except the background is black and not white:/
Anyone know what I'm doing wrong?
I think this is what you want:
<?php
$square=500;
// Load up the original image
$src = imagecreatefrompng('original.png');
$w = imagesx($src); // image width
$h = imagesy($src); // image height
printf("Orig: %dx%d\n",$w,$h);
// Create output canvas and fill with white
$final = imagecreatetruecolor($square,$square);
$bg_color = imagecolorallocate ($final, 255, 255, 255);
imagefill($final, 0, 0, $bg_color);
// Check if portrait or landscape
if($h>=$w){
// Portrait, i.e. tall image
$newh=$square;
$neww=intval($square*$w/$h);
printf("New: %dx%d\n",$neww,$newh);
// Resize and composite original image onto output canvas
imagecopyresampled(
$final, $src,
intval(($square-$neww)/2),0,
0,0,
$neww, $newh,
$w, $h);
} else {
// Landscape, i.e. wide image
$neww=$square;
$newh=intval($square*$h/$w);
printf("New: %dx%d\n",$neww,$newh);
imagecopyresampled(
$final, $src,
0,intval(($square-$newh)/2),
0,0,
$neww, $newh,
$w, $h);
}
// Write result
imagepng($final,"result.png");
?>
Note also, that if you want to scale down 300x600 to fit in 500x500 whilst maintaining aspect ratio, you will get 250x500 not 200x500.
I am executing my following code for creating a transparent image but everytime it shows me black background.
kindly tell me my my fault in the code.
<?php
//set the content type
header('Content-type: image/jpeg');
//create the image
$im = imagecreatetruecolor(250, 200);
$black = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
// Make the background transparent
imagecolortransparent($im, $black);
//text to draw
$text=$_POST['text'];
//font path
$font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
// Add the text
imagettftext($im, 15, 0, 50, 50, -$blue, $font, $text);
//view the image
imagejpeg($im);
imagedestroy($im);
?>
You cannot make jpeg images transparent. Use png instead
Change below 2 lines:
header('Content-type: image/png');
imagepng($im);
Update
Reference Link: create transparent png image
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.