0){ $wpipp_exclude_from_watermark = (bool) get_post_meta($original_image_post_id, 'wpipp_exclude_from_watermark', true); } if($wpipp_exclude_from_watermark){ //if image is excluded return the original image unaltered and exit render_original_image(); } }//end exclude_from_watermark_functions /* * minimum image size functions */ function minimum_image_size_functions(){ global $wpipp_minimum_image_size_string, $image_width, $image_height; //if the orginal image width and height is less than the minimum required image size specified in settings, do not watermark $wpipp_minimum_image_size_int = 0; if(is_numeric($wpipp_minimum_image_size_string)){ $wpipp_minimum_image_size_int = intval($wpipp_minimum_image_size_string); } //ensure image is loaded by ensuring $image_width is greater than 0 if($image_width < $wpipp_minimum_image_size_int and $image_height < $wpipp_minimum_image_size_int and $image_width > 0){ //if the image is too small for watermarking, return the original image unaltered and exit render_original_image(); } }//end minimum_image_size_functions /* * text watermark */ function text_watermark($text = NULL, $background_colour = NULL, $text_colour = NULL){ global $wpipp_watermark_text_string, $original_image_post_id, $wpipp_watermark_text_border_width_string, $wpipp_watermark_text_colour_string, $wpipp_watermark_text_background_colour_string, $font_size, $font_angle, $font; if(pathinfo($font, PATHINFO_EXTENSION)==''){ //add ttf extension $font = $font . '.ttf'; } if($text == NULL){ $text = $wpipp_watermark_text_string; if(function_exists('render_special_text') && $original_image_post_id) { $text = render_special_text($text, $original_image_post_id); } } //assign default text if no src if($text == "" ){ $text = "WPIPP - Text Undefined"; } if($background_colour == NULL){ $background_colour = $wpipp_watermark_text_background_colour_string; } if($text_colour == NULL){ $text_colour = $wpipp_watermark_text_colour_string; } //set the border, default to 5 $wpipp_watermark_text_border_width_int = 5; if(is_null($wpipp_watermark_text_border_width_string) or $wpipp_watermark_text_border_width_string=="0") { //default border to 1/4 of the font size $wpipp_watermark_text_border_width_int == $font_size/4; } else { $wpipp_watermark_text_border_width_int = intval($wpipp_watermark_text_border_width_string); } //peg lower limit at 1 if($wpipp_watermark_text_border_width_int<1){ $wpipp_watermark_text_border_width_int = 1; } //peg upper limit at 100 if($wpipp_watermark_text_border_width_int>100){ $wpipp_watermark_text_border_width_int = 100; } $border = $wpipp_watermark_text_border_width_int; // First we create our bounding box for the first text $bbox = imagettfbbox($font_size, $font_angle, $font, $text); // This is our cordinates for X and Y $bbox_x = ($bbox[4]- $bbox[0]) ; $bbox_y = ($bbox[5]-$bbox[1])*-1; // Create the watermark image $watermark = imagecreatetruecolor($bbox_x + ($border*2), $bbox_y + ($border*2)) ; if($background_colour==""){ $background_colour = "none"; } //make the background image transparent if($background_colour=="none"){ //make backgground transparent $transparent_colour = imagecolorallocatealpha($watermark, 0, 0, 0, 127); imagefill($watermark, 0, 0, $transparent_colour); } else { $fillcolourarray = get_rgb_from_hex($background_colour); $fillcolour = imagecolorallocate($watermark, $fillcolourarray['red'], $fillcolourarray['green'], $fillcolourarray['blue']); imagefill($watermark, 0, 0, $fillcolour); } // Create some colors $white = imagecolorallocate($watermark, 255, 255, 255); $grey = imagecolorallocate($watermark, 128, 128, 128); $black = imagecolorallocate($watermark, 0, 0, 0); //imagefilledrectangle($watermark, 0, 0, $bbox_x+($border*2), $bbox_y+($border*2), $grey); //bounding box //imagefilledrectangle($watermark, $border, $border, $bbox_x+$border, $bbox_y+$border, $white); $font_colour = $black; if($text_colour){ $custom_colour = get_rgb_from_hex($text_colour); $font_colour = imagecolorallocate($watermark, $custom_colour['red'], $custom_colour['green'], $custom_colour['blue']); } // Add the text imagettftext($watermark, $font_size, $font_angle, $border, $font_size+$border, $font_colour, $font, $text); return $watermark; die(); }//end text_watermark /* * scale watermark * scales the watermark * watermark - the watermark to be used * original_image - the original target image * returns - the scaled watermark */ function scale_watermark_to_image($watermark, $original_image){ global $watermark_width, $watermark_height, $original_image_width, $original_image_height, $wpipp_watermark_scale_string, $wpipp_debug_message_string; if($wpipp_watermark_scale_string=="on"){ //get the width and height of the watermark and original image $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $original_image_width = imagesx($original_image); $original_image_height = imagesy($original_image); //scaling factor is 266 h $scaling = (( $watermark_height / 266 ) * $original_image_height) / $watermark_height; $new_width = $watermark_width * $scaling; $new_height = $watermark_height * $scaling; $resized_watermark = imagecreatetruecolor($new_width, $new_height); //keep transparent aspects imagealphablending($resized_watermark, false); $success = imagecopyresampled($resized_watermark, $watermark, 0, 0, 0, 0, $new_width, $new_height, $watermark_width, $watermark_height); if($success){ return $resized_watermark; } else { $wpipp_debug_message_string = $wpipp_debug_message_string . " " . "Could not resize watermark"; return $watermark; } } else { return $watermark; } } /* * place watermark on image * watermark - the watermark to be used * original_image - the image to place the watermark on * position - the position of the watermark * returns - the original_image with watermark applied in position */ function place_watermark_on_image($watermark, $original_image, $position){ global $watermark_width, $watermark_height, $original_image_width, $original_image_height, $wpipp_watermark_transparency_string; //get the width and height of the watermark and original image $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $original_image_width = imagesx($original_image); $original_image_height = imagesy($original_image); if($position == "rp"){ //Logic for repeat x,y $y_pos = 0; //repeat y start until > watermark height while($y_pos < $original_image_height){ $x_pos = 0; //repeat x start until > watermark width while($x_pos < $original_image_width){ if(function_exists('wpipp_copy_and_merge')){ $original_image = wpipp_copy_and_merge($original_image, $watermark, $x_pos, $y_pos, $watermark_width, $watermark_height, $wpipp_watermark_transparency_string); } else { imagecopy($original_image, $watermark, $x_pos, $y_pos, 0, 0, $watermark_width, $watermark_height); } //next x position $x_pos = $x_pos + $watermark_width; } //next y position $y_pos = $y_pos + $watermark_height; } }else{ //logic for no repeat x, y $location_array = calculate_destination_coordinates($watermark, $original_image, $position); $x_pos = $location_array['dest_x']; $y_pos = $location_array['dest_y']; //use imagecopy if opacity = 100 (ie none), otherwise alpha is disregarded with imagecopymerge if(function_exists('wpipp_copy_and_merge')){ $original_image = wpipp_copy_and_merge($original_image, $watermark, $x_pos, $y_pos, $watermark_width, $watermark_height, $wpipp_watermark_transparency_string); } else { imagecopy($original_image, $watermark, $x_pos, $y_pos, 0, 0, $watermark_width, $watermark_height); } } return $original_image; }//end place_watermark_on_image /* * calculate destination coordinates * returns destination coordinates in array('dest_x' => $dest_x, 'dest_y' => $dest_y); */ function calculate_destination_coordinates($watermark, $original_image, $position){ global $original_image_width, $original_image_height, $watermark_width, $watermark_height; //@todo - check max proportions of watermark compared with orginal and resize if necessary - suggest 25% of image height if($position == ""){ //set position as bottom right if not set $position = "br"; } //defaults $dest_x = 0; $dest_y = 0; if($position == "tl"){ //top left // Set the destination positions of the watermark //x is 0 $dest_x = 0; //y is 0 $dest_y = 0; } elseif($position == "tc"){ //top centre // Set the destination positions of the watermark //x is (image width - watermark width)/2 $dest_x = ($original_image_width - $watermark_width)/2; //y is 0 $dest_y = 0; } elseif($position == "tr"){ //top right // Set the destination positions of the watermark //x is image width - watermark width $dest_x = $original_image_width - $watermark_width; //y is 0 $dest_y = 0; } elseif($position == "cl"){ //centre left // Set the destination positions of the watermark //x is 0 $dest_x = 0; //y is (image height - watermark height)/2 $dest_y = ($original_image_height - $watermark_height)/2; } elseif($position == "cc"){ //centre centre // Set the destination positions of the watermark //x is (image width - watermark width)/2 $dest_x = ($original_image_width - $watermark_width)/2; //y is (image height - watermark height)/2 $dest_y = ($original_image_height - $watermark_height)/2; } elseif($position == "cr"){ //centre right // Set the destination positions of the watermark //x is image width - watermark width $dest_x = $original_image_width - $watermark_width; //y is (image height - watermark height)/2 $dest_y = ($original_image_height - $watermark_height)/2; } elseif($position == "bl"){ //bottom right // Set the destination positions of the watermark //x is 0 $dest_x = 0; //y is image height - watermark height $dest_y = $original_image_height - $watermark_height; } elseif($position == "bc"){ //bottom centre // Set the destination positions of the watermark //x is (image width - watermark width)/2 $dest_x = ($original_image_width - $watermark_width)/2; //y is image height - watermark height $dest_y = $original_image_height - $watermark_height; } elseif($position == "br"){ //bottom right // Set the destination positions of the watermark //x is img width- watermark width $dest_x = $original_image_width - $watermark_width; //y is image height - watermark height $dest_y = $original_image_height - $watermark_height; } return array('dest_x' => $dest_x, 'dest_y' => $dest_y); }//end calculate_destination_coordinates /** * converts a hex colour to an rgb representation * returns (array) the RGB values for the parameter hex_colour supplied */ function get_rgb_from_hex($hex_colour) { $hex_colour = str_replace("#", "", $hex_colour); if(strlen($hex_colour) == 6) { $red = hexdec(substr($hex_colour,0,2)); $green = hexdec(substr($hex_colour,2,2)); $blue = hexdec(substr($hex_colour,4,2)); } else { //repeat the hex value for shortcode $red_hex = substr($hex_colour,0,1); $green_hex = substr($hex_colour,1,1); $blue_hex = substr($hex_colour,2,1); $red = hexdec($red_hex.$red_hex); $green = hexdec($green_hex.$green_hex); $blue = hexdec($blue_hex.$blue_hex); } $rgb = array('red' => $red, 'green' => $green, 'blue' => $blue); return $rgb; } /** * returns a post id for the image url supplied * returns (int) post id for image url or 0 if not found */ function get_post_id_from_url($url_string){ global $wpdb; $result = 0; //cast the url to the original url guid if it's a thumbnail, large etc. $url_string = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $url_string ); $results = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts where guid = %s AND post_type = 'attachment'", $url_string ) ); foreach ( $results as $row ) { $result = $row->ID ; } return $result; } /* * render original image */ function render_original_image(){ global $original_image_path, $wpipp_render_option_string; //attempt to open the file, surpressing errors $file_passthrough = @fopen($original_image_path, 'rb'); if($file_passthrough!=false){ // Set the content-type header('Content-Type: image/jpeg'); // process the image fpassthru($file_passthrough); } else { render_text("Cannot load original image"); } exit; }//end render_original_image /* * render text * text - the text to render */ function render_text($text){ global $wpipp_render_option_string; $text_image_display = imagecreate(600, 50); // White background and blue text $bg = imagecolorallocate($text_image_display, 255, 255, 255); $textcolor = imagecolorallocate($text_image_display, 0, 0, 255); // Write the string at the top left imagestring($text_image_display, 5, 0, 0, $text, $textcolor); // Set the content-type if($wpipp_render_option_string=="PNG"){ header('Content-Type: image/png'); imagepng($text_image_display); } else { header('Content-Type: image/jpeg'); imagejpeg($text_image_display); } imagedestroy($text_image_display); exit; }//end render_text /* * render watermarked image * watermarked_image - the watermarked image to be rendered */ function render_watermark_image($watermarked_image){ global $wpipp_render_option_string, $original_image; // Using imagepng() results in clearer text compared with imagejpeg() // todo - we should be using this notation, but appears incompatible with PHP 5 - imagepng($original_image, NULL, $q); if($wpipp_render_option_string=="PNG"){ header('Content-Type: image/png'); imagepng($original_image); } else { header('Content-Type: image/jpeg'); imagejpeg($original_image); } }//end render_watermarked_image /* * hotlinking functions */ function lite_hotlinking_functions(){ global $wpipp_hotlinking_option_string; //hotlinking prevention $wp_site_url = site_url("/"); $is_hotlink_detected = wpipp_lite_is_hotlink($wp_site_url); if($is_hotlink_detected){ //hotlinking action increment_hotlink_counter(); //increment hotlink attempt counter if($wpipp_hotlinking_option_string=="true"){ if(function_exists('render_hotlink')){ render_hotlink(); return; } } } else{ //none hotlinking action //do nothing } }//end hotlinking_functions /** * increments the hotlink counter */ function increment_hotlink_counter(){ global $original_image_post_id; $hotlink_count = get_post_meta($original_image_post_id, HOTLINK_COUNT_META_KEY, true); if(!$hotlink_count || $hotlink_count==''){ $hotlink_count = '0'; } $hotlink_count = intval($hotlink_count) + 1; update_post_meta($original_image_post_id, HOTLINK_COUNT_META_KEY, $hotlink_count); } /** * dummy method for plugging wp_is_post_revision and allowing update of post_meta */ function wp_is_post_revision(){ return false; } /** * returns referring site */ function wpipp_lite_get_referrer(){ //not referrer is spelt incorrectly by php $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL; return $referrer; } /** * returns boolean for if resource is being hotlinked * allow non-http referrs or those with no ref? */ function wpipp_lite_is_hotlink($sitename){ $referrer = wpipp_lite_get_referrer(); //needs to be triple equality as will return false if not found (otherwise will typecast) if(strpos($referrer, $sitename)===0){ return false; } else { return true; } } /** * debug functionality */ function debug_wpipp(){ global $wpipp_visible_enabled, $wpipp_visible_enabled_string, $wpipp_minimum_image_size, $wpipp_minimum_image_size_string, $wpipp_watermark_text, $wpipp_watermark_text_string, $wpipp_watermark_text_font, $wpipp_watermark_text_font_string, $wpipp_watermark_text_size, $wpipp_watermark_text_size_string, $wpipp_watermark_text_colour, $wpipp_watermark_text_colour_string, $wpipp_watermark_text_border_width, $wpipp_watermark_text_border_width_string, $wpipp_watermark_text_background_colour, $wpipp_watermark_text_background_colour_string, $wpipp_watermark_image_url, $wpipp_watermark_image_url_string, $wpipp_watermark_position, $wpipp_watermark_position_string, $wpipp_watermark_scale, $wpipp_watermark_scale_string, $wpipp_watermark_transparency, $wpipp_watermark_transparency_string, $wpipp_caching_option, $wpipp_caching_option_string, $wpipp_hotlinking_option, $wpipp_hotlinking_option_string, $original_image_post_id, $original_image_path, $relative_path, $full_image_src, $wpipp_debug_message_string, $wpipp_debug_option_string; $wpipp_debug = isset($_GET['wpipp_debug']) ? $_GET['wpipp_debug'] : ""; if(isset($wpipp_debug_option_string) and $wpipp_debug_option_string=="true") { $wpipp_debug = "true"; } if($wpipp_debug=="true"){ echo "

WP Image Protect Debug Values"; echo "

WordPress Options Values

"; echo "wpipp_visible_enabled_string" . "->" . $wpipp_visible_enabled_string; echo "
"; echo "wpipp_minimum_image_size_string" . "->" . $wpipp_minimum_image_size_string; echo "
"; echo "wpipp_watermark_text_string" . "->" . $wpipp_watermark_text_string; echo "
"; echo "wpipp_watermark_text_font_string" ."->" . $wpipp_watermark_text_font_string; echo "
"; echo "wpipp_watermark_text_size_string" . "->" . $wpipp_watermark_text_size_string; echo "
"; echo "wpipp_watermark_text_colour_string" . "->" . $wpipp_watermark_text_colour_string; echo "
"; echo "wpipp_watermark_text_border_width_string" . "->" . $wpipp_watermark_text_border_width_string; echo "
"; echo "wpipp_watermark_text_background_colour_string" . "->" . $wpipp_watermark_text_background_colour_string; echo "
"; echo "wpipp_watermark_image_url_string" ."->" . $wpipp_watermark_image_url_string; echo "
"; echo "wpipp_watermark_position_string" . "->" . $wpipp_watermark_position_string; echo "
"; echo "wpipp_watermark_scale_string" . "->" . $wpipp_watermark_scale_string; echo "
"; echo "wpipp_watermark_transparency_string" . "->" . $wpipp_watermark_transparency_string; echo "
"; echo "wpipp_caching_option_string" ."->" . $wpipp_caching_option_string; echo "
"; echo "wpipp_hotlinking_option_string" . "->" . $wpipp_hotlinking_option_string; echo "
"; echo "

Image Properties

"; echo "original_image_path" . "->" . $original_image_path; echo "
"; echo "relative_path" . "->" . $relative_path; echo "
"; echo "full_image_src" . "->" . $full_image_src; echo "
"; echo "original_image_post_id" . "->" . $original_image_post_id; echo "
"; echo "exclude from watermark (evaluation)" . "->" . get_post_meta($original_image_post_id, 'wpipp_exclude_from_watermark', true); echo "
"; echo "additional debug info" . "->" . $wpipp_debug_message_string; exit(0); } } load_wp_variables(); set_font_variables(); load_original_image_properties(); debug_wpipp(); increment_image_view_counter(); if(function_exists('exclude_from_watermark_functions')){ exclude_from_watermark_functions(); } lite_hotlinking_functions(); if(function_exists('caching_functions')){ caching_functions(); } load_original_image(); if(function_exists('minimum_image_size_functions')){ minimum_image_size_functions(); } $watermark = NULL; $watermark_type = $wpipp_visible_enabled_string; if($watermark_type=="text"){ $watermark = text_watermark(); } elseif($watermark_type=="image") { if(function_exists('image_watermark')){ $watermark = image_watermark(); } else { echo "error loading premium files"; } } else { render_original_image(); } if(!is_null($watermark)){ $watermark = scale_watermark_to_image($watermark, $original_image); $watermarked_image = place_watermark_on_image($watermark, $original_image, $wpipp_watermark_position_string); render_watermark_image($watermarked_image); imagedestroy($watermark); imagedestroy($watermarked_image); } imagedestroy($original_image); ?>