PHP merge images saving transparency - php

I am trying to merge 2 images, both png with the transparent alpha layer. But I got this .
Image with 'A' letter just erase other image and makes it transparent. Here's my code
$letterimg = imagecreatefrompng('assets/img/qr/a2.png');
$codeimg = imagecreatefrompng('assets/img/qr/'.$code.'.png');
imagesavealpha($letterimg,true);
imagecopy($codeimg,$letterimg,50,50,0,0,imagesx($codeimg),imagesy($codeimg));
header('Content-Type: image/png');
imagepng($codeimg);

Problem is resolved by changing image color mode from RGB to indexed color

Related

converting rgb image to cmyk using Imagick

I have image that i want to convert from rgb to cmyk using color profile.
Here is the image that i am using
And the peace of code that i am using to convert into cmyk.
$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage($image);
$IMagick->transformImageColorspace(12);
$icc_cmyk = file_get_contents("USWebUncoated.icc");
$IMagick->profileImage('icc', $icc_cmyk);
$IMagick->writeImage ($image2);
And here is the output image that i got.
I then checked color space and yes it is cmyk, but you can mark that green color is change though. Am i doing anything wrong here?
It is known that converting RGB to CMYK can change colors. Also browsers hae challenges rendering CMYK images properly. There is nothing you are doing wrong.

Partial black background when watermarking PNG image with GD PHP

I have pieced together a PHP class to perform various image related functions using GD functions of PHP.
It works great for all image types. Rotate, flip, resize, crop and to a lesser extent, watermark.
All but the latter work perfectly. For example after a few changes, rotated PNG images retained their transparency whereas before they were losing that and the background turning black. Common problem, it appears. But all working now.
Where I'm still getting stuck is watermarking a PNG image with another PNG image. It appears to work fine with JPG and other images. This is the code (simplified):
public function writeWatermarkSimple()
{
$watermarkFile = 'watermark.png';
$watermarkImage = imagecreatefrompng($watermarkFile);
imagealphablending($watermarkImage, false);
imagesavealpha($watermarkImage, true);
$imageFile = 'image.png';
$baseImage = imagecreatefrompng($imageFile);
imagealphablending($baseImage, false);
imagesavealpha($baseImage, true);
$marginH = imagesx($baseImage) - imagesx($watermarkImage);
$marginV = imagesy($baseImage) - imagesy($watermarkImage);
$cut = imagecreatetruecolor(imagesx($watermarkImage), imagesy($watermarkImage));
imagecopy($cut, $baseImage, 0, 0, $marginH, $marginV, imagesx($watermarkImage), imagesy($watermarkImage));
imagecopy($cut, $watermarkImage, 0, 0, 0, 0, imagesx($watermarkImage), imagesy($watermarkImage));
imagecopymerge($baseImage, $cut, $marginH, $marginV, 0, 0, imagesx($watermarkImage), imagesy($watermarkImage), 80);
if (!imagepng($baseImage, 'watermarked_image.png'))
{
return false;
}
return true;
}
This has been pieced together with various guides and advice people have given based on a similar issue. Again, working perfectly with JPG images and PNG watermarks, but not PNG & PNG.
Some example images:
http://i.imgur.com/hHRWinj.png - This is the watermark I'm using.
http://i.imgur.com/6sy8Ncs.png - This is the image I'm applying the watermark to.
http://i.imgur.com/ghovYLm.png - This is the end result.
The bit I find interesting is that any part of the watermark that is overlaid on a non-transparent portion of the image is working fine. Just the rest of it has the black background.
This leads me to believe I'm close, and I hope that the expertise of you fine people may lead me to the solution.
Thanks ever so for reading.
So, I'm not giving up on finding the correct answer to do this using GD. However, I was overjoyed to find that what needed up to 30 lines of code with GD can be achieved using much less with ImageMagick:
$image = new Imagick();
$image->readimage($this->_image);
$watermark = new Imagick();
$watermark->readimage($this->_watermark->_getImage());
$watermark->evaluateImage(Imagick::EVALUATE_DIVIDE, 2, Imagick::CHANNEL_ALPHA);
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, $marginH, $marginV);
So this is before (with GD):
http://i.imgur.com/AlS0TcO.png
And after (with ImageMagick and the code above):
http://i.imgur.com/zBxlC3R.png
If anyone has an answer that is purely GD then I'd be immensely grateful.
Ran into some similar issues recently and while this may not exactly solve your problem, these were some useful discoveries that I made.
In my case, I have an original .jpg image and a watermark .png image. The watermark image has a fully transparent background. I wanted to specify the opacity in my script and have it change the watermark opacity before placing it on top of the origina image. Most posts out there regarding PHP watermarking assume that the original watermark .png file already has the solid watermark portion set to the correct opacity rather than changing it via the script.
gd didn't like a 24 bit .png and caused some goofy issues. Switching to 8 bit resolved that with gd. On the other hand, imagick works very well with a 24 bit .png and the final result seems to be better.
For me, using gd worked just fine if I was opening the original watermark .png and using imagecopymerge() to set the watermark transparency. If however I tried to scale the original watermark .png (which has transparent background) first, then I would get similar results as you with black or white background portion of where watermark image is. See How do I resize pngs with transparency in PHP? for a partial solution by filling the new wm image with transparent rectangle first. For me this still produced an opaque white background on the final result no matter what I tried.
I switched to imagick and was using setImageOpacity() to change the transparency of my watermark .png before applying it on top of my original image and I was still getting the same effect with a black background. Finally read in the PHP doc for setImageOpacity() that if the original .png has any transparent pixels and you try to lower the opacity, those pixels become opaque (black) with the new transparency applied. Instead, need to use the evaluateImage() function. This will instead evaluate each pixel's alpha channel only and divide by the specifid number.
I assume the black / white background issue with gd is likely due to similar ways that it treats alpha channels when scaling / combining as compared to imagick and if you want to do it all in gd you just need to find some similar way to evaluate and manipulate the alpha channel per-pixel because the "easy" ways seem to take an already transparent background and make it opaque.
So, the solution:
Assuming you want to apply your watermark at an opacity of 45% and you're using imagick, then instead of this:
$watermark->setImageOpacity(.45);
do this
$watermark->evaluateImage(Imagick::EVALUATE_DIVIDE, (1/.45), Imagick::CHANNEL_ALPHA);
You need to divide 1 by your opacity to get the demoninator by which the function will divide the alpha channel value for each pixel. In this case, 1/.45 = 2.2222, so then the function will divide the alpha channel of each pixel by 2.2222. This means a solid pixel (alpha of 1) would result in 1/2.2222 or .45 alpha or transparency when finished. Any pixels that were already transparent (alpha 0) would stay transparent because 0 divided by anything is always what? Zero!
After you change the watermark transparency then you can use compositeImage() to merge the watermark onto the original image.

