Adding water mark to jpg/jpeg and png works perfectly file but strange color appear when the same watermark is added to the gif image
the gif image with watermark is here
the strange color here is my png watermark image which is shown like this
my code
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);
Solved this after converting the gif image to a true color image
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
$tmp = imagecreatetruecolor(imagesx($image), imagesy($image));
$bg = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $bg);
imagecopy($tmp, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
$image = $tmp;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);
Related
so my following code is working like if i upload image it will resize image to 720x450 and then watermark it. but i wish not to modify the width and height and put the watermark at the bottom right of an image of any size
if someone could help me out here?
$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = 720; $height = 450;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 90);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
appreciate your help and time.
You are providing manual height and width, Just assign the original height ans width of image
$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $owidth; $height = $oheight;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 90);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
Try this will work as you expected.
for more info check here http://php.net/manual/en/image.examples-watermark.php
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:
Im Trying to watermark and resize and imagen but im doing something wrong on the resize part any help please
$image = #imagecreatefromjpeg('15_pic1.jpg');
$watermark = #imagecreatefrompng('watermark.png');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth));
$startheight = (($imageheight - $watermarkheight));
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
//resize
list($width, $height) = getimagesize($image);
imagecopyresized($thumb, $image, 0, 0, 0, 0, 300, 300, $width, $height);
header("Content-type: image/jpeg"); imagejpeg($thum);
imagedestroy($image);
imagedestroy($watermark);
Got it working here is the code
$img = "15_pic1.jpg";
$wat = "watermark.png" ;
$wit = "300";
$hei = "300";
$imagestart = #imagecreatefromjpeg($img);
$watermark = #imagecreatefrompng($wat);
list($width, $height) = getimagesize($img);
$imagewidth = imagesx($imagestart);
$imageheight = imagesy($imagestart);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth));
$startheight = (($imageheight - $watermarkheight));
imagecopy($imagestart, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
$thumb = imagecreatetruecolor($wit, $hei);
imagecopyresized($thumb, $imagestart, 0, 0, 0, 0, $wit, $hei, $width, $height);
header("Content-type: image/jpeg"); imagejpeg($thumb);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($thumb);
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
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