我真的很喜欢WP_Image_Editor
班我想将其用于我正在制作的一些自定义功能,但我想知道-水印图像-有没有一种方法可以使用WP_Image_Editor
要将图像与水印合并?我想不出任何明显的办法。
使用WP_Image_Editor使用WordPress为图像添加水印
2 个回复
SO网友:David
如果您真的想使用这些类,唯一的方法就是扩展两个现有的实现Wp_Image_Editor_Imagick
和Wp_Image_Editor_GD
.
以下是Wp_Image_Editor_GD
:
namespace WPSE98156;
use
Wp_Image_Editor_Gd,
Wp_Error;
class WatermarkImageEditor extends Wp_Image_Editor_Gd {
/*
* @param resource $stamp (A GD image resource)
*
* @return bool|WP_Error
*/
public function stamp_watermark( $stamp ) {
/**
* The resource of the image to edit is stored in
* $this->image, after $this->load() was called
*/
$loaded = $this->load();
if ( is_wp_error( $loaded ) )
return $loaded;
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx( $stamp );
$sy = imagesy( $stamp );
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy(
$this->image,
$stamp,
imagesx( $this->image ) - $sx - $marge_right,
imagesy( $this->image ) - $sy - $marge_bottom,
0,
0,
imagesx( $stamp ),
imagesy( $stamp )
);
}
/**
* @param array $args
*
* @return bool
*/
public static function test( $args = [] ) {
/**
* Maybe implement your own test here, whether the environment
* is able to deal with your implementation of the
* stamp_watermark() method
*/
return parent::test( $args );
}
/**
* @param string $mime_type
*
* @return bool
*/
public static function supports_mime_type( $mime_type ) {
/**
* Todo: Check here if the implementation of the method stamp_watermark()
* can deal with the mime-types image/png, image/jpeg and image/gif
*/
return parent::supports_mime_type( $mime_type );
}
}
实施如下this example on php.net.现在已经有了实现,必须使用过滤器将新类添加到可能的图像编辑器堆栈中wp_image_editors
:
namespace WPSE98156;
add_filter( \'wp_image_editors\', function( $editors ) {
if ( ! is_array( $editors ) )
return $editors; //someone broke the filtered value
array_unshift( $editors, WatermarkImageEditor::class );
return $editors;
} );
现在,在调用wp_get_image_editor()
: $editor = wp_get_image_editor( \'/path/to/image.jpeg\' );
if ( ! is_wp_error( $editor ) && is_callable( [ $editor, \'stamp_watermark\' ] ) && ! is_wp_error( $loaded ) ) {
$stamp = imagecreatefrompng( \'/path/to/watermark.png\' );
$success = $editor->stamp_watermark( $stamp );
if ( ! is_wp_error( $success ) )
$editor->save();
}
您也可以简单地显式创建实例。每个其他客户端,使用wp_get_image_editor()
不会知道该方法stamp_watermark()
无论如何Some important notes:
代码未经测试。它的目的是提供第一种方法来扩展Wp_Image_Editor_Gd
类别test() 和supportet_mime_types()
根据这一点
SO网友:CarlosBC
我在实现David代码时遇到了很多问题。我有一些错误,比如“WP\\u IMAGE\\u EDITOR\\u GD”找不到,“WatermarkImageEditor”找不到……当我让它工作时,它不能与alpha通道png一起工作,所以我花了很多时间让它与它们一起工作。所以我会解释我是怎么得到它的。
将所有这些代码放在一个php文件中,我的称为水印。php。
<?php
class WatermarkImageEditor extends WP_Image_Editor_GD {
public function stamp_watermark( $stampPath, $marginH=0, $marginV=0 ) {
$loaded = $this->load();
if ( is_wp_error( $loaded ) ) return $loaded;
$stamp=imagecreatefrompng( $stampPath );
if(is_wp_error($stamp)){ return $stamp; }
imagealphablending($stamp, true);
$sx = imagesx( $stamp );
$sy = imagesy( $stamp );
imagealphablending($this->image, true);
imagecopy(
$this->image, $stamp,$marginH,$this->size[\'height\']-$sy-$marginV,0,0,$sx, $sy
);
}
public static function test( $args = [] ) { return parent::test( $args ); }
public static function supports_mime_type( $mime_type ) { return parent::supports_mime_type( $mime_type ); }
}
现在,我们需要注册过滤器。我在自己的插件中使用它,所以我在主插件文件中有此代码,但您也可以将其放在其他地方,如函数中。php。请注意,我正在使用require\\u once加载水印。php,所以水印。php必须位于同一文件夹中。add_filter( \'wp_image_editors\', function( $editors ) {
require_once __DIR__. \'/watermark.php\';
if ( ! is_array( $editors ) )
return $editors; //someone broke the filtered value
array_unshift( $editors, "WatermarkImageEditor");
return $editors;
} );
最后一步,调用stamp\\u watermark()。在这个示例中,我从磁盘加载一个图像,调整其大小,放置水印,然后保存它。请注意,stamp\\u watermark()在第一个参数上接收水印的路径或url,其他两个参数是可选边距。$editor= wp_get_image_editor($imagePath);
$editor->resize(1920, 1080, TRUE);
if(is_callable([$editor,\'stamp_watermark\'])){
$success = $editor->stamp_watermark( ABSPATH.\'wp-content/uploads/watermark-full.png\', 20, 20 );
if(!is_wp_error($success)){ $editor->save($imagePath); }
}
结束