Jump to content

Need help adding watermark.png during upload


lovephp
Go to solution Solved by lovephp,

Recommended Posts

mates the following code below i use to update uploaded images but i need help adding watermark to the images during upload

 

if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $name = $_POST["name"];
        IF($_FILES['file']['name']!='')
        {
                 $file='../uploads/profiles/'.$row['image'];
                 @unlink($file);
                 $tmp_name = $_FILES["file"]["tmp_name"];
                 $namefile = $_FILES["file"]["name"];
                 $ext = end(explode(".", $namefile));
                 $image_name=time().".".$ext;
                 $fileUpload = move_uploaded_file($tmp_name,"../uploads/profiles/".$image_name);
        }
        else
        {
                $image_name=$row['image'];
        }
         $sqlAdd ="update  tbl_emp set name='".$name."', image='".$image_name."'  where id=".$_GET['id'];
           mysql_query($sqlAdd);
         header("Location:add.php?id=".@$_GET['id']."&msg=success");
         exit;
    }      

 

your help is appreciated

Link to comment
Share on other sites

here is what i tried but no output

 

$image_path = "watermark.png";
function watermark_image($oldimage_name, $new_image_name){
    global $image_path;
    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width = $height = 500;    
    $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, 100);
    imagedestroy($im);
    unlink($oldimage_name);
    return true;
}

if(isset($_GET['id']))
{
    $qry = "SELECT * FROM tbl_emp where id=".$_GET['id'];
    $result = mysql_query($qry);
    $row = mysql_fetch_array($result);
    
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $name = $_POST["name"];
        IF($_FILES['file']['name']!='')
        {
                 $file='../uploads/profiles/'.$row['image'];
                 @unlink($file);
                    $tmp_name = $_FILES["file"]["tmp_name"];
                 $namefile = $_FILES["file"]["name"];
                 $ext = end(explode(".", $namefile));
                 $image_name=time().".".$ext;
                 $fileUpload = move_uploaded_file($tmp_name,"../uploads/profiles/".$image_name);
                 watermark_image($namefile, $image_name);
        }
        else
        {
                $image_name=$row['image'];
        }
         $sqlAdd ="update  tbl_emp set name='".$name."', image='".$image_name."'  where id=".$_GET['id'];
           mysql_query($sqlAdd);
         header("Location:add.php?id=".@$_GET['id']."&msg=success");
         exit;
    }      

Link to comment
Share on other sites

So you are looking to merge two images.

 
function watermark_image($oldimage_name, $new_image_name=null,$watermark){
 
    //global $image_path; //don't use global, as it leads to hard to maintain code.
 
    list($owidth,$oheight) = getimagesize($oldimage_name);
 
    $width = $height = 500;    //this should be set as an option for code reusability.
//create main image.
    $im = imagecreatetruecolor($width, $height);   
    $img = imagecreatefromjpeg($oldimage_name); 
    imagecopyresampled($im, $img, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    //create watermark image.
    $img_src = imagecreatetruecolor($width,$height);
    $wm = imagecreatefrompng($watermark);
    list($wmWidth,$wmHeight) = getimagesize($watermark);
    imageAlphaBlending($img_src, false);
imageSaveAlpha($img_src, true);
imagecopyresampled($img_src,$wm,0,0,0,0,$width,$height,$wmWidth,$wmHeight); 
//merge the images, we will use imagecopy() instead of imagecopymerge() because we don't need to set trans.
imagecopy($img, $img_src, 0, 0, 0, 0, $width,$height);
//if there is a new image name, save the image.
    if(!empty($new_image)) {
imagejpeg($img,$new_image_name,100);
imagedestroy($im); 
return true;
} else { //if there isn't a new image name, output the image.
imagejpeg($img,null,100);
imagedestroy($im); 
return true;
}
return false;
}
//set content header.
header('Content-Type: image/jpeg');
//call and output the function. (since we didn't send an image name.)
watermark_image($image_path.$image, null,$image_path.$watermark);
  • Like 1
Link to comment
Share on other sites

This is a watermarking function that I built and use.

// $wm is the watermark image you want to use
// $img is the image to watermark
// $xy should be an array of x and y coordinates - $xy['x'] = 150 $xy['y'] = 40
// placement defaults to center if $xy is not supplied
// $dest woudl be a place to save the file if you are not displaying the new image directly to the browser.
function watermarkImage($wm, $img, $xy = NULL, $dest = NULL)
{
	$watermark = imagecreatefrompng($wm);
	$wm_src = getimagesize($wm);
	$source = getimagesize($img);
	$mime = $source['mime'];
	
	if($mime === 'image/jpg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/jpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/pjpeg')
	{$src = imagecreatefromjpeg($img);}
	elseif($mime === 'image/png')
	{$src = imagecreatefrompng($img);}
	elseif($mime === 'image/gif')
	{$src = imagecreatefromgif($img);}

	$x = ($xy !== NULL) ? $xy['x'] : floor(($source[0] / 2) - ($wm_src[0] / 2));
	$y = ($xy !== NULL) ? $xy['y'] : floor(($source[1] / 2) - ($wm_src[1] / 2));
	
	imagecopy($src, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
	imagealphablending($src, false);
	imagesavealpha($src, true);

	if($mime === 'image/jpg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/jpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/pjpeg')
	{
		if($dest !== NULL)
		{ imagejpeg($src, $dest, 95); }
		else
		{
			header('Content-Type: image/jpeg');
			imagejpeg($src);
		}
	}
	elseif($mime === 'image/png')
	{
		if($dest !== NULL)
		{ imagepng($src, $dest); }
		else
		{
			header('Content-Type: image/png');
			imagepng($src);
		}
	}
	elseif($mime === 'image/gif')
	{
		if($dest !== NULL)
		{ imagegif($src, $dest); }
		else
		{
			header('Content-Type: image/gif');
			imagegif($src);
		}
	}

	imagedestroy($src);
}
  • Like 1
Link to comment
Share on other sites

  • Solution

issue solver :-D

 

all i had to do was first upload the image then watermark

 

                 $fileUpload = move_uploaded_file($tmp_name,"../uploads/profiles/".$image_name);
                  $image_name=time().".".$ext;
                 watermark_image($tmp_name,"../uploads/profiles/".$image_name);
 

Edited by lovephp
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.