2012-09-12 72 views
1

在我用於縮略圖的主題中,我希望它們使用Ottos function.php腳本進行黑白處理。但是,當使用插件爲顏色較大的圖像添加水印時,黑白大拇指會變回顏色。單擊b/w時,Prettyphoto中將加載彩色照片。爲Wordpress添加水印功能function.php

所以我想我必須在function.php中添加水印,但是沒有解釋如何做到這一點。

因此,使用水印腳本和黑白腳本,我試圖合併這兩個......你猜對了,它阻止了站點加載,這並不奇怪。

所以我的問題是,有沒有人這樣做?它實際上會起作用嗎?

感謝

下面是我到目前爲止已經試過......對原圖上傳圖像時

add_action('after_setup_theme','themename_watermark'); 
function themename_watermark() { 
    add_image_size('watermark-test-image', 500, 650, true); 
} 




add_filter('wp_generate_attachment_metadata','themename_watermark'); 
function themename_watermark($meta) { 
    $file = wp_upload_dir(); 
    $file = trailingslashit($file['path']).$meta['sizes']['watermark-test-image']['file']; 
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file); 
    $img = wp_load_image($file); 




//watermark position 
$p = $_GET['p']; if(!$p) $p = 'br'; 
/* 
p can be anything from the following list: 
tl = top left 
tc = top center 
tr = top right 
cl = center left 
c = center of the image 
cr = center right 
bl = bottom left 
bc = bottom center 
br = bottom right 
*/ 
//watermarked image quality 
$q = $_GET['q']; 
//if the quality field is missing or is not on the 0 to 100 scale then we set the quality to 93 
if(!$q || $q<0 || $q>100) $q = '93'; 


$filetype = substr($img,strlen($img)-4,4); 
$filetype = strtolower($filetype); 
if($filetype == ".gif") $image = @imagecreatefromgif($img); 
if($filetype == ".jpg") $image = @imagecreatefromjpeg($img); 
if($filetype == ".png") $image = @imagecreatefrompng($img); 
if (!$image) die(); 

//getting the image size for the original image 
$img_w = imagesx($image); 
$img_h = imagesy($image); 
//if the filename has 150x150 in it's name then we don't apply the watermark 
if (eregi("155x194", $img)) { 
    imagejpeg($image, null, $q); die(); 
} else { 
    $watermark = @imagecreatefrompng('watermark.php'); 
} 
/* 
//if you want to use the watermark only on bigger images then use this instead of the condition above 
if ($img_w < "150") {//if image width is less then 150 pixels 
    imagejpeg($image, null, $q); die(); 
} else { 
    $watermark = @imagecreatefrompng('watermark.png'); 
} 
*/ 

//getting the image size for the watermark 
$w_w = imagesx($watermark); 
$w_h = imagesy($watermark); 

if($p == "tl") { 
    $dest_x = 0; 
    $dest_y = 0; 
} elseif ($p == "tc") { 
    $dest_x = ($img_w - $w_w)/2; 
    $dest_y = 0; 
} elseif ($p == "tr") { 
    $dest_x = $img_w - $w_w; 
    $dest_y = 0; 
} elseif ($p == "cl") { 
    $dest_x = 0; 
    $dest_y = ($img_h - $w_h)/2; 
} elseif ($p == "c") { 
    $dest_x = ($img_w - $w_w)/2; 
    $dest_y = ($img_h - $w_h)/2; 
} elseif ($p == "cr") { 
    $dest_x = $img_w - $w_w; 
    $dest_y = ($img_h - $w_h)/2; 
} elseif ($p == "bl") { 
    $dest_x = 0; 
    $dest_y = $img_h - $w_h; 
} elseif ($p == "bc") { 
    $dest_x = ($img_w - $w_w)/2; 
    $dest_y = $img_h - $w_h; 
} elseif ($p == "br") { 
    $dest_x = $img_w - $w_w; 
    $dest_y = $img_h - $w_h; 
} 

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $w_w, $w_h); 
imagejpeg($image, null, $q); 
imagedestroy($image); 
imagedestroy($watermark); 

if (function_exists('add_theme_support')) { 
add_theme_support('post-thumbnails'); 
set_post_thumbnail_size(500, 650); // default Post Thumbnail dimensions (cropped) 


} 

回答

2

喜歡的東西下面爲我工作。只需從$ meta數組中更改您想要處理的文件即可。我的水印文件位於我上傳文件夾的根目錄中。

function water_mark($meta, $id){ 

    if(!isset($meta['sizes'])) { 
     return $meta; 
    } 

    $upload_path = wp_upload_dir();  
    $path = $upload_path['basedir']; 

    //handle the different media upload directory structures 
    if(isset($path)){  
     $file = trailingslashit($upload_path['basedir'].'/').$meta['file']; 
     $water_path = trailingslashit($upload_path['basedir'].'/').'watermark.png'; 
    }else{ 
     $file = trailingslashit($upload_path['path']).$meta['file']; 
     $water_path = trailingslashit($upload_path['path']).'watermark.png'; 
    } 

    //list original image dimensions 
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file); 

    //load watermark - list its dimensions 
    $watermark = imagecreatefrompng($water_path); 
    list($wm_width, $wm_height, $wm_type) = @getimagesize($water_path); 
    //if your watermark is a transparent png uncomment below 
    //imagealphablending($watermark, 1); 

    //load fullsize image 
    $image = wp_load_image($file); 
    //if your watermark is a transparent png uncomment below 
    //imagealphablending($image, 1); 

    //greyscale image 
    imagefilter($image, IMG_FILTER_GRAYSCALE); 

    //create merged copy 
    //if your watermark is a transparent png uncomment below 
    //imagecopy($image, $watermark, $orig_w - ($wm_width + 10), $orig_h - ($wm_height + 10), 0, 0, $wm_width, $wm_height); 

    //if your watermark is a transparent png comment out below 
    imagecopymerge($image, $watermark, $orig_w - ($wm_width + 10), $orig_h - ($wm_height + 10), 0, 0, $wm_width, $wm_height, 70); 

    //save image backout 
    switch ($orig_type) { 
     case IMAGETYPE_GIF: 
      imagegif($image, $file); 
      break; 
     case IMAGETYPE_PNG: 
      imagepng($image, $file, 10); 
      break; 
     case IMAGETYPE_JPEG: 
      imagejpeg($image, $file, 95); 
      break; 
    } 

    imagedestroy($watermark); 
    imagedestroy($image); 

    //return metadata info 
    wp_update_attachment_metadata($id, $meta); 
    return $meta; 
} 


add_filter('wp_generate_attachment_metadata','water_mark', 10, 2);