diff --git a/code.php b/code.php index 67b13b6..750c544 100755 --- a/code.php +++ b/code.php @@ -79,6 +79,64 @@ function imagettfbbox_t( $size, $text_angle, $fontfile, $text ) { return $ret; } +/** + * Convert a hex string to red, green, blue, and alpha values between 0 - 255. + * + * Accepts a 1, 2, 3, 4, 6, and 8 digit hex code. + * + * The alpha transparency can be set with :50 at the end of the hex string. + * + * #ccc:50 would be R = 204, G = 204, B = 204, Alpha = 128 (or 50%) + * Same thing for #cccccc80 + * + * @param string $hex The hex string to convert + */ +function hex2rgba( $hex = '' ) { + $hex = strtolower( $hex ); + $hex = str_replace( '#', '', $hex ); + $hex_parts = explode( ':', $hex ); + $hex = $hex_parts[0]; + $hex_length = strlen( $hex ); + $input = $hex; + switch ($hex_length) { + case 1: + $hex = $input . $input . $input . $input . $input . $input; + break; + case 2: + $hex = $input[0] . $input[1] . $input[0] . $input[1] . $input[0] . $input[1]; + break; + case 3: + $hex = $input[0] . $input[0] . $input[1] . $input[1] . $input[2] . $input[2]; + break; + case 4: + $hex = $input[0] . $input[0] . $input[1] . $input[1] . $input[2] . $input[2] . $input[3] . $input[3]; + break; + } + // Set the alpha level to 100% for 6 character hex codes + if ( strlen( $hex ) === 6 ) { + $hex .= 'ff'; + } + $parts = str_split( $hex, 2 ); + $alpha = $parts[3]; + $alpha = hexdec( $alpha ); + $alpha_percent = round( $alpha / 255, 2 ); + if ( isset( $hex_parts[1] ) ) { + $user_supplied_alpha = $hex_parts[1]; + $user_supplied_alpha = abs( (int) $user_supplied_alpha ); + $user_supplied_alpha = min( $user_supplied_alpha, 100 ); + $alpha_percent = $user_supplied_alpha / 100; + $alpha = round( $alpha_percent * 255 ); + } + + return (object) array( + 'r' => hexdec( $parts[0] ), + 'g' => hexdec( $parts[1] ), + 'b' => hexdec( $parts[2] ), + 'alpha' => (int) $alpha, + 'alpha_percent' => $alpha_percent, + ); +} + // Get the query string from the URL. x would = 600x400 if the url was https://dummyimage.com/600x400 $x = strtolower( $_GET['x'] ); // Strip / if it's the first character @@ -242,9 +300,6 @@ function imagettfbbox_t( $size, $text_angle, $fontfile, $text ) { $width = round( $width, 3 ); $height = round( $height, 3 ); -// To easily manipulate colors between different formats -require 'color.class.php'; - // Find the background color which is always after the 2nd slash in the url $bg_color = 'ccc'; if ( ! empty( $x_pieces[1] ) ) { @@ -253,8 +308,7 @@ function imagettfbbox_t( $size, $text_angle, $fontfile, $text ) { $bg_color = $bg_color_parts[0]; } } -$background = new color(); -$background->set_hex( $bg_color ); +$background = hex2rgba( $bg_color ); // Find the foreground color which is always after the 3rd slash in the url $fg_color = '000'; @@ -264,8 +318,7 @@ function imagettfbbox_t( $size, $text_angle, $fontfile, $text ) { $fg_color = $fg_color_parts[0]; } } -$foreground = new color(); -$foreground->set_hex( $fg_color ); +$foreground = hex2rgba( $fg_color ); // This is the default text string that will go right in the middle of the rectangle // × is the multiplication sign, it is not an 'x' @@ -292,17 +345,22 @@ function imagettfbbox_t( $size, $text_angle, $fontfile, $text ) { // Create an image $img = imageCreate( $width, $height ); -$bg_color = imageColorAllocate( +$bg_alpha = 127 - ( $background->alpha >> 1); +$bg_color = imagecolorallocatealpha( $img, - $background->get_rgb( 'r' ), - $background->get_rgb( 'g' ), - $background->get_rgb( 'b' ) + $background->r, + $background->g, + $background->b, + $bg_alpha ); -$fg_color = imageColorAllocate( + +$fg_alpha = 127 - ( $foreground->alpha >> 1); +$fg_color = imagecolorallocatealpha( $img, - $foreground->get_rgb( 'r' ), - $foreground->get_rgb( 'g' ), - $foreground->get_rgb( 'b' ) + $foreground->r, + $foreground->g, + $foreground->b, + $fg_alpha ); // Ric Ewing: I modified this to behave better with long or narrow images and condensed the resize code to a single line diff --git a/color.class.php b/color.class.php deleted file mode 100755 index d95624a..0000000 --- a/color.class.php +++ /dev/null @@ -1,291 +0,0 @@ - -* @copyright Sven Wagener -* @include Funktion:_include_ -* @url http://phpclasses.ehd.com.br/browse/package/804.html -*/ -class color { - - /** - * @var array $rgb - * @access private - * @desc array for RGB colors - */ - var $rgb=array('r'=>0,'g'=>0,'b'=>0); - - /** - * @var string $hex - * @access private - * @desc variable for HTML HEX color - */ - var $hex=''; - - /** - * @var array $cmyk - * @access private - * @desc array for cmyk colors - */ - var $cmyk=array('c'=>0,'m'=>0,'y'=>0,'b'=>0); - - /** - * Sets the RGB values - * @param int $red number from 0-255 for blue color value - * @param int $green number from 0-255 for green color value - * @param int $blue number from 0-255 for blue color value - * @access public - * @desc Sets the RGB values - */ - function set_rgb($red,$green,$blue){ - - $this->rgb['r']=$red; - $this->rgb['g']=$green; - $this->rgb['b']=$blue; - - $this->convert_rgb_to_cmyk(); - $this->convert_rgb_to_hex(); - } - - /** - * Sets the HEX HTML color value - * @param string $hex 6,3,2, or 1 characters long. - * @access public - * @desc Sets the HEX HTML color value like ffff00. It will convert shorthand to a 6 digit hex. - */ - function set_hex($hex){ - //$hex = settype($hex, 'string'); - $hex = strtolower($hex); - $hex = preg_replace('/#/', '', $hex); //Strips out the # character - $hexlength = strlen($hex); - $input = $hex; - switch($hexlength) { - case 1: - $hex = $input.$input.$input.$input.$input.$input; - break; - case 2: - $hex = $input[0].$input[1].$input[0].$input[1].$input[0].$input[1]; - break; - case 3: - $hex = $input[0].$input[0].$input[1].$input[1].$input[2].$input[2]; - break; - } - $this->hex=$hex; - - $this->convert_hex_to_rgb(); - $this->convert_rgb_to_cmyk(); - } - - /** - * Sets the HTML color name, converting it to a 6 digit hex code. - * @param string $name The name of the color. - * @access public - * @desc Sets the HTML color name, converting it to a 6 digit hex code. - */ - function set_name($name){ - $this->hex = $this->convert_name_to_hex($name); - - $this->convert_hex_to_rgb(); - $this->convert_rgb_to_cmyk(); - } - - /** - * Sets the CMYK color values - * @param int $c number from 0-100 for c color value - * @param int $m number from 0-100 for m color value - * @param int $y number from 0-100 for y color value - * @param int $b number from 0-100 for b color value - * @access public - * @desc Sets the CMYK color values - */ - function set_cmyk($c,$m,$y,$b){ - $this->cmyk['c']=$c; - $this->cmyk['m']=$m; - $this->cmyk['y']=$y; - $this->cmyk['b']=$b; - - $this->convert_cmyk_to_rgb(); - $this->convert_rgb_to_hex(); - } - - /** - * Sets the pantone color value - * @param string $pantone_name name of the pantone color - * @access public - * @desc Sets the pantone color value - */ - function set_pantone($pantone_name){ - $this->pantone=$pantone_name; - $this->cmyk['c']=$this->pantone_pallete[$pantone_name]['c']; - $this->cmyk['m']=$this->pantone_pallete[$pantone_name]['m']; - $this->cmyk['y']=$this->pantone_pallete[$pantone_name]['y']; - $this->cmyk['b']=$this->pantone_pallete[$pantone_name]['b']; - - $this->convert_cmyk_to_rgb(); - $this->convert_rgb_to_hex(); - } - - /** - * Sets the pantone pc color value - * @param string $pantone_name_pc name of the pantone pc color - * @access public - * @desc Sets the pantone pc color value - */ - function set_pantone_pc($pantone_name){ - $this->pantone_pc=$pantone_name; - $this->cmyk['c']=$this->pantone_pallete_pc[$pantone_name]['c']; - $this->cmyk['m']=$this->pantone_pallete_pc[$pantone_name]['m']; - $this->cmyk['y']=$this->pantone_pallete_pc[$pantone_name]['y']; - $this->cmyk['b']=$this->pantone_pallete_pc[$pantone_name]['b']; - - $this->convert_cmyk_to_rgb(); - $this->convert_rgb_to_hex(); - } - - //include("pantone.color.class.php"); - - /** - * Returns the RGB values of a set color - * @return array $rgb color values of red ($rgb['r']), green ($rgb['green') and blue ($rgb['b']) - * @access public - * @desc Returns the RGB values of a set color - */ - function get_rgb($val){ - if($val) { - return $this->rgb[$val]; - } else { - return $this->rgb; - } - } - - /** - * Returns the HEX HTML color value of a set color - * @return string $hex HEX HTML color value - * @access public - * @desc Returns the HEX HTML color value of a set color - */ - function get_hex(){ - return $this->hex; - } - - /** - * Returns the CMYK values of a set color - * @return array $cmyk color values of c ($cmyk['c']), m ($cmyk['m'), y ($cmyk['blue']) and b ($cmyk['b']) - * @access public - * @desc Returns the CMYK values of a set color - */ - function get_cmyk(){ - return $this->cmyk; - } - - /** - * Converts the RGB colors to HEX HTML colors - * @access private - * @desc Converts the RGB colors to HEX HTML colors - */ - function convert_rgb_to_hex(){ - $this->hex=$this->hex_trip[$this->rgb['r']].$this->hex_trip[$this->rgb['g']].$this->hex_trip[$this->rgb['b']]; - } - - /** - * Converts the RGB colors to CMYK colors - * @access private - * @desc Converts the RGB colors to CMYK colors - */ - function convert_rgb_to_cmyk(){ - $c = (255-$this->rgb['r'] )/255.0*100; - $m = (255-$this->rgb['g'] )/255.0*100; - $y = (255-$this->rgb['b'] )/255.0*100; - - $b = min(array($c,$m,$y)); - $c=$c-$b; - $m=$m-$b; - $y=$y-$b; - - $this->cmyk = array( 'c' => $c, 'm' => $m, 'y' => $y, 'b' => $b); - } - - /** - * Converts the CMYK colors to RGB colors - * @access private - * @desc Converts the CMYK colors to RGB colors - */ - function convert_cmyk_to_rgb(){ - $red=$this->cmyk['c']+$this->cmyk['b']; - $green=$this->cmyk['m']+$this->cmyk['b']; - $blue=$this->cmyk['y']+$this->cmyk['b']; - - $red=($red-100)*(-1); - $green=($green-100)*(-1); - $blue=($blue-100)*(-1); - - $red=round($red/100*255,0); - $green=round($green/100*255,0); - $blue=round($blue/100*255,0); - - $this->rgb['r']=$red; - $this->rgb['g']=$green; - $this->rgb['b']=$blue; - } - - /** - * Converts the HTML HEX colors to RGB colors - * @access private - * @desc Converts the HTML HEX colors to RGB colors - * @url http://css-tricks.com/snippets/php/convert-hex-to-rgb/ - */ - function convert_hex_to_rgb(){ - $red = substr($this->hex,0,2); - $green = substr($this->hex,2,2); - $blue = substr($this->hex,4,2); - $this->rgb['r'] = hexdec( $red ); - $this->rgb['g'] = hexdec( $green ); - $this->rgb['b'] = hexdec( $blue ); - } - - /** - * Converts HTML color name to 6 digit HEX value. - * @access private - * @param string $name One of the offical HTML color names. - * @desc Converts HTML color name to 6 digit HEX value. - * @url http://en.wikipedia.org/wiki/HTML_color_names - */ - function convert_name_to_hex($name){ - $color_names = array( - 'aqua' => '00ffff', - 'cyan' => '00ffff', - 'gray' => '808080', - 'grey' => '808080', - 'navy' => '000080', - 'silver' => 'C0C0C0', - 'black' => '000000', - 'green' => '008000', - 'olive' => '808000', - 'teal' => '008080', - 'blue' => '0000FF', - 'lime' => '00FF00', - 'purple' => '800080', - 'white' => 'ffffff', - 'fuchsia' => 'FF00FF', - 'magenta' => 'FF00FF', - 'maroon' => '800000', - 'red' => 'FF0000', - 'yellow' => 'FFFF00' - ); - if (array_key_exists($name, $color_names)) { - return $color_names[$name]; - } - else { - //error - } - } -} -?> \ No newline at end of file