下面是我对使用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;
}
如果您将该代码添加到主题的函数中。php文件,下载phpthumb并设置您应该可以选择的路径。我已经在本地安装的xampp上运行了它,所以希望它也能为其他人工作。phpThumb注释来自简单的演示示例。