gerkintrigg Posted July 17, 2007 Share Posted July 17, 2007 Hi everyone. i'm trying to work out how to watermark an image... I have done it before at build.nextdaygraphics.com but I'm trying to do it for the new design at www.nextdaygraphics.com and it's not working as I'd like... the code I have is: <?php ob_start(); $edgePadding=15; // used when placing the watermark near an edge $quality=100; // used when generating the final image $default_watermark='ndg.png'; // the default image to use if no watermark was chosen $watermark = "ndg.png"; // location of watermark $original = $img; //location of original $_POST['watermark']='ndg.png'; $_POST['process']='y'; if(isset($_POST['process'])){ // an image has been posted, let's get to the nitty-gritty if(isset($original)){ // be sure that the other options we need have some kind of value if(!isset($_POST['save_as'])) $_POST['save_as']='jpeg'; if(!isset($_POST['v_position'])) $_POST['v_position']='center'; if(!isset($_POST['h_position'])) $_POST['h_position']='center'; if(!isset($_POST['wm_size'])) $_POST['wm_size']='1'; if(!isset($_POST['watermark'])) $_POST['']=$default_watermark; if(!isset($_POST['watermarkee'])) $_POST['']=$original; // file upload success $size=getimagesize($original); if($size[2]==2 || $size[2]==3){ // it was a JPEG or PNG image, so we're OK so far //$original=$_FILES['watermarkee']['tmp_name']; $target_name=date('YmdHis').'_'. // if you change this regex, be sure to change it in generated-images.php:26 preg_replace('`[^a-z0-9-_.]`i','',$original); $target=dirname(__FILE__).'/results/'.$target_name; $watermark=dirname(__FILE__).'/watermarks/'.$_POST['watermark']; $wmTarget=$watermark.'.tmp'; $origInfo = getimagesize($original); $origWidth = $origInfo[0]; $origHeight = $origInfo[1]; $waterMarkInfo = getimagesize($watermark); $waterMarkWidth = $waterMarkInfo[0]; $waterMarkHeight = $waterMarkInfo[1]; // watermark sizing info if($_POST['wm_size']=='larger'){ $placementX=0; $placementY=0; $_POST['h_position']='center'; $_POST['v_position']='center'; $waterMarkDestWidth=$waterMarkWidth; $waterMarkDestHeight=$waterMarkHeight; // both of the watermark dimensions need to be 5% more than the original image... // adjust width first. if($waterMarkWidth > $origWidth*1.05 && $waterMarkHeight > $origHeight*1.05){ // both are already larger than the original by at least 5%... // we need to make the watermark *smaller* for this one. // where is the largest difference? $wdiff=$waterMarkDestWidth - $origWidth; $hdiff=$waterMarkDestHeight - $origHeight; if($wdiff > $hdiff){ // the width has the largest difference - get percentage $sizer=($wdiff/$waterMarkDestWidth)-0.05; }else{ $sizer=($hdiff/$waterMarkDestHeight)-0.05; } $waterMarkDestWidth-=$waterMarkDestWidth * $sizer; $waterMarkDestHeight-=$waterMarkDestHeight * $sizer; }else{ // the watermark will need to be enlarged for this one // where is the largest difference? $wdiff=$origWidth - $waterMarkDestWidth; $hdiff=$origHeight - $waterMarkDestHeight; if($wdiff > $hdiff){ // the width has the largest difference - get percentage $sizer=($wdiff/$waterMarkDestWidth)+0.05; }else{ $sizer=($hdiff/$waterMarkDestHeight)+0.05; } $waterMarkDestWidth+=$waterMarkDestWidth * $sizer; $waterMarkDestHeight+=$waterMarkDestHeight * $sizer; } }else{ $waterMarkDestWidth=round($origWidth * floatval($_POST['wm_size'])); $waterMarkDestHeight=round($origHeight * floatval($_POST['wm_size'])); if($_POST['wm_size']==1){ $waterMarkDestWidth-=2*$edgePadding; $waterMarkDestHeight-=2*$edgePadding; } } // OK, we have what size we want the watermark to be, time to scale the watermark image resize_png_image($watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget); // get the size info for this watermark. $wmInfo=getimagesize($wmTarget); $waterMarkDestWidth=$wmInfo[0]; $waterMarkDestHeight=$wmInfo[1]; $differenceX = $origWidth - $waterMarkDestWidth; $differenceY = $origHeight - $waterMarkDestHeight; // where to place the watermark? switch($_POST['h_position']){ // find the X coord for placement case 'left': $placementX = $edgePadding; break; case 'center': $placementX = round($differenceX / 2); break; case 'right': $placementX = $origWidth - $waterMarkDestWidth - $edgePadding; break; } switch($_POST['v_position']){ // find the Y coord for placement case 'top': $placementY = $edgePadding; break; case 'center': $placementY = round($differenceY / 2); break; case 'bottom': $placementY = $origHeight - $waterMarkDestHeight - $edgePadding; break; } if($size[2]==3) $resultImage = imagecreatefrompng($original); else $resultImage = imagecreatefromjpeg($original); imagealphablending($resultImage, TRUE); $finalWaterMarkImage = imagecreatefrompng($wmTarget); $finalWaterMarkWidth = imagesx($finalWaterMarkImage); $finalWaterMarkHeight = imagesy($finalWaterMarkImage); imagecopy($resultImage, $finalWaterMarkImage, $placementX, $placementY, 0, 0, $finalWaterMarkWidth, $finalWaterMarkHeight ); if($size[2]==3){ imagealphablending($resultImage,FALSE); imagesavealpha($resultImage,TRUE); imagepng($resultImage,$target,$quality); }else{ imagejpeg($resultImage,$target,$quality); } imagedestroy($resultImage); imagedestroy($finalWaterMarkImage); // display resulting image for download $watermarked='watermark/results/'.$target_name; unlink($wmTarget); }else{ ?> <div> <span class="Brown_Header_Text">Watermarked Image</span> <p class="boldText">The image uploaded was not a JPEG or PNG image.</p> </div> <?php } }else{ ?> <div> <span class="Brown_Header_Text">Watermarked Image</span> <p class="boldText">Unable to upload the image to watermark.</p> </div> <?php } } $page_display=ob_get_clean(); echo $page_display;?> The error I'm getting is the output code: <div> <span class="Brown_Header_Text">Watermarked Image</span> <p class="boldText">Unable to upload the image to watermark.</p> </div> But i have no idea why it's hitting this else statement... (I didn't write this code and am rather at a loss with the GD library) I'm doing it slightly differently to the first site for a number of reasons and think that this code should work... but it's not. any suggestions please? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 17, 2007 Share Posted July 17, 2007 looks like "process" isn't being set in the form... try print_r($_POST); at the top of the code and post the results here Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted July 17, 2007 Author Share Posted July 17, 2007 Good call it was entirely to do with posted variables and a call to an undefined function. Once i included the function and changed the posted variables, it worked. Thanks a lot! 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.