Transparent PNG over JPG in PHP - php

What seems to be simple, isn't :(
I'm trying to add something like a watermark (transparent png) on an image (jpg).
This is the code I'm using:
$width = 800;
$height = 600;
$bottom_image = imagecreatefromjpeg("portrait1.jpg");
$top_image = imagecreatefrompng("man2.png");
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagecopy($bottom_image, $top_image, 200, 200, 0, 0, $width, $height);
header('Content-type: image/png');
imagepng($bottom_image);
When I merge the images, the png is positioned at the right place, everythig above and left of it is good (jpg is copied), but everything else is black.
I've tried setting imagesavealpha and imagealphablending to false, there wasn't any difference.
You can see the resulting image at http://ekstrakt.selfip.com/photobomb/image.php
I've searched around the net, I can't find a solution.
Any help is appreciated.

Your $width and $height should be the dimensions of the watermark, not of the photo. What you're telling it to do is copy the watermark with a much bigger size than it is. When it reads part of an image that doesn't exist (coordinates out of bounds) the result is opaque black, giving the result you see.

Use imagecopymerge() instead of imagecopy()
U may also like imagesavealpha()

Related

WideImage - Canvas with transparent background

We're using a CMS with WideImage built in and no scope to change this, however we've come up against a situation where we need to produce 300x300 images.
However the images that are uploaded at approx 100x100 in jpg and png format with various levels of transparency.
I'm trying to create a 300x300 canvas, and place the 100x100 image inside of it - however I want both the canvas to remain transparent, and the image placed on top to keep its transparency.
I've got
$image = WideImage::loadFromFile( $this->local_path );
$canvas = WideImage::createTrueColorImage(300, 300);
$canvas_bg = $canvas->allocateColor(255, 255, 255);
$canvas->fill(0, 0, $canvas_bg);
$resized_image = $canvas->merge($image);
However this obviously just adds a white background to the canvas, I cant figure out from their documentation how to make it transparent.
Thanks
How about:
$image = WideImage::loadFromFile($this->local_path);
$resized_image = $image->resizeCanvas(300, 300, 0, 0);
You can specify the X and Y location of the image within the new canvas as well as its width and height. $image->resizeCanvas(300, 300, "center", "center") works quite well.

Convert any type of an image to PNG and add to PNG

