lovephp Posted May 26, 2015 Share Posted May 26, 2015 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 https://forums.phpfreaks.com/topic/296498-need-help-adding-watermarkpng-during-upload/ Share on other sites More sharing options...
jcbones Posted May 26, 2015 Share Posted May 26, 2015 Start with this Tutorial http://www.phpfreaks.com/tutorial/php-add-text-to-image. It's a little dated, but I don't think much has changed. There are also some pretty good script samples in the manual on imagettftext. Link to comment https://forums.phpfreaks.com/topic/296498-need-help-adding-watermarkpng-during-upload/#findComment-1512668 Share on other sites More sharing options...
lovephp Posted May 26, 2015 Author Share Posted May 26, 2015 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 https://forums.phpfreaks.com/topic/296498-need-help-adding-watermarkpng-during-upload/#findComment-1512669 Share on other sites More sharing options...
QuickOldCar Posted May 26, 2015 Share Posted May 26, 2015 imagecopymerge() Forgot the link to watermark example. http://php.net/manual/en/image.examples.merged-watermark.php Link to comment https://forums.phpfreaks.com/topic/296498-need-help-adding-watermarkpng-during-upload/#findComment-1512670 Share on other sites More sharing options...
jcbones Posted May 26, 2015 Share Posted May 26, 2015 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); Link to comment https://forums.phpfreaks.com/topic/296498-need-help-adding-watermarkpng-during-upload/#findComment-1512674 Share on other sites More sharing options...
fastsol Posted May 26, 2015 Share Posted May 26, 2015 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); } Link to comment https://forums.phpfreaks.com/topic/296498-need-help-adding-watermarkpng-during-upload/#findComment-1512675 Share on other sites More sharing options...
lovephp Posted May 27, 2015 Author Share Posted May 27, 2015 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); Link to comment https://forums.phpfreaks.com/topic/296498-need-help-adding-watermarkpng-during-upload/#findComment-1512681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.