Watermark

Discussion in 'PHP' started by aruffell, Dec 12, 2010.

  1. #1
    I need some kind of a custom rewriterule to place watermarks onto my website.

    I have files called:

    watermark.png
    empty.png
    watermark.php which includes the following code:

    <? 
    $src = $_GET['src']; 
    
    header('Content-type: image/jpeg'); 
    
    //this will prevent the watermark from showing up in the thumbnail images 
    
    $size = getimagesize($src); 
    if ($size[1] < 400) { 
    $watermark = imagecreatefrompng('empty.png'); 
    } else { 
    $watermark = imagecreatefrompng('watermark.png'); 
    } 
    
    $watermark_width = imagesx($watermark); 
    $watermark_height = imagesy($watermark); 
    $image = imagecreatetruecolor($watermark_width, $watermark_height); 
    
    if(eregi('.gif',$src)) { 
    $image = imagecreatefromgif($src); 
    } 
    elseif(eregi('.jpeg',$src)||eregi('.jpg',$src)) { 
    $image = imagecreatefromjpeg($src); 
    } 
    elseif(eregi('.png',$src)) { 
    $image = imagecreatefrompng($src); 
    } 
    else { 
    exit("Your image is not a gif, jpeg or png image. Sorry."); 
    } 
    
    $dest_x = ($size[0] - $watermark_width) / 2; 
    $dest_y = ($size[1] - $watermark_height) / 2;   
    imagecolortransparent($watermark,imagecolorat($watermark,0,0)); 
    imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height); 
    
    imagejpeg($image, "", 95); 
    imagedestroy($image); 
    imagedestroy($watermark); 
    ?>
    PHP:
    All of these files are in the root directory.

    I just need the htaccess rewriterule code if anyone can shed some light as to what it would be.

    I've found the same system being used on a wordpress site with the following rewrite - RewriteRule ^(.*)wp-content/uploads/(.*) $1watermark.php?src=wp-content/uploads/$2

    Sadly i don't know where to put this in my htaccess file and what to edit the file paths too?

    I'm using vBulletin with VbSEO so my images are at /attachments/ even though they are in root/images/attachments

    Really need this sorted tonight as i've got some important images going up tonight that i don't want stolen.

    Many thanks,

    Andy
     
    aruffell, Dec 12, 2010 IP
  2. aruffell

    aruffell Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    Ok i found a different plugin which i think could be altered.

    All i need is so that it only watermarks on images that are 400px wide or greater.

    Here is the vBulletin code:

    // begin variables
    
    // the path to the PNG file that you want to overlay with
    // must be on the local machine, not an http:// URL
    $WATERMARK_PNG_FILE = '/home/aruffell/public_html/watermark.png';
    
    // how you want to position the watermark enter either center or bottom_left
    $WATERMARK_POSITION = 'center';
    
    // user you want to see watermarking on - this is just for testing
    // set to 0 to work for all users
    $VB_USER_ID = 0;
    
    // end variables - you shouldn't need to edit below here
    
    
    // if there's no filepath to work with there's nothing we can do
    // we also need the watermark file to exist
    // if $VB_USER_ID is specified (for testing) show it just them
    if(! empty($attachpath) && file_exists($WATERMARK_PNG_FILE) && ( $VB_USER_ID == 0 || $GLOBALS['vbulletin']->userinfo['userid'] == $VB_USER_ID )) {
    
    
    	// derive output name
    	$fo = preg_replace('/\.attach$/', '.marked', $attachpath);
    
    	// image doesn't exist or thumbnail is newer than the cached file - create a watermarked version
    	if( (! file_exists($fo) || filemtime($WATERMARK_PNG_FILE) > filemtime($fo) || true) && $fo != $attachpath ) {
    
    		// decide what image type it is
    		if( preg_match('/\.png$/i', $attachmentinfo['filename']) ) {
    			$im = @imagecreatefrompng($attachpath);
    		} elseif( preg_match('/\.jpg$/i', $attachmentinfo['filename']) ) {
    			$im = @imagecreatefromjpeg($attachpath);
    		
    			// create an empty truecolor container
    			$tempimage = @imagecreatetruecolor(@imagesx($im), @imagesy($im));
           
    			// copy the 8-bit gif into the truecolor image
    			@imagecopy($tempimage, $im,
    				0, 0, 0, 0, 
    				@imagesx($im), @imagesy($im)
    			);
          
    			// copy the source_id int
    			$im = $tempimage;
    		}
    
    		// open the watermark image
    		$wm = @imagecreatefrompng($WATERMARK_PNG_FILE);
    
    		@imagealphablending($wm, false);
    		@imagesavealpha($wm, true);
    
    		// catch opening problems
    		if($im && $wm) {
    
    			if($WATERMARK_POSITION == 'center') {
    				$pos_x = (imagesx($im) / 2) - (imagesx($wm) / 2);
    				$pos_y = (imagesy($im) / 2) - (imagesy($wm) / 2);
    			} else {
    				$pos_x = imagesx($im) - imagesx($wm) - 10;
    				$pos_y = imagesy($im) - imagesy($wm) - 10;
    			}
    
    
    			// merge the files together
    			$copy_worked = @imagecopy($im,$wm,
         				$pos_x, $pos_y,
    				0, 0, imagesx($wm), imagesy($wm)
    			);
    		}
    
    		// write out the new image
    		if($copy_worked) {
    
    			// decide what image type it is
    			if( preg_match('/\.png$/i', $attachmentinfo['filename']) ) {
    				@imagepng($im,$fo);
    			} elseif( preg_match('/\.jpg$/i', $attachmentinfo['filename']) ) {
    				@imagejpeg($im,$fo);
    			} elseif( preg_match('/\.gif$/i', $attachmentinfo['filename']) ) {
    				@imagegif($im,$fo);
    			}
    		}
    	}
    
    	// check that the new file is there - if so open the file pointer to it
    	if( @filesize($fo) > 0 && $fp2 = fopen($fo,'rb') ) {
    
    		// re-send image size header
    		header('Content-Length: '. filesize($fo));
    
    		@fclose($fp);
    
    		$fp = $fp2;
    
    	}
    }
    Code (markup):
    Any ideas?

    Thanks,

    Andy
     
    aruffell, Dec 13, 2010 IP