Transparency of PHP merged image not working - php

I'm creating an image with imagecopymerge, but the image being overlaid on top, is a PNG but the transparent part is pure white. How do I enable transparency?
$image = imagecreatefromjpeg($this->getFile());
$size = getimagesize($this->getFile());
$watermark = imagecreatefrompng('../watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = $size[0] - $watermark_width - 10;
$dest_y = $size[1] - $watermark_height - 5;
//die($watermark_width);
$thumb_image = imagecreatetruecolor($this->getThumbWidth(), $this->getThumbHeight());
imagealphablending($thumb_image,true);
imagealphablending($image,true);
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagecopyresampled( $thumb_image, $image, 0, 0, 0, 0, $this->getThumbResizeWidth(), $this->getThumbResizeHeight(), $this->getWidth(), $this->getHeight() );
imagejpeg( $thumb_image, $this->getThumbDestination(), $this->getThumbQuality() );
imagedestroy($thumb_image);
imagedestroy($image);

So I FINALLY found a solution. It's using imagecopy().
Here is the article that nudged me in the right direction.
That took quite a few hours of research!

Use imagecolortransparent()
Read more here
EDIT:
A better solution is here:
https://stackoverflow.com/a/313103/1533203

Related

PHP-GD Transparency of watermark PNG not correctly merged with JPEG

I am trying to install a watermark in the middle of my image, but every time it shows a weird square which is not fully transparent. This is the result of my code:
This is my code:
<?php
header("Content-type: image/png");
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
imagesavealpha($watermark,true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = (imagesx($image) - $watermark_width)/2;
$dest_y = (imagesy($image) - $watermark_height)/2;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
I usually create a new true colour resource and copy everything into it. This ensures GD doesn't get too quirky. It's a little bit more resource intensive, but should be negligible for most cases.
Below is your code modified to create a new image, copy in the jpeg, and then overlay the partially transparent watermark:
<?php
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$img_w = imagesx($image);
$img_h = imagesy($image);
$new = imagecreatetruecolor($img_w, $img_h);
imagecopy($new, $image, 0, 0, 0, 0, $img_w, $img_h);
imagedestroy($image);
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = ($img_w - $watermark_width) / 2;
$dest_y = ($img_h - $watermark_height) / 2;
imagecopy($new, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
header('Content-type: image/png');
imagejpeg($new);
imagedestroy($new);
imagedestroy($watermark);
Result:

PHP imagecopy & imagecopymerge changing background from transparent to black

I am copying one image and placing over second image, second image is transparent in background. While copying background of second image is converted to black. Even if i display image just after initializing image from file, it gives me black background. Please help..
<?php
header('Content-type:image/png');
$watermark = imagecreatefrompng('eye.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefrompng('img.png');
$size = getimagesize('img.png');
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopyresampled($image, $watermark, 5, 5, 0,0,55 ,55, $watermark_width, $watermark_height);
//imagecopymerge($image, $watermark, 5, 5, 0, 0, $watermark_width, $watermark_height, 50);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
remove the function
imagejpeg($image);
and add
imagealphablending($image, false);
imagesavealpha($image,true);
imagepng($image);

Merging and resizing an image

I'm attempting to expand my function from simply resizing an image, to also adding a watermark. The problem is the watermark is not being added. I've confirmed the path is correct. Why is it not working?
$image = imagecreatefromjpeg($this->getFile());
$size = getimagesize($this->getFile());
$watermark = imagecreatefrompng('../watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = $size[0] - $watermark_width - 10;
$dest_y = $size[1] - $watermark_height - 5;
//die($watermark_width);
$thumb_image = imagecreatetruecolor($this->getThumbWidth(), $this->getThumbHeight());
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagecopyresampled( $thumb_image, $image, 0, 0, 0, 0, $this->getThumbResizeWidth(), $this->getThumbResizeHeight(), $this->getWidth(), $this->getHeight() );
imagejpeg( $thumb_image, $this->getThumbDestination(), $this->getThumbQuality() );
imagedestroy($thumb_image);
imagedestroy($image);
After posting the code I found a couple of errors, the major one being, contrary to my belief, an incorrect file path for the watermark.png. The is updated and does work for me. There is still an issue with alpha transparency, however.

php gd script not saving image properly

I've got this script, which only saves the image at $image, and not the image at $newimage_2. Help?
<?php
$newimage_1 = imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
$newimage_2 = imagecreatefromjpeg($newimage_1);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagejpeg($newimage_2);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($newimage_2);
?>
$source_file_path=$_FILES["image"]["tmp_name"];
$src = imagecreatefromjpeg($source_file_path);
list($width,$height)=getimagesize($source_file_path);
$newwidth=540;
$newheight=round(($height/$width)*$newwidth);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$target_file_path = "images/".$filewhereyouwanttosaveit;
$watermark = imagecreatefrompng('imgs/copyright.png');
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = ($newwidth - $watermarkwidth);
$startheight = ($newheight - $watermarkheight);
imagecopy($tmp,$watermark,$startwidth,$startheight,0,0,$watermarkwidth,$watermarkheight);
imagegif($tmp,$target_file_path);
you probably dont need the resizing but code may help you...
imagegif or jpg or png or some else

image ontop of an image in php

Here is a link to the page http://www.true-gamerz.net/test2.php
if you look at the image, The rank image is transpent but it dose not show it on the screen
dose anyone know why this is happen?
Here is the code
<?
header('content-type: image/png');
$watermark = imagecreatefrompng('images/ranks/ranks_18.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefrompng("images/card/test.png");
$size = getimagesize("images/card/test.png");
$dest_x = $watermark_width;
$dest_y = $watermark_height;
imagecopymerge($image, $watermark, 289, 4, 0, 0, $watermark_width, $watermark_height, 100);
imagepng($image);
imagedestroy($image);
imagedestroy($watermark);
?>
imagecopymerge does not support alpha channel
Read this for workaround:
http://www.php.net/manual/en/function.imagecopymerge.php#92787

Categories