[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2005 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE 8 */ 9 10 namespace Joomla\CMS\Image\Filter; 11 12 use Joomla\CMS\Image\ImageFilter; 13 14 // phpcs:disable PSR1.Files.SideEffects 15 \defined('JPATH_PLATFORM') or die; 16 // phpcs:enable PSR1.Files.SideEffects 17 18 /** 19 * Image Filter class fill background with color; 20 * 21 * @since 3.4 22 */ 23 class Backgroundfill extends ImageFilter 24 { 25 /** 26 * Method to apply a background color to an image resource. 27 * 28 * @param array $options An array of options for the filter. 29 * color Background matte color 30 * 31 * @return void 32 * 33 * @since 3.4 34 * @throws \InvalidArgumentException 35 */ 36 public function execute(array $options = []) 37 { 38 // Validate that the color value exists and is an integer. 39 if (!isset($options['color'])) { 40 throw new \InvalidArgumentException('No color value was given. Expected string or array.'); 41 } 42 43 $colorCode = $options['color'] ?? null; 44 45 // Get resource dimensions 46 $width = imagesx($this->handle); 47 $height = imagesy($this->handle); 48 49 // Sanitize color 50 $rgba = $this->sanitizeColor($colorCode); 51 52 // Enforce alpha on source image 53 if (imageistruecolor($this->handle)) { 54 imagealphablending($this->handle, false); 55 imagesavealpha($this->handle, true); 56 } 57 58 // Create background 59 $bg = imagecreatetruecolor($width, $height); 60 imagesavealpha($bg, empty($rgba['alpha'])); 61 62 // Allocate background color. 63 $color = imagecolorallocatealpha($bg, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']); 64 65 // Fill background 66 imagefill($bg, 0, 0, $color); 67 68 // Apply image over background 69 imagecopy($bg, $this->handle, 0, 0, 0, 0, $width, $height); 70 71 // Move flattened result onto current handle. 72 // If handle was palette-based, it'll stay like that. 73 imagecopy($this->handle, $bg, 0, 0, 0, 0, $width, $height); 74 75 // Free up memory 76 imagedestroy($bg); 77 } 78 79 /** 80 * Method to sanitize color values and/or convert to an array 81 * 82 * @param mixed $input Associative array of colors and alpha, or hex RGBA string when alpha FF is opaque. Defaults to black and opaque alpha. 83 * 84 * @return array Associative array of red, green, blue and alpha 85 * 86 * @since 3.4 87 * 88 * @note '#FF0000FF' returns an array with alpha of 0 (opaque) 89 */ 90 protected function sanitizeColor($input) 91 { 92 // Construct default values 93 $colors = ['red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0]; 94 95 // Make sure all values are in 96 if (\is_array($input)) { 97 $colors = array_merge($colors, $input); 98 } elseif (\is_string($input)) { 99 // Convert RGBA 6-9 char string 100 $hex = ltrim($input, '#'); 101 102 $hexValues = [ 103 'red' => substr($hex, 0, 2), 104 'green' => substr($hex, 2, 2), 105 'blue' => substr($hex, 4, 2), 106 'alpha' => substr($hex, 6, 2), 107 ]; 108 109 $colors = array_map('hexdec', $hexValues); 110 111 // Convert Alpha to 0..127 when provided 112 if (\strlen($hex) > 6) { 113 $colors['alpha'] = floor((255 - $colors['alpha']) / 2); 114 } 115 } else { 116 // Cannot sanitize such type 117 return $colors; 118 } 119 120 // Make sure each value is within the allowed range 121 foreach ($colors as &$value) { 122 $value = max(0, min(255, (int) $value)); 123 } 124 125 $colors['alpha'] = min(127, $colors['alpha']); 126 127 return $colors; 128 } 129 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |