I am trying to do a watermark mark with the php code, and everything seems to work fine, until I put a transparent PNG file to a GIF. This what happens:
So instead of transparent black watermark, I get this semi solid green thing on the top.
The watermark is
I use the following php code:
...
$image = imagecreatefromgif($filepath);;
$watermark_image = imagecreatefrompng($wm_filepath);
imagealphablending($watermark_image, false);
imagesavealpha($watermark_image, true);
imagegif($image, $filepath);
imagedestroy($image);
imagecopy($image, $watermark_image, $offset['x'], $offset['y'], 0, 0, imagesx($watermark_image), imagesy($watermark_image) );
p.s. I have to mention that I tried to combine different settings using
imagealphablending()
imagesavealpha()
and got no result
UPD:
Now I am saving image as a png file. I deleted these two rows imagealphablending($watermark_image, false); imagesavealpha($watermark_image, true); and it worked. However, the transparency of PNG is overlapping GIF. imagealphablending($image, true); didn't help. What shall I do?
GIF files are limited to 256 colours and have a single colour designated as transparent. So you can't have antialiased transparency, nor can you have much antialiasing of any kind.
I suggest saving as PNG instead.
Related
I have two graphics file.
The first image - a background image in JPG format
The second file - PNG file with the figure in the center filled with white, with a black border on a path. The main background of transparent PNG file.
Question:
How to merge two files with transparency (see image example)? Background of the first file should be placed inside the figure in the second file.
Scheme:
Images:
PNG file - profiles.in.ua/tmp/sample2.jpg
JPG file - profiles.in.ua/tmp/sample1.png
PHP code:
$mask = new Imagick(realpath('mask.png'));
$pattern = new Imagick(realpath('pattern.jpg'));
$pattern->resizeImage($mask->width, $mask->height, Imagick::FILTER_LANCZOS, 1);
$pattern->compositeImage($mask, Imagick::COMPOSITE_ATOP, 0, 0);
header("Content-Type: image/png");
echo $pattern->getImageBlob();
$mask->destroy();
$pattern->destroy();
Assuming the mask image is always made exclusively of white pixels (which should be overwritten with the pattern), black pixels (which should overwrite the pattern), and transparent pixels (which should remain transparent), you can get this effect by compositing the pattern into the non-transparent pixels in the mask, then darkening the result with the mask.
The PNG file you provided did not have a transparent background as specified; instead it was white and grey hatching. I had to edit it first to add a transparent background before this code worked.
$mask = new Imagick(realpath('sample1.png'));
$pattern = new Imagick(realpath('sample2.jpg'));
$pattern->resizeImage($mask->width, $mask->height, Imagick::FILTER_LANCZOS, 1);
$image = clone($mask);
$image->compositeImage($pattern, Imagick::COMPOSITE_IN, 0, 0);
$image->compositeImage($mask, Imagick::COMPOSITE_DARKEN, 0, 0);
header("Content-Type: image/png");
echo $image;
$image->destroy();
$mask->destroy();
$pattern->destroy();
You need to fix the end of your code. All good until the end.
$base->writeImage('output.png');
header("Content-Type: image/png");
echo $base;
Update me :)
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.
I created an image from other image with the following code,
imagepng(imagecreatefromstring(file_get_contents(destination path)),source_path);
The image was created,But when i try to create a png image from a transparent image,The background become black color.
Why this happens?How to solve this?
If you look at the GD Documentation you'll find the alpha based functions. Taken directly from imagesavealpha() is the following code that opens a png and outputs it with transparency maintained;
// Load a png image with alpha channels
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
// (So any resizing/rotating/cropping etc)
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
I have the last few hours tried to get a PNG logo with a transparent background on top of a JPG background. I have tried several ways and with several globals as well, but I do not seem to be able to get the result I want.
"First Attempt":
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
As you can see, the Jaguar logo is all black.
"Second Attempt"
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->setImageColorspace($overlay->getImageColorspace() );
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
This one the Jaguar logo looks like it should, but the background is all messed up now.
I have tried with Imagick::setImageMatte and tried to add the overlay to a white background (thought I does need to have a transparent background) and still it won't display the image properly. I have tried many other variations of the 2 above snippets but they all seem to make the PNG completely or partial black.
What am I missing or doing wrong? Can anyone give me a nudge in the right direction?
Please note this needs to be done in PHP.
Thank you very much!
I am a huge idiot! Turns out I forgot to convert the images from CMYK to RGB.
For anyone who might encounter this in the future, learn from my incompetence!
I was trying to overlay a png with transparency over the top of another png. I used this line from the PHP docs.
$src1->compositeImage($src2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
but I was getting the same problem. The overlay came through as black only. Changing it to this seemed to fix the colours.
$src1->compositeImage($src2, Imagick::COMPOSITE_DEFAULT, 0, 0);
I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.