smonkcaptain Posted July 22, 2010 Share Posted July 22, 2010 Hey all. I currently have an Upload.php page, and a Watermark.php page, which uploads the image, then goes to the watermark page to apply the watermark. I'm currently having quality lose issues with this method, so i was wondering if i could have the watermark to be applied directly on the uploaded file, before saving it. So rather than getting the image from the save location, it get's it directly from the upload page. i tried $im=$_FILES['photo]'['tmpname']; but it didnt work. Here are the two PHP pages: <?php include '../connect.php'; error_reporting(0); session_start(); $username=$_SESSION['username']; // make a note of the current working directory, relative to root. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']); // make a note of the directory that will recieve the uploaded file $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '../queued/'; // make a note of the location of the upload form in case we need it $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . '../members/photoupload.php'; // make a note of the location of the success page $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . '../members/upload_success.php'; // fieldname used within the file <input> of the HTML form $fieldname = 'photo'; // possible PHP upload errors $errors = array(1 => 'php.ini max file size exceeded', 2 => 'html form max file size exceeded', 3 => 'file upload was only partial', 4 => 'no file was attached'); // check the upload form was actually submitted else print the form isset($_POST['submit']) or error('the upload form is neaded', $uploadForm); // check for PHP's built-in uploading errors ($_FILES[$fieldname]['error'] == 0) or error($errors[$_FILES[$fieldname]['error']], $uploadForm); // check that the file we are working on really was the subject of an HTTP upload @is_uploaded_file($_FILES[$fieldname]['tmp_name']) or error('not an HTTP upload', $uploadForm); // validation... since this is an image upload script we should run a check // to make sure the uploaded file is in fact an image. Here is a simple check: // getimagesize() returns false if the file tested is not an image. @getimagesize($_FILES[$fieldname]['tmp_name']) or error('only image uploads are allowed', $uploadForm); // make a unique filename for the uploaded file and check it is not already // taken... if it is already taken keep trying until we find a vacant one // sample filename: 1140732936-filename.jpg $now = time(); while(file_exists($uploadFilename = $uploadsDirectory.$now.'.jpg')) { $now++; } // now let's move the file to its final location and allocate the new filename to it @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename) or error('receiving directory insuffiecient permission', $uploadForm); //This gets all the other information from the form $genre=$_POST['genre']; $aircraft=$_POST['aircraft']; $airline=$_POST['airline']; $airport=$_POST['airport']; $country=$_POST['country']; $registration=$_POST['registration']; $connumber=$_POST['connumber']; $day=$_POST['day']; $month=$_POST['month']; $year=$_POST['year']; $remarks=$_POST['remarks']; $photo=($_FILES['photo']); $icao=$_POST['icao']; $iata=$_POST['iata']; $getid=mysql_query("SELECT id FROM `members` WHERE username LIKE '$username'"); while($id=mysql_fetch_array($getid)){ $photographerid= $id['id']; } $date=$day.'-'.$month.'-'.$year; foreach($_POST['tag'] as $tag){ if($tag){ $tags[] = $tag; } } $tag = implode('+',$tags); //Writes the information to the database mysql_query("INSERT INTO `queued_photos` (`username`, `photographerid`, `genre`, `aircraft`, `airline`, `country`, `airport`, `registration`,`msn`, `category`, `date`, `photoname`, `remarks`) VALUES ('$username', '$photographerid', '$genre', '$aircraft', '$airline', '$country', '$airport', '$registration', '$connumber', '$tag', '$date', '$now', '$remarks')")or die(mysql_error()); $position=$_POST['position']; $size=$_POST['size']; $strength=$_POST['strength']; $watermark =$_POST['watermark']; if($watermark=="No"){ header("Location: watermark.php?id=$now&option=no"); }elseif ($watermark=="Yes"){ // If you got this far, everything has worked and the file has been successfully saved. // We are now going to redirect the client to a success page. header("Location: watermark.php?id=$now&size=$size&strength=$strength&position=$position&width=$imwidth"); } ?> and heres the watermark.php script: <?php ini_set("memory_limit","512M"); $username="Jimmy Leaman"; if(isset($_GET['id'])){ $id = $_GET['id']; } if(isset($_GET['option'])){ $option = $_GET['option']; } if(isset($_GET['strength'])){ $strength= $_GET['strength']; } if(isset($_GET['size'])){ $size = $_GET['size']; } if(isset($_GET['position'])){ $position = $_GET['position']; } if(isset($_GET['width'])){ $width = $_GET['width']; } if($option=="no"){ $stamp = imagecreatefrompng('none.png'); } if($strength=="verylight" & $size=="small"){ $stamp = imagecreatefrompng('watermarks_vlight.png'); } if($strength=="verylight" & $size=="med"){ $stamp = imagecreatefrompng('watermarkm_vlight.png'); } if($width>="1024" & $strength=="verylight" & $size=="large"){ $stamp = imagecreatefrompng('watermarkl_vlight.png'); } if($width<="1024" & $strength=="verylight" & $size=="large"){ $stamp = imagecreatefrompng('watermarks_vlight.png'); } if($strength=="verylight" & $size=="verylarge"){ $stamp = imagecreatefrompng('watermarkxl_vlight.png'); } if($strength=="light" & $size=="small"){ $stamp = imagecreatefrompng('watermarks_light.png'); } if($strength=="light" & $size=="med"){ $stamp = imagecreatefrompng('watermarkm_light.png'); } if($width>="1024" & $strength=="light" & $size=="large"){ $stamp = imagecreatefrompng('watermarkl_light.png'); } if($width<="1024" & $strength=="light" & $size=="large"){ $stamp = imagecreatefrompng('watermarks_light.png'); } if($strength=="light" & $size=="verylarge"){ $stamp = imagecreatefrompng('watermarkxl_light.png'); } if($strength=="med" & $size=="small"){ $stamp = imagecreatefrompng('watermarks_med.png'); } if($strength=="med" & $size=="med"){ $stamp = imagecreatefrompng('watermarkm_med.png'); } if($width>="1024" & $strength=="med" & $size=="large"){ $stamp = imagecreatefrompng('watermarkl_med.png'); } if($width<="1024" & $strength=="med" & $size=="large"){ $stamp = imagecreatefrompng('watermarks_med.png'); } if($strength=="med" & $size=="verylarge"){ $stamp = imagecreatefrompng('watermarkxl_med.png'); } if($strength=="strong" & $size=="small"){ $stamp = imagecreatefrompng('watermarks_strong.png'); } if($strength=="strong" & $size=="med"){ $stamp = imagecreatefrompng('watermarkm_strong.png'); } if($width>="1024" & $strength=="strong" & $size=="large"){ $stamp = imagecreatefrompng('watermarkl_strong.png'); } if($width<="1024" & $strength=="strong" & $size=="large"){ $stamp = imagecreatefrompng('watermarks_strong.png'); } if($strength=="strong" & $size=="verylarge"){ $stamp = imagecreatefrompng('watermarkxl_strong.png'); } list($width, $height) = getimagesize('../queued/'.$id.'.jpg'); $sx = imagesx($stamp); $sy = imagesy($stamp); if($position=="top"){ $marge_right = ($width-$sx)/2; $marge_bottom = ($height)-(0.07*$height)-($sy)+20; } if($position=="centre"){ $marge_right = ($width-$sx)/2; $marge_bottom = ($height-$sy)/2; } if($position=="bottom"){ $marge_right = ($width-$sx)/2 ; $marge_bottom = (0.07*$height); } // Load the stamp and the photo to apply the watermark to $im = imagecreatefromjpeg('../queued/'.$id.'.jpg'); // Set the margins for the stamp and get the height/width of the stamp image $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)); // Create the image $cim = imagecreatetruecolor($width/2,20); // Create some colors $white = imagecolorallocate($cim, 255, 255, 255); $grey = imagecolorallocate($cim, 128, 128, 128); $black = imagecolorallocate($cim, 0, 0, 0); imagefilledrectangle($cim, 0, 0, 399, 29, $black); // The text to draw $text = 'Aviation-Photographs.net'; // Replace path by your own font path $font = 'BOMBARD.ttf'; // Add the text imagettftext($cim, 13, 0, $width/2-195, 15, $white, $font, $text); imagecopy($im, $cim, $width-$width/2, $height-20, 0, 0, imagesx($cim), imagesy($cim)); // Create the image $cim2 = imagecreatetruecolor($width/2,20); // Create some colors $white2 = imagecolorallocate($cim2, 255, 255, 255); $grey2 = imagecolorallocate($cim2, 128, 128, 128); $black2 = imagecolorallocate($cim2, 0, 0, 0); imagefilledrectangle($cim2, 0, 0, 399, 29, $black); // The text to draw $text2 = 'Copyright - '.$username.''; // Replace path by your own font path $font2 = 'EuroScope.ttf'; // Add the text imagettftext($cim2, 10, 0, 10, 14, $white2, $font2, $text2); imagecopy($im, $cim2, 0, $height-20, 0, 0, imagesx($cim2), imagesy($cim2)); imagejpeg($im, '../queued/'.$id.'_b.jpg', 100); // Using imagepng() results in clearer text compared with imagejpeg() header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?> Thanks all Link to comment https://forums.phpfreaks.com/topic/208485-how-to-apply-watermark-directly-from-uploaded-file/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.