PHP GD :image re size with some black rectangles?

I am trying to accomplish this task for 2 days, read various stuffs online but still can't find out what is happen, also read all here at SO about similar problems but nothing.
I have image 400x400 and want to generate 120x120 using php gd. using this code:
$image_p = imagecreatetruecolor(120,120);
$image = imagecreatefromstring($X_IMAGE);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 120, 120, 400, 400);
// RETURN
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 70);
//destroy...
$X_IMAGE is 400x400 JPG that is stored as string.
All images are generated in 120x120 but most of them have some BLACK rectangle at bottom at some pictures it is larger on some it is smaller but 50% of images are with that square. So all are VISIBLE, just some part of image is covered with that black color. What would be solution for my problem? All source images are JPG and also those 120x120 that I need are JPG as you can see...
The problem is that your original image is not a square! You pass 400x400 to imagecopyresampled, but the height of the original image maybe is not 400px!
In the image you posted, for example, you have a not-squared original image. When you tell PHP to resample a square on anther image resource, you will resample the image plus a non-existent rectangle at the bottom.
The solution depends on what you want to output.
Do you want a scaled image that keeps ratio? For example, from 400x300 to 120x90.
Or a scaled image that not keeps ratio? For example, from 400x300 to a distorted 120x120?
Or a cropped thumbnail? 400x300 to a 120x120 with left and right parts trimmed out a little?
Do you want to replace the black rectangle with a white one, so fill the resampled image in that way?

PHP. Imagecolorallocate returns false

I use some code:
$colors = array ();
$colors['test'] = imagecolorallocate($img, 0 ,1 ,2);
....
It works until I change picture to other
After that imagecolorallocate and imagecolorallocatealpha return false
$img is correct image, i.e imagepng print it correctly
How to make imagecolorallocate work?
Comment on the documentation of this function:
If you even in a situation where it's
not allocating the color you want it
could be because of your images color
allocation table. GIF and 8-bit PNG
images are very susceptible to this.
If your using an GIF and PNG try
dropping a color from the table,
should let you allocate another.
If the source image in an indexed image (8-bit or less) then make sure there is room for additional colors. E.g. an 8-bit image might not be using all 256 colors from the palette in that case the function should succeed and it will not otherwise.
Edit
I looked at your image and it is infact an 8-bit PNG image that uses all 256 colors. Try converting it to a true color image (either via photoshop or by copying the image into a GD image created via imagecreatetruecolor()); do all the editing, then save as GIF.

PHP: Merging images into an alpha translucent image with GD

I'm trying to create an image with varying level of transparency, using GD. But I can get only fully transparent or fully opaque pixels in the image.
What I want to get: What I actually get:
(source: bx.at.ua)
(source: bx.at.ua)
(source: bx.at.ua)
(source: bx.at.ua)
Here's a piece of the code I used to create images like this one ▲ fixed code:
$im=imagecreatetruecolor($sx,$sy);
imageantialias($im,true);
imagesavealpha($im,true);
$c00FF00=imagecolorallocate($im,0,255,0);
$cFFFFFF=imagecolorallocate($im,255,255,255);
$cFFFFFF_00=imagecolorallocatealpha($im,255,255,255,127);
imagecolortransparent($im,$cFFFFFF);
imagefilledrectangle($im,0,0,$sx,$sy,$cFFFFFF$cFFFFFF_00);
$sim=imagecreatefrompng('gradient.png');
imagecopy($im,$sim,$dest_x,$dest_y,0,0,imagesx($sim),imagesy($sim));
imagedestroy($sim);
imagettftext($im,$size,0,$text_x,$text_y,$c00FF00,$font,'Test');
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
What can I do to get the desired (translucent) result?
You need to call imagesavealpha() against your imported PNG ($sim) before merging, as well as against the final image.
What you are looking for is not transparency but alpha. Alpha is another color channel that goes along with the red, green and blue colors to determine the transparency level (just transparent is visible or not as opposed to being able to set the level of transparency). Check out imagecolorallocatealpha

Categories