lingo5 Posted July 23, 2014 Share Posted July 23, 2014 $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { foreach ($_FILES as $file) { $fileName = $file['name']; $tmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; if($fileName==""){ $filePath = '../img/none.jpg'; } else{ $filePath = $uploadDir . $fileName; } ////generate random name //// get the file extension first $ext = substr(strrchr($fileName, "."), 1); // //// make the random file name $randName = md5(rand() * time()); // //// and now we have the unique file name for the upload file $filePath = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath = str_replace(" ", "_", $filePath); $result = move_uploaded_file($tmpName, $filePath); //if (!$result) { //echo "Error uploading file"; //exit; //} if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileinsert[]=$filePath; } /* * PHP GD * adding watermark to an image with GD library */ // Load the watermark and the photo to apply the watermark to $stamp = imagecreatefrompng('../img/watermark_fotos.png'); $im = imagecreatefromjpeg('$filePath'); // Set the margins for the stamp and get the height/width of the stamp image $marge_right = 10; $marge_bottom = 10; $sx = imagesx($stamp); $sy = imagesy($stamp); // Copy the stamp image onto our photo using the margin offsets and the photo // width to calculate positioning of the stamp. imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp)); // Output and free memory header('Content-type: image/png'); imagepng($im); imagedestroy($im); Hi, I use this code to upload an image and save the path to my DB. I have added some code to watermark the image but it doesn't seem to be doing it... please help. Thanks :-) Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 23, 2014 Share Posted July 23, 2014 https://www.youtube.com/user/betterphp/search?query=watermark Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 23, 2014 Author Share Posted July 23, 2014 thanx fast sol.. I have already gone through all that, my problem is I can't get the watermark part of my code to work. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 24, 2014 Author Share Posted July 24, 2014 please I need someone to help me with my code?. What am I doing wrong?. Thanks Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 24, 2014 Share Posted July 24, 2014 you need to determine what your code is actually doing to find where the problem is at. to start with, do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects? the following line - $im = imagecreatefromjpeg('$filePath'); should be producing php error(s), because php variables are not replaced with their content when they are between single-quotes. you may also need to turn OFF any output_buffering setting that might be ON in your php.ini since that can hide php error messages. next, your upload code is looping over multiple uploaded files. do you want to water-mark all of them or just the last one (which is what your current code is trying to do) and what do you want to do with the final water-marked image(s)? the current code is outputting it (the last image) back to the browser immediately after processing the uploaded file. do you want to save the water-marked image as a file so that you don't need to run the gd functions (which take a lot of memory and processing time) every time you display the image? also, if your intent is to upload multiple images at one time (what the first part of your code supports), you won't be able to output the images back to the browser all at once as you can only output one image at a time. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 24, 2014 Author Share Posted July 24, 2014 Hi mac_gyver, thanks for your answer. In this case what I want to do is to upload one image to my "uploads" folder, watermark the image and save the path to my database. The uploading and path saving parts work fine, but I have trouble watermarking the image. I don't need the image to be outputed to the browser after being watermarked, so I have removed tha part of the code. ..... Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 24, 2014 Share Posted July 24, 2014 Try this function that I have in my code base. //Creates a water marked image // $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); } Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 24, 2014 Author Share Posted July 24, 2014 thanks but I wouldn't know how to do that... still trying to learn. I'm not a programmer you see I'm a psichology student learning some PHP jut because I have an interest in progamming. Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 24, 2014 Share Posted July 24, 2014 Seriously!, you don't know how to use a standard function? I reworked your code to handle my function. I can't say it's perfect cause I'm not going to make a html form to fully test it. <?php //Creates a water marked image // $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); } $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { foreach ($_FILES as $file) { $fileName = $file['name']; $tmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; if($fileName==""){ $filePath = '../img/none.jpg'; } else{ $filePath = $uploadDir . $fileName; } ////generate random name //// get the file extension first $ext = substr(strrchr($fileName, "."), 1); // //// make the random file name $randName = md5(rand() * time()); // //// and now we have the unique file name for the upload file $filePath = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath = str_replace(" ", "_", $filePath); $result = move_uploaded_file($tmpName, $filePath); //if (!$result) { //echo "Error uploading file"; //exit; //} if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileinsert[]=$filePath; } /* * PHP GD * adding watermark to an image with GD library */ // Load the watermark and the photo to apply the watermark to $stamp = '../img/watermark_fotos.png'; // Set the margins for the stamp and get the height/width of the stamp image $margin['x'] = 10; $margin['y'] = 10; $destination = 'path_where_you_want_the_image_stored'; watermarkImage($filePath, $stamp, $margin, $destination); } ?> You have some other badly written code in there but I didn't focus on any of that. This in particular is pretty bad. if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } Magic quotes should be disabled by default on any server at this point, it's a very bad function that php used in the past and has since been removed. Towards the end of the code you will need to edit the $destination var value to an actual path and file name you want the watermarked images stored to. Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 24, 2014 Share Posted July 24, 2014 Seriously!, you don't know how to use a standard function? Sorry that was rude, I remember when I struggled with functions too. 1 Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 24, 2014 Author Share Posted July 24, 2014 thanks!!... one question, what is $destination supposed to be? my script is uploading the image to te UPLOADS folder and saving the path to the database so I don't understand what this other path should be, What I want to do is to watermark the image ad then save the path to the database. Sorry I'm not sure I'm explaining this properly... Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 24, 2014 Share Posted July 24, 2014 The destination can be whatever you want it to be for the final image name and where you want it stored. If you simply want the uploaded file replaced with the new watermarked image then do $destination = $filePath; If it doesn't store it then you'll need to verify the path to the folder you're trying to store it in, but since it seems to work for the upload, in theory it should work for the watermark too. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 24, 2014 Author Share Posted July 24, 2014 ooops I'm a bit lost now... what is getting saved to my DB is just the watermark. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 25, 2014 Author Share Posted July 25, 2014 only saving the watermak Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 25, 2014 Share Posted July 25, 2014 Not sure where my last post went but, you don't have anything in your code to save to a db or even connect to one. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 25, 2014 Author Share Posted July 25, 2014 $query = "INSERT INTO t_images (imagen_path) ". "VALUES ('$filePath')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); //Creates a water marked image // $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); } $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { foreach ($_FILES as $file) { $fileName = $file['name']; $tmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; if($fileName==""){ $filePath = '../img/none.jpg'; } else{ $filePath = $uploadDir . $fileName; } ////generate random name //// get the file extension first $ext = substr(strrchr($fileName, "."), 1); // //// make the random file name $randName = md5(rand() * time()); // //// and now we have the unique file name for the upload file $filePath = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath = str_replace(" ", "_", $filePath); $result = move_uploaded_file($tmpName, $filePath); //if (!$result) { //echo "Error uploading file"; //exit; //} if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileinsert[]=$filePath; } /* * PHP GD * adding watermark to an image with GD library */ // Load the watermark and the photo to apply the watermark to $stamp = '../img/watermark_fotos.png'; // Set the margins for the stamp and get the height/width of the stamp image $margin['x'] = 10; $margin['y'] = 10; $destination = $filePath; watermarkImage($filePath, $stamp, $margin, $destination); Hi, this is the code you modified for me and this is my db update query Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 25, 2014 Author Share Posted July 25, 2014 I've posted your code... what happened to it?? Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 25, 2014 Share Posted July 25, 2014 Just put the query stuff right before these lines /* * PHP GD * adding watermark to an image with GD library */ Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 26, 2014 Author Share Posted July 26, 2014 //Creates a water marked image // $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); } $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { foreach ($_FILES as $file) { $fileName = $file['name']; $tmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; if($fileName==""){ $filePath = '../img/none.jpg'; } else{ $filePath = $uploadDir . $fileName; } ////generate random name //// get the file extension first $ext = substr(strrchr($fileName, "."), 1); // //// make the random file name $randName = md5(rand() * time()); // //// and now we have the unique file name for the upload file $filePath = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath = str_replace(" ", "_", $filePath); $result = move_uploaded_file($tmpName, $filePath); //if (!$result) { //echo "Error uploading file"; //exit; //} if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileinsert[]=$filePath; } $id_trabajo = $colname_trabajos_selected_RS; $query = "INSERT INTO t_imagenes_trabajos (imagen_path,id_trabajo) ". "VALUES ('$filePath','$id_trabajo')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); //echo "<br>Files uploaded<br>"; header("Location: PC_images_display.php?id_trabajo=$colname_trabajos_selected_RS&imageninserted=true"); /* * PHP GD * adding watermark to an image with GD library */ // Load the watermark and the photo to apply the watermark to $stamp = '../img/watermark_fotos.png'; // Set the margins for the stamp and get the height/width of the stamp image $margin['x'] = 10; $margin['y'] = 10; $destination = $filePath; watermarkImage($filePath, $stamp, $margin, $destination); } Hi, I tried that but still not placing the watermark over the image, just saving the watermark aolone to my db... Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 26, 2014 Share Posted July 26, 2014 the first parameter to the watermarkImage() function is the watermark, the second parameter is the image to watermark. Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 26, 2014 Share Posted July 26, 2014 the first parameter to the watermarkImage() function is the watermark, the second parameter is the image to watermark. Ahh, good eye. I got those backwards when I gave you the code. This line watermarkImage($filePath, $stamp, $margin, $destination); Should be this watermarkImage($stamp, $filePath, $margin, $destination); Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 27, 2014 Author Share Posted July 27, 2014 Thanks it works now!!!... but I now need to understand how it works. Where do I change wtermark size and position? Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 27, 2014 Share Posted July 27, 2014 The watermark size is based on the actual watermark image you are using. The position is controlled by the $margin vars. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted July 27, 2014 Author Share Posted July 27, 2014 ah ok, thanks.. so this places the watermark on the top left // Set the margins for the stamp and get the height/width of the stamp image $margin['x'] = 20; $margin['y'] = 20; what if I want it on the bottom right?. I have tried "-" values and it didn't work Quote Link to comment Share on other sites More sharing options...
CroNiX Posted July 27, 2014 Share Posted July 27, 2014 (edited) You'd have to calculate the size of the image receiving the watermark. Let's say the original image TO be watermarked is 100 wide and 100 tall. x = 100, y = 100 would be the very far bottom right. let's say the watermark image itself that is being applied to the original image is 10 wide and 10 tall. So you'd take 100 - 10 for the x value, and same for the y value. So you'd put the watermark at x=90, y=90 to be in the bottom right. You are already getting the sizes of the watermark image an the source image here: $wm_src = getimagesize($wm);$source = getimagesize($img); So you just need to use them in your calculation Edited July 27, 2014 by CroNiX Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.