I am trying to create an employee poster for a website that I develop. My goal is to take an image from a directory on the server of any file type (.png, .gif, .jpeg, etc.) and copy it onto another generate imaged that will then be outputted to the browser.
The problem is that I use:
$final_image = imagecreatefrompng("large_background.png");
for making the final image and for some reason if I add profile images with the type jpeg's, gif's, etc, (any type that isn't a jpeg) it doesn't work. The images never show up in the output. However, if I use png's it does work.
To solve this problem I tried converting the image to a png and then creating a png from it as shown in the code below. Unfortunately it doesn't work. Profile images still do not show up on the background.
// get image from database
$image_from_database = could be a .png, .jpeg, .gif, etc.
// get the image from the profile images directory
$path = "profile_images/".$image_from_database;
// create a png out of the image
$image = imagecreatefrompng(imagepng($path));
// add the $image to my larger $final_image (which is a png)
imagecopy($final_image, $image, $x, $y, 0,0, $height, $width);
imagepng($final_image, $ouput_url);
...
Can anybody tell me why this won't work? My profile images do not show up in the output of the final image.
My questions,
Is this line imagecreatefrompng(imagepng(...)); even possible? Essentially I want to convert an image of any type into a png and then create a png from it.
I'm just running a few local tests... The following works:
$src = imagecreatefromgif('test.gif');
$dest = imagecreatefrompng('test.png');
imagecopy($dest, $src, 0, 0, 0, 0, 100, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($src);
imagedestroy($dest);
So does this:
$src = imagecreatefromstring(file_get_contents('test.gif'));
If you're still having trouble after trying the latter example, please update your question. The actual images you're using would be helpful, in addition to a functional code example.
After you read an image with a imagecreatefrom* function, it doesn't matter what format the original was.
imagecreatefrom* functions return an image resource. When the image is loaded you are using internal representation of the images and not PNG, JPEG or GIF images.
If the images are successfully loaded imagecopy should have no problems with them.
This code uses images in different formats and works without a problem:
$img = imagecreatefrompng('bg.png');
$png_img = imagecreatefrompng('img.png');
$jpeg_img = imagecreatefromjpeg('img.jpeg');
$gif_img = imagecreatefromgif('img.gif');
/* or use this, so you don't need to figure out which imagecreatefrom* function to use
$img = imagecreatefromstring(file_get_contents('bg.png'));
$png_img = imagecreatefromstring(file_get_contents('img.png'));
$jpeg_img = imagecreatefromstring(file_get_contents('img.jpeg'));
$gif_img = imagecreatefromstring(file_get_contents('img.gif'));
*/
imagecopyresampled($img, $png_img, 10, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $jpeg_img, 120, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $gif_img, 230, 10, 0,0, 100, 100, 200, 200);
header('Content-Type: image/png');
imagepng($img);
Your example
$image = imagecreatefrompng(imagepng($path));
is wrong.
imagepng is used to output an image resource as a PNG image. If you provide a path as a second argument a PNG image file is created, otherwise it's printed to the output like echo does.
What imagepng actually returns is a boolean, indicating, if the output was successful.
You are then passing that boolean to imagecreatefrompng which expects a filepath. This is obviously wrong.
I suspect you have a problem with loading images.
imagecreatefrom* functions return FALSE on failure and you should check, if you have any problems with that.
Maybe your image paths are relative to doc root and your working directory is different.
Or you have permission problem.
Or your images are just missing.
It's impossible to tell from your question.

PHP GD imagecopymerge php with transparency

I'm trying to draw a partially transparent PNG image on another image I created in my script, but it behaves really strange. I'm using imagecopymerge because I want to use different opacity values, but when I do this, the output looks like this:
There must be some problem when processing the image. the yellow parts aren't even visible in the png file. Everything but the black parts are transparent.
I saved the image in photoshop and it looks ok when I just use imagecopy or something.
here are the relevant parts of the script:
$imgLogoBg = file_exists($logoBgImgFile)?imagecreatefrompng($logoBgImgFile):null;
$image = imagecreatetruecolor(imagesx($imgBase), imagesy($imgBase));
imagefill($image, 0,0, imagecolorat($imgBase,0,0));
imagecopymerge( $image, $imgLogoBg,
0,0,
0,0, imagesx($imgLogoBg), imagesy($imgLogoBg),50);
imagepng($image);
I can't figure out what the problem is. when I use another image the result is similar.

Merge images in PHP - GIF and JPG

I am trying to merge two images - a GIF image with a smaller JPG image. The output should be GIF.
The issue is that GIF image colors remain correct, but the colors of the JPG image are altered.
The GIF image has only 256 colors (8-bit), but is there a way to make the merged image to be a true-color resource which later can be converted to a 8-bit GIF for output?
Issue solved.
I updated the code. Here is the solution which works fine:
<?php
header('Content-Type: image/gif');
$gif_address = 'file.gif';
$jpg_address = 'file.jpg';
$image1 = imagecreatefromgif($gif_address);
$image2 = imagecreatefromjpeg($jpg_address);
$merged_image = imagecreatetruecolor(800, 800);
imagecopymerge($merged_image, $image1, 0, 0, 0, 0, 800, 800, 100);
imagecopymerge($merged_image, $image2, 0, 0, 0, 0, 500, 500, 100);
imagegif($merged_image);
imagedestroy($image1);
imagedestroy($image2);
imagedestroy($merged_image);
?>
From your explanation (some code would help), i would hazard a guess you are merging the jpeg onto the gif. Id say the easiest way is to use imageCreateTrueColor to create a new image the size you need then use imagecopy to copy the GIF into this new image. Merge the jpg onto this and then at a later date you can covert the true colour image to a gif.
If im missing something a bit of example code of what you are currently doing might help.

PHP/GD ImageSaveAlpha and ImageAlphaBlending

I'm using GD to resize and convert images, however during my tests I found a weird behavior when converting transparent PNG's to JPEG's. According to the manual ImageAlphaBlending() is on by default but in order to preserve the transparency I must set ImageSaveAlpha() to true (which in turn requires that I set ImageAlphaBlending() to false). So the correct way should be:
$result = ImageCreateFromPNG(...);
ImageAlphaBlending($result, false);
ImageSaveAlpha($result, true);
ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT);
ImageJPEG($result);
ImageDestroy($result);
However if I do it the "correct" way all the transparency area comes up black in the JPEG. This seems to work (JPEG with white background on transparent areas) on my tests:
$result = ImageCreateFromPNG(...);
ImageAlphaBlending($result, true); // true by default, but still...
ImageSaveAlpha($result, true);
ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT);
ImageJPEG($result);
ImageDestroy($result);
Can someone please enlighten me on this subject?
It probably depends on your PNG. A PNG file can contain a background color, which can be used when transparency doesn't work. Your PNG probably has a white background. When you set imageaplhablending to true it picks up the background color from your PNG and uses that when writing the JPEG. When you set it to false it picks the default for GD which is black.
You can try it for yourself. Create a transparent PNG and save it with an orange or pink background color. Your second example should show that color.
By the way, the PNG background color trick is a nice one for IE6 images. IE6 does not support transparent PNGs so it will display them with whatever background color you saved them with. When saving transparent PNGs, save them with the same background color as your website. It will look better than white or black boxes around your PNG images in IE6.
If you are converting from PNG (or GIF) to JPG, you should probably copy the final image to another image that is filled with white, using imagecopy ($image is any image already created with GD):
// Create a new background
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
// Allocate the color
$color = imagecolorallocate($bg, 255, 255, 255);
// Fill the background with white
imagefill($bg, 0, 0, $color);
// Alpha blending must be enabled on the background!
imagealphablending($bg, TRUE);
// Copy the current image onto the opaque background
if (imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)))
{
// Replace the image with the background copy
imagedestroy($image);
$image = $bg;
}
Hope that helps.

Categories