Title: Overriding class AttachmentProcessorGD
Last modified: December 3, 2021

---

# Overriding class AttachmentProcessorGD

 *  Resolved [Lovro Hrust](https://wordpress.org/support/users/lovor/)
 * (@lovor)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/overriding-class-attachmentprocessorgd/)
 * 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](https://wordpress.org/support/users/szaleq/)
 * (@szaleq)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/overriding-class-attachmentprocessorgd/#post-15183841)
 * 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](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.

 * ![](https://ps.w.org/easy-watermark/assets/icon.svg?rev=2153955)
 * [Easy Watermark](https://wordpress.org/plugins/easy-watermark/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/easy-watermark/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/easy-watermark/)
 * [Active Topics](https://wordpress.org/support/plugin/easy-watermark/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/easy-watermark/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/easy-watermark/reviews/)

## Tags

 * [class](https://wordpress.org/support/topic-tag/class/)
 * [overriding](https://wordpress.org/support/topic-tag/overriding/)

 * 1 reply
 * 2 participants
 * Last reply from: [Wojtek Szałkiewicz](https://wordpress.org/support/users/szaleq/)
 * Last activity: [4 years, 5 months ago](https://wordpress.org/support/topic/overriding-class-attachmentprocessorgd/#post-15183841)
 * Status: resolved