Hello I am trying to combine two transparent png-24 images, both size 400width, 150height.
A background: ["http://www.fenixflame.net/Background-Zanaris-24.png"][1]
And the image I want to overlay adobe the background:
["http://www.fenixflame.net/Bandos-Slayer-24.png"][2]
I've tryed overlaying transparent images using php but only png-8 images. Can't use png-8 beacause the images just don't render correctly.
Edit: Code I've tryed:
$image = imagecreatefrompng("http://www.fenixflame.net/Background-Zanaris-24.png");
$frame = imagecreatefrompng("http://www.fenixflame.net/Bandos-Slayer-24.png");
//
//imagealphablending($frame,true);
//
$insert_x = imagesx($frame);
$insert_y = imagesy($frame);
imagecopymerge($image,$frame,0,0,0,0,$insert_x,$insert_y,100);
//
//# Save the image to a file imagepng($image, '/path/to/save/image.png');
imagepng($image, "/home1/fenixfla/public_html/Images/Signatures/NewImageBG.png");
//
//# Output straight to the browser.
imagepng($image);
//
I have write a small example to merge two transparent image in this link scitam.com
try this code it works fine.
$width = 200;
$height = 200;
$base_image = imagecreatefromjpeg("base.jpg");
$top_image = imagecreatefrompng("top.png");
$merged_image = "merged.png";
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height);
imagepng($base_image, $merged_image);
Use GD Library to render the image and output it in php.
http://www.php.net/manual/en/ref.image.php
It gets pretty hairy after that. You have to use start doing things like
imagealphablending($image, false);
imagesavealpha($image, true);
and so on to make sure transparency is correct.
you can see an example of what I did for a client way back on their front page here. It was super tedious and a huge pain. Have fun
How about using lib ImageMagick composite (http://www.imagemagick.org/script/composite.php)
function composite() {
$command = "/usr/local/bin/composite [... your properties...]";
exec($command, $output, $result);
return ($result == 0 && $output[0] == "");
}
You might want to check this out: http://php.net/manual/en/function.imagecopymerge.php
The imagecopymerge function is part of the PHP GD library.
Related
I have a problem here when I need create a image with transparent background. I still don´t know if the problem is with fabricjs or with php. Everything works fine when I sent a image with colored background. The problem occurs when I send a image with transparent background.
The generated image is created with black background.
So, let me explain better:
When the user click in save button, I´m sending the string representation of the canvas to php in the server-side, to be generated the image of the canvas. So I´m using the follow function to sending the string representation of the canvas by Ajax (POST function of jQuery):
function sendStringRepresentation(){
var strDataURI = canvas.toDataURL();
strDataURI = strDataURI.substr(22, strDataURI.length);
$.post("action/createImage.php",
{
str: strDataURI
},
function(data){
if(data == "OK"){
$("#msg").html("Image created.");
}
else{
$("#msg").html("Image not created.");
}
});
}
In PHP file I´m using the follow code to generate the image:
// createImage.php
$data = base64_decode($_POST["str"]);
$urlUploadImages = "../uploads/img/";
$nameImage = "test.png";
$img = imagecreatefromstring($data);
if($img) {
imagepng($img, $urlUploadImages.$nameImage, 0);
imagedestroy($img);
// [database code]
echo "OK";
}
else {
echo 'ERROR';
}
Again, the problem is just with background transparent canvas. With colored background everything works fine.
The last step is quite the opposite:
imagecopyresampled( $img, $alpha_image, 0, 0, 0, 0, $w, $h, $w, $h );
And voila! The image is transparent!
Why did you use GD for this? You can use file_put_contents for save png file from your canvas.
// createImage.php
$data = base64_decode($_POST["str"]);
$urlUploadImages = "../uploads/img/test.png";
file_put_contents($urlUploadImages, $data);
I don't know if this is exactly the problem you're experiencing, but some of the GD library's imagecreate* functions create images without the alpha channel.
The workaround I've found is to create an image using imagecreatetruecolor and copy your transparent image onto it.
Try a process like this:
$img = imagecreatefromstring($data);
$w = imagesx($img);
$h = imagesy($img);
$alpha_image = imagecreatetruecolor( $w, $h );
imagecopyresampled( $alpha_image, $img, 0, 0, 0, 0, $w, $h, $w, $h );
That should ensure that you end up with a "true color" image with the proper alpha channel.
JPG toDataURL transforms transparent background to black.
I had the exact same problem and added this
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
to rodrigopandini's code and it works perfect now.:)
// createImage.php
$data = base64_decode($_POST["str"]);
$urlUploadImages = "../uploads/img/";
$nameImage = "test.png";
$img = imagecreatefromstring($data);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
if($img) {
imagepng($img, $urlUploadImages.$nameImage, 0);
imagedestroy($img);
// [database code]
echo "OK";
}
else {
echo 'ERROR';
}
I have a one color image and I want to change that color with GD library. Image is a PNG and I want to keep transparency as well. I have written following code that keeps transparency and changes color but resulting color is not the color that I used in imagefilter function.
For example if I pass 0,0,255 then resulting image color will be 194,194,255.
Please help me fixing this problem. Here is my code.
<?php
// first we will create a transparent image. an image that has no color.
$width = 294; $height=333;
$image = imagecreatetruecolor($width,$height); //black image of the specified width x height.
imagealphablending($image, false); // set blend mode to false.
$col=imagecolorallocatealpha($image,255,255,255,127); // fill color
imagefilledrectangle($image,0,0,$width,$height,$col);
imagealphablending($image,true);
$shirt = imagecreatefrompng("primary_shirts/shirt.png");
imagesavealpha($shirt, true);
imagefilter($shirt, IMG_FILTER_GRAYSCALE);
imagefilter($shirt, IMG_FILTER_COLORIZE, 0,0,255);
imagecopy($image, $shirt, 0, 0, 0, 0, $width, $height);
imagealphablending($image,true);
imagealphablending($image,false);
imagesavealpha($image,true);
if(imagepng($image, "primary_shirts/hello.png", 1)){
echo "http://localhost/site/primary_shirts/hello.png";
}
imagedestroy($image);
imagedestroy($shirt);
?>
EDIT: I m trying to colorize an image to blue color(0,0,255). This script colorize image but resulting image is not (0,0,255) instead it is (76,76,255). Why so?
if(imagepng($image, "primary_shirts/hello.png", 1)){
should be
if(imagepng($image, "primary_shirts/shirt.png", 1)){
?
And where's your header("Content-type: image/png");?
I don't know what the contents of hello.png nor shirt.png are. But try to stick to the simplest example (unless you're going to provide more info)
$red = 0; $green = 204; $blue = 204;
$shirt = "primary_shirts/shirt.png";
$im = imagecreatefrompng ($shirt);
imagesavealpha($im, true);
imagefilter($im,IMG_FILTER_GRAYSCALE);
if($im && imagefilter($im,IMG_FILTER_COLORIZE, $red-195, $green-195, $blue-195 ) )
{
imagepng($im, 'primary_shirts/hello.png');
echo "http://localhost/site/primary_shirts/hello.png";
}
it seems IMG_FILTER_COLORIZE does not really do what php.net says. It in fact subtracts an image color from what you pass it. Above code is exactly doing what i want it to do so far.
I use this code to create an image from another png image, the background is black by default. My question is how to set a transparent background?
$input = imagecreatefrompng('image.png');
$output = imagecreatetruecolor(50, 50);
imagecopy($output, $input, 4,0, 8,8, 8,8);
imagecopy... etc.
header('Content-Type: image/png');
imagepng($output);
Is there a easy way of doing this? Thanks
Sets the transparent color in the given image.
int imagecolortransparent ( resource $image [, int $color ] )
Here's the link
Since the PHP function imagecopymerge doesn't work with the Alpha channel, you'll want to use the function from the first comment on this page imagecopymerge_alpha:
http://php.net/manual/en/function.imagecopymerge.php
Just have the transparent image as the base and merge it together with the image you need.
I've tried it out and it works fine for a project of mine.
None of the solutions worked for me, it would always convert transparent pixels on the source image to black on the destination image. What worked was changing imagecopy/imagecopymerge/imagecopymerge_alpha to imagecopyresampled and just passing the same width and height twice.
//Create destination image.
$png = imagecreatetruecolor(1024, 1024);
imagealphablending($png, false);
imagesavealpha($png, true);
//Make destination image be all transparent.
$color = imagecolorallocatealpha($png, 0, 0, 0, 127); //127 means completely transparent.
imagefill($png, 0, 0, $color);
//Load source image.
$png2 = imagecreatefrompng($sourceurl);
imagealphablending($png2, false);
imagesavealpha($png2, true);
$sizex = imagesx($png2);
$sizey = imagesy($png2);
//Copy to destination and save to file.
imagecopyresampled( $png, $png2,
0, 0,
0, 0,
$sizex, $sizey,
$sizex, $sizey);
imagepng($png, "result.png");
imagealphablending($input, true);
imagesavealpha($input, true);
imagealphablending($output, true);
imagesavealpha($output, true);
Or propably
int imagesavealpha($img,true);
http://www.php.net/manual/en/function.imagesavealpha.php
Full credit goes to:
http://consistentcoder.com/combine-a-transparent-png-image-on-top-of-another-image-with-php
The following code will overlay the foreground image onto the background image while preserving the transparency of the overlay:
//set the source image (foreground)
$sourceImage = 'table.png';
//set the destination image (background)
$destImage = 'create-a-surreal-head-of-tree-photo-manipulation.jpg';
//get the size of the source image, needed for imagecopy()
list($srcWidth, $srcHeight) = getimagesize($sourceImage);
//create a new image from the source image
$src = imagecreatefrompng($sourceImage);
//create a new image from the destination image
$dest = imagecreatefromjpeg($destImage);
//set the x and y positions of the source image on top of the destination image
$src_xPosition = 75; //75 pixels from the left
$src_yPosition = 50; //50 pixels from the top
//set the x and y positions of the source image to be copied to the destination image
$src_cropXposition = 0; //do not crop at the side
$src_cropYposition = 0; //do not crop on the top
//merge the source and destination images
imagecopy($dest,$src,$src_xPosition,$src_yPosition,
$src_cropXposition,$src_cropYposition,
$srcWidth,$srcHeight);
//output the merged images to a file
/*
* '100' is an optional parameter,
* it represents the quality of the image to be created,
* if not set, the default is about '75'
*/
imagejpeg($dest,
'combine-a-transparent-png-image-on-top-of-another-image-with-php-01.jpg',
100);
//destroy the source image
imagedestroy($src);
//destroy the destination image
imagedestroy($dest);
I am using PHP to rotate an PNG image, with a transparant background. But whatever I try, there are still some black lines around the original image.
How do I remove the black lines. Everything else works fine. The image is transparant, the image is rotated, the new corners are also transparant. Just the black lines around the original square (which is rotated) are annoying me.
I use this code:
$angle = -100;
header('Content-type: image/png');
$image = 'http://mapning.com/img/plane.png';
$file = imagecreatefrompng($image);
$rotate = imagerotate($file, $angle, 0);
imageSaveAlpha($rotate, true);
ImageAlphaBlending($rotate, false);
$transparentColor = imagecolorallocatealpha($rotate, 200, 200, 200, 127);
imagefill($rotate, 0, 0, $transparentColor);
imagepng($rotate);
I found my answer here:
http://ru2.php.net/manual/en/function.imagerotate.php#47985
I think better use imagick
Here is an extension for PHP
Or if you want with GD see here
http://ru2.php.net/manual/en/function.imagerotate.php#46338
I have a code that resize and colorize the image accordingly input values... the problem is I can able to colorize only one time with fresh image saved by other application..Please help me.. I hope there are many PHP expers are here.....
<?php
createImage(50,50, 0,0, 255);
function createImage($width, $height, $nR, $nG, $nB)
{
$image = imagecreatefrompng("source.png");
imagealphablending($image, false);
imagesavealpha($image, true);
//resize the image
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesx($image));
//colorize the image
$nrgb = str_pad(dechex($nR), 2, '0', STR_PAD_LEFT). str_pad(dechex($nG), 2, '0', STR_PAD_LEFT). str_pad(dechex($nB), 2, '0', STR_PAD_LEFT);
$newColor = $nrgb;
$c2 = sscanf($newColor ,"%2x%2x%2x");
for($i=0;$i<$width;$i++)
{
for($j=0;$j<$height;$j++)
{
$cIndex = imagecolorat($new_image,$i,$j);
imagecolorset($new_image,$cIndex,$c2[0],$c2[1],$c2[2]);
}
}
header("Content-Type: image/png");
imagepng($new_image,"test.png");
}
?>
Sounds to me like you are manipulating an image resource and outputting it and then wanting to go back and further manipulate it without starting over. You can do this by
a) save the image resource as a session variable, and then use the session variable in subsequent alterations.
b) save the altered image before outputting it, and then open the saved altered image and go from there. I don't know what file type you are using but for instance with gif images your code should be using imagegif() to output the image. You would utilize this same function (or other image type equivalent function) to also save the image.
I suggest looking at the imagefilter function found here: http://php.net/manual/en/function.imagefilter.php
Look at IMG_FILTER_COLORIZE on that page.