在我用于缩略图的主题中,我希望它们是黑白的,这是我使用 Ottos function.php 脚本完成的。但是,当使用插件为彩色较大图像添加水印时,黑白拇指会变回彩色。单击黑白时,彩色照片会在 Prettyphoto 中加载。
所以我想我必须在 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)
}