我想复制一个图像并用新名称将其转换为BW,但当我尝试用imagejpeg()
:
PHP警告:imagejpeg(/srv/www/raquelfriera.test/current/web/app/uploads/2018/02/2018/02/one-year-womens-performance-10-400x266-bw.jpg):无法打开流:没有此类文件或目录
我根据自定义大小创建图像img-400
.应用过滤器要添加的字符串替换-bw
在文件名的末尾使用imagejpeg保存它($image,$dest)所有这些都在过滤器挂钩内wp_generate_attachment_metadata
. 这样,我可以在上载或wp media regenerate
.
这是整个功能:
function bw_filter($meta) {
$path = wp_upload_dir();
/* get dirname of file to get correct date folder */
$subdir = trailingslashit(dirname($meta[\'file\']));
/* get basedir on path to get uploads folder */
$file = trailingslashit($path[\'basedir\']).$subdir.$meta[\'sizes\'][\'img-400\'][\'file\'];
$str = str_replace(".", "-bw.", $subdir.$meta[\'sizes\'][\'img-400\'][\'file\']);
$dest = trailingslashit($path[\'basedir\']).$subdir.$str;
list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
switch ($orig_type) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($file);
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_CONTRAST, -30);
imagegif( $image, $dest );
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($file);
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_CONTRAST, -30);
imagepng( $image, $dest );
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($file);
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_CONTRAST, -30);
imagejpeg( $image, $dest );
break;
}
return $meta;
}
add_filter(\'wp_generate_attachment_metadata\', __NAMESPACE__ . \'\\\\bw_filter\',10,2);
如何将图像保存到新文件中以上载目录?