使用header()
函数在请求过程中生成输出并使其崩溃。
要修改上载的图像,应使用wp_handle_upload
筛选而不是save_post
行动此外,理想情况下,您应该使用WP_Image_Editor
类,它将使用服务器中可用的最佳图像处理库(GD、Imagemagick),并触发其他插件可以使用的几个过滤器和操作:
add_filter( \'wp_handle_upload\', \'cyb_handle_upload_callback\' );
function cyb_handle_upload_callback( $data ) {
// get instance of WP_Image_Editor class
$image = wp_get_image_editor( $data[\'file\'] );
if( ! is_wp_error( $image ) ) {
// Manipulate your image here
$stamp = imagecreatefrompng( \'path/to/stamp.png\' );
$margin_right = 10;
$margeing_bottom = 10;
$sx = imagesx( $stamp );
$sy = imagesy( $stamp );
imagecopy(
$data[\'file\'],
$stamp,
imagesx( $data[\'file\'] ) - $sx - $margin_right,
imagesy( $data[\'file\'] ) - $sy - $margin_bottom,
0,
0,
imagesx( $stamp ),
imagesy( $stamp )
);
// Save the modified image
$image->save();
}
return $data;
}
如果我们遵循
this implementation extending WP_Editor_Image
class:
add_filter( \'wp_handle_upload\', \'cyb_handle_upload_callback\' );
function cyb_handle_upload_callback( $data ) {
$image = wp_get_image_editor( $data[\'file\'] );
if ( ! is_wp_error( $image ) && is_callable( [ $image, \'stamp_watermark\' ] ) ) {
$stamp = imagecreatefrompng( \'/path/to/stamp.png\' );
$success = $editor->stamp_watermark( $stamp );
if ( ! is_wp_error( $success ) ) {
$image->save();
}
}
return $data;
}