static function contrastColors($string, $background) { $background = Color::StringToRgb24($background); return preg_replace_callback('/(?<!\\$)((?:\\$\\$)*)(\\$[0-9a-f][^\\$]{0,2})/iu', function ($matches) use($background) { $color = Color::StringToRgb24($matches[2]); $color = Color::Contrast($color, $background); $color = Color::Rgb24ToRgb12($color); $color = Color::Rgb12ToString($color); return $matches[1] . '$' . $color; }, $string); }
/** * Draw a string with on an image using quality method. Slow with big text size * @param string $string The style-coded string to draw * @param imageResource $image The image to draw on * @param string $fontName The TTF font to use, which has been declared previously * @param int $x X position * @param int $y Y position * @param int $size Font size * @param string $defaultColor Default text color in 3 or 6 hexadecimal characters * @param int $precision Higher value means higher precision (but also longer execution time), usual values are 1 to 3 */ static function onImageQuality($string, $image, $fontName, $x = 0, $y = 0, $size = 10, $defaultColor = '000', $precision = 2) { $defaultColor = Color::StringToRgb24($defaultColor); $factor = 1 << ($precision > 31 ? 31 : $precision); $tokens = self::parseString($string); $rawText = ''; foreach ($tokens as $token) { $rawText .= $token->text; } $maxBBox = imagettfbbox($size, 0, self::$fonts[$fontName]->getFile(), $rawText); $brush = imagecreatetruecolor($maxBBox[2] * 2, -$maxBBox[5] + 5); imagefill($brush, 0, 0, 0x7fffffff); $hugeBrush = imagecreatetruecolor(imagesx($brush) * $factor, imagesy($brush) * $factor); imagefill($hugeBrush, 0, 0, 0x7fffffff); $xOffset = 0; foreach ($tokens as $token) { $xOffset += $token->onImage($hugeBrush, $fontName, $xOffset, imagesy($hugeBrush) * 0.75, $factor * $size, $defaultColor, $factor); } $brushX = $x + imagesx($brush) / 2; $brushY = $y - imagesy($brush) * 0.25; imagecopyresampled($brush, $hugeBrush, 0, 0, 0, 0, imagesx($brush), imagesy($brush), imagesx($hugeBrush), imagesy($hugeBrush)); imagesetbrush($image, $brush); imageline($image, $brushX, $brushY, $brushX, $brushY, IMG_COLOR_BRUSHED); }