这是我使用一种wordpress图像过滤器的尝试,我尝试使用Chip Bennett建议的一种,但没有成功。
我要做的是创建一个自定义大小,然后检查创建的每个图像(如果是该特定大小),然后应用phpthumb过滤器。理想情况下,我希望能够仅检查自定义图像尺寸的名称,但是我还没有弄清楚该怎么做。
add_theme_support( 'post-thumbnails' );
add_image_size( 'rounded-saturated', 250, 100, true );
require_once('path_to\phpthumb.class.php');
add_filter('image_make_intermediate_size', 'paul_rounded_filter');
function paul_rounded_filter($file) {
$info = getimagesize($file);
// check for our image size and do stuff
if($info[0] == 250 && $info[1] == 100)
{
// create phpThumb object
$phpThumb = new phpThumb();
$phpThumb->resetObject();
// set data source -- do this first, any settings must be made AFTER this call
$phpThumb->setSourceData(file_get_contents($file));
$output_filename = $file;
// PLEASE NOTE:
// You must set any relevant config settings here. The phpThumb
// object mode does NOT pull any settings from phpThumb.config.php
//$phpThumb->setParameter('config_document_root', '/home/groups/p/ph/phpthumb/htdocs/');
//$phpThumb->setParameter('config_cache_directory', '/tmp/persistent/phpthumb/cache/');
// set parameters (see "URL Parameters" in phpthumb.readme.txt)
$phpThumb->setParameter('fltr', 'ric|30|30');
$phpThumb->setParameter('fltr', 'sat|-100');
// generate & output thumbnail
if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it!
if ($phpThumb->RenderToFile($output_filename)) {
// do something on success
echo 'Successfully rendered to "'.$output_filename.'"';
//die;
} else {
// do something with debug/error messages
echo 'Failed:<pre>'.implode("\n\n", $phpThumb->debugmessages).'</pre>';
die;
}
} else {
// do something with debug/error messages
echo 'Failed:<pre>'.$phpThumb->fatalerror."\n\n".implode("\n\n", $phpThumb->debugmessages).'</pre>';
die;
}
}
if ( $width || $height ) {
if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) {
$resized_file = apply_filters('image_make_intermediate_size', $resized_file);
return array(
'file' => wp_basename( $resized_file ),
'width' => $info[0],
'height' => $info[1],
);
}
}
return false;
}
如果您将该代码添加到主题的functions.php文件中,请下载phpthumb并设置路径。我已经在本地安装的xampp上使用了它,因此希望它也应该对其他人也有用。phpThumb注释来自简单的演示示例。