Watermarking Images with WordPress with WP_Image_Editor

If you really want to use these classes the only way would be to extend both existing implementations Wp_Image_Editor_Imagick and Wp_Image_Editor_GD.

Here’s an approach for the 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 );
    }
}

The implementation follows this example on php.net.

Now that you have your implementation you must add the new class to the stack of possible image editors using the filter 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;
} );

Now it’s likely possible to get an instance of your custom editor when calling 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();
}

You could also simply create the instance explicitly. Every other client, using wp_get_image_editor() won’t be aware of the method stamp_watermark() anyway.

Some important notes:

  • The code is not tested. It’s meant to provide a first approach on how to extend the Wp_Image_Editor_Gd class.
  • The examples show syntax that requires at least PHP 5.5
  • All tasks about loading required files are not shown. Use proper auto loading.
  • I’m not into the details of the GD API. So I’m not sure about supported mime-types, you should test this and implement the methods test() and supportet_mime_types() according to this.

Leave a Comment