Elementor Code Reference
  • Developers
  • Code Reference
  • Docs
  • Blog
  • Account
  • Developers
  • Code Reference
  • Docs
  • Blog
  • Account

BFI_Image_Editor_GD_1_3

BFI_Image_Editor_GD_1_3()


Methods

  • _opacity — BFI_Image_Editor_GD_1_3::_opacity( $image,  $opacity ) Source includes/libraries/bfi-thumb/bfi-thumb.php protected function _opacity( $image, $opacity ) { if ( ! function_exists( 'imagealphablending' ) || ! function_exists( 'imagecolorat' ) || ! function_exists( 'imagecolorallocatealpha' ) || ! function_exists( 'imagesetpixel' ) ) { return false; } // get image width and height $w = imagesx( $image ); $h = imagesy( $image ); // turn […]
  • _opacity — BFI_Image_Editor_GD_1_3::_opacity( $image,  $opacity ) Source includes/libraries/bfi-thumb/bfi-thumb.php protected function _opacity( $image, $opacity ) { if ( ! function_exists( 'imagealphablending' ) || ! function_exists( 'imagecolorat' ) || ! function_exists( 'imagecolorallocatealpha' ) || ! function_exists( 'imagesetpixel' ) ) { return false; } // get image width and height $w = imagesx( $image ); $h = imagesy( $image ); // turn […]
  • _opacity — BFI_Image_Editor_GD_1_3::_opacity( $image,  $opacity ) Source includes/libraries/bfi-thumb/bfi-thumb.php protected function _opacity( $image, $opacity ) { if ( ! function_exists( 'imagealphablending' ) || ! function_exists( 'imagecolorat' ) || ! function_exists( 'imagecolorallocatealpha' ) || ! function_exists( 'imagesetpixel' ) ) { return false; } // get image width and height $w = imagesx( $image ); $h = imagesy( $image ); // turn […]
  • colorize — Tints the image a different color
  • colorize — Tints the image a different color
  • colorize — Tints the image a different color
  • grayscale — Makes the image grayscale
  • grayscale — Makes the image grayscale
  • grayscale — Makes the image grayscale
  • negate — Negates the image

Source

includes/libraries/bfi-thumb/bfi-thumb.php

	class BFI_Image_Editor_GD_1_3 extends WP_Image_Editor_GD {

		/** Rotates current image counter-clockwise by $angle.
		 * Ported from image-edit.php
		 * Added presevation of alpha channels
		 *
		 * @since  3.5.0
		 * @access public
		 *
		 * @param float $angle
		 *
		 * @return boolean|WP_Error
		 */
		public function rotate( $angle ) {
			if ( function_exists( 'imagerotate' ) ) {
				$rotated = imagerotate( $this->image, $angle, 0 );

				// Add alpha blending
				imagealphablending( $rotated, true );
				imagesavealpha( $rotated, true );

				if ( is_resource( $rotated ) ) {
					imagedestroy( $this->image );
					$this->image = $rotated;
					$this->update_size();

					return true;
				}
			}

			return new WP_Error( 'image_rotate_error', 'Image rotate failed.', $this->file );
		}

		/** Changes the opacity of the image
		 *
		 * @supports 3.5.1
		 * @access   public
		 *
		 * @param float $opacity (0.0-1.0)
		 *
		 * @return boolean|WP_Error
		 */
		public function opacity( $opacity ) {
			$opacity /= 100;

			$filtered = $this->_opacity( $this->image, $opacity );

			if ( is_resource( $filtered ) ) {
				// imagedestroy($this->image);
				$this->image = $filtered;

				return true;
			}

			return new WP_Error( 'image_opacity_error', 'Image opacity change failed.', $this->file );
		}


		// from: http://php.net/manual/en/function.imagefilter.php
		// params: image resource id, opacity (eg. 0.0-1.0)
		protected function _opacity( $image, $opacity ) {
			if ( ! function_exists( 'imagealphablending' ) || ! function_exists( 'imagecolorat' ) || ! function_exists( 'imagecolorallocatealpha' ) || ! function_exists( 'imagesetpixel' ) ) {
				return false;
			}

			// get image width and height
			$w = imagesx( $image );
			$h = imagesy( $image );

			// turn alpha blending off
			imagealphablending( $image, false );

			// find the most opaque pixel in the image (the one with the smallest alpha value)
			$minalpha = 127;
			for ( $x = 0; $x < $w; $x++ ) {
				for ( $y = 0; $y < $h; $y++ ) {
					$alpha = ( imagecolorat( $image, $x, $y ) >> 24 ) & 0xFF;
					if ( $alpha < $minalpha ) {
						$minalpha = $alpha;
					}
				}
			}

			// loop through image pixels and modify alpha for each
			for ( $x = 0; $x < $w; $x++ ) {
				for ( $y = 0; $y < $h; $y++ ) {

					// get current alpha value (represents the TANSPARENCY!)
					$colorxy = imagecolorat( $image, $x, $y );
					$alpha = ( $colorxy >> 24 ) & 0xFF;

					// calculate new alpha
					if ( $minalpha !== 127 ) {
						$alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha );
					} else {
						$alpha += 127 * $opacity;
					}

					// get the color index with new alpha
					$alphacolorxy = imagecolorallocatealpha( $image, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );

					// set pixel with the new color + opacity
					if ( ! imagesetpixel( $image, $x, $y, $alphacolorxy ) ) {
						return false;
					}
				}
			}

			imagesavealpha( $image, true );

			return $image;
		}

		/** Tints the image a different color
		 *
		 * @supports 3.5.1
		 * @access   public
		 *
		 * @param string hex color e.g. #ff00ff
		 *
		 * @return boolean|WP_Error
		 */
		public function colorize( $hexColor ) {
			if ( function_exists( 'imagefilter' ) && function_exists( 'imagesavealpha' ) && function_exists( 'imagealphablending' ) ) {

				$hexColor = preg_replace( '#^\##', '', $hexColor );
				$r = hexdec( substr( $hexColor, 0, 2 ) );
				$g = hexdec( substr( $hexColor, 2, 2 ) );
				$b = hexdec( substr( $hexColor, 2, 2 ) );

				imagealphablending( $this->image, false );
				if ( imagefilter( $this->image, IMG_FILTER_COLORIZE, $r, $g, $b, 0 ) ) {
					imagesavealpha( $this->image, true );

					return true;
				}
			}

			return new WP_Error( 'image_colorize_error', 'Image color change failed.', $this->file );
		}

		/** Makes the image grayscale
		 *
		 * @supports 3.5.1
		 * @access   public
		 *
		 * @return boolean|WP_Error
		 */
		public function grayscale() {
			if ( function_exists( 'imagefilter' ) ) {
				if ( imagefilter( $this->image, IMG_FILTER_GRAYSCALE ) ) {
					return true;
				}
			}

			return new WP_Error( 'image_grayscale_error', 'Image grayscale failed.', $this->file );
		}

		/** Negates the image
		 *
		 * @supports 3.5.1
		 * @access   public
		 *
		 * @return boolean|WP_Error
		 */
		public function negate() {
			if ( function_exists( 'imagefilter' ) ) {
				if ( imagefilter( $this->image, IMG_FILTER_NEGATE ) ) {
					return true;
				}
			}

			return new WP_Error( 'image_negate_error', 'Image negate failed.', $this->file );
		}
	}
Elementor Code Reference
  • Code Reference
  • Elementor Hooks
    • PHP Hooks
    • JS Hooks
  • Elementor Classes
  • Elementor Methods
  • Elementor Functions
Developer Resources

Visit the official Developer Resources to learn how to extend Elementor.

Get Involved

Visit Elementor GitHub repository to contribute code or suggest new ideas.

  • Contact
  • About Us
  • Terms of Service
  • Privacy Policy
  • Template Library
  • Features
  • Theme Builder
  • Get Pro
Theme by Pojo.me
//Made in Elementor Page Builder
Enter your email and be the first to learn about new updates and features.
No thanks, I just want to download Elementor