• Hello, I am trying to size fonts of watermark depending on image size, and since I want that plugin remains updatable, I am trying to override only apply_text_watermark method of AttachmentProcessorGD class. I put code in mu-plugin and class definition in a filter easy-watermark/available-processors. However, the method is not overriden. How to do it properly? Below is the code:

    <?php
    /*
      Plugin Name: modify easy watermark plugin
      Description: Modify text size to put on image watermark.
      Version: 1.0.0
      Author: 
      Author URI: 
      License: No license
    */
    
    namespace EasyWatermark\AttachmentProcessor;
    
    defined( 'ABSPATH' ) || exit;
    
    return;
    
    \add_filter( 'easy-watermark/available-processors', 'EasyWatermark\AttachmentProcessor\extendedProcessor' );
    
    function extendedProcessor() {
    
    		// \spl_autoload_register( function() {
    			class AttachmentProcessorGDExt extends AttachmentProcessorGD {
    				/**
    					 * Applies text watermark
    					 *
    					 * @param  Watermark $watermark Watermark object.
    					 * @return mixed
    					 */
    					private function apply_text_watermark( $watermark ) {
    
    						echo 'rabotajet';
    						return;
    
    						$output_image = $this->get_output_image();
    
    						if ( ! $output_image ) {
    							return new WP_Error( 'gd_error', __( 'Could not create output image. Please check your server configuration.', 'easy-watermark' ) );
    						}
    
    						$font = Text::get_font_path( $watermark->font );
    
    						if ( ! $font ) {
    							/* translators: font name */
    							return new WP_Error( 'font_not_found', sprintf( __( 'Font "%s" could not be found. ', 'easy-watermark' ), $watermark->font ) );
    						}
    
    						$text_size  = $this->calculate_text_size( $watermark->text_size, $watermark->text_angle, $font, $watermark->text );
    						$image_size = $this->get_input_image_size();
    
    						// correct iamge size
    
    						$color_rgb = $this->get_rgb_color( $watermark->text_color );
    						$color     = imagecolorallocatealpha(
    							$output_image,
    							$color_rgb['red'],
    							$color_rgb['green'],
    							$color_rgb['blue'],
    							127 * ( 100 - $watermark->opacity ) / 100
    						);
    
    						if ( false === $color ) {
    							return new WP_Error( 'gd_error', __( 'Something went wrong while allocating text color.', 'easy-watermark' ) );
    						}
    
    						$offset_x = $this->compute_offset( $this->get_position( $watermark->alignment, 'x' ), $watermark->offset['x'], $image_size['width'], $text_size['width'] );
    						$offset_y = $this->compute_offset( $this->get_position( $watermark->alignment, 'y' ), $watermark->offset['y'], $image_size['height'], $text_size['height'] );
    
    						$result = imagettftext(
    							$output_image,
    							$watermark->text_size,
    							$watermark->text_angle,
    							$offset_x - $text_size['delta_x'], $offset_y - $text_size['delta_y'],
    							$color,
    							$font,
    							$watermark->text
    						);
    
    						if ( false === $result ) {
    							return new WP_Error( 'gd_error', __( 'Something went wrong while applying watermark text.', 'easy-watermark' ) );
    						}
    
    						return true;
    
    					}
    				}
    		// } );
    
    	return [
    		'gd' => [
    			'label' => __( 'GD', 'easy-watermark' ),
    			'class' => 'EasyWatermark\\AttachmentProcessor\\AttachmentProcessorGDExt',
    		],
    	];
    }
Viewing 1 replies (of 1 total)
  • Plugin Author Wojtek Sza?kiewicz

    (@szaleq)

    Hi Lovro,

    The problem is that the AttachmentProcessorGD is not meant to be extended. It has some private methods which cannot be extended (the apply_text_watermark method is one of them). This easy-watermark/available-processors filter was created to allow the addition of other processors in the future, not the modification of the existing ones.
    But it is still possible to achieve what you want, you just need to extend the abstract AttachmentProcessor class and copy all the code of the AttachmentProcessorGD into your new class. Then you can make your modifications. Here is a working example that will add a ” mod” suffix to the watermark text: https://pastebin.com/GfiuPuxG
    Please note the line 496 – this is where I made a modification.

    Just be careful while doing your changes, you can easily break the plugin and it might get hard to debug.

    Hope it will work for you, good luck ??

Viewing 1 replies (of 1 total)
  • The topic ‘Overriding class AttachmentProcessorGD’ is closed to new replies.