public static function image_apply_color_filter($destImage, $srcImage, $hexaColor) { if (!file_exists($srcImage)) { return false; } if (!is_dir(dirname($destImage))) { sh_linker::getInstance()->helper->createDir(dirname($destImage)); } $rgbColor = sh_colors::RGBStringToRGBArray('#' . $hexaColor); $baseImage = imagecreatefrompng($srcImage); imagesavealpha($baseImage, true); imagefilter($baseImage, IMG_FILTER_COLORIZE, $rgbColor['R'], $rgbColor['G'], $rgbColor['B']); imagepng($baseImage, $destImage); imagedestroy($baseImage); return true; }
protected function createPreview($font, $height) { if (file_exists(SH_FONTS_FOLDER . $font)) { $font = SH_FONTS_FOLDER . $font; } elseif (file_exists($this->linker->site->templateFolder . 'fonts/' . $font)) { $font = $this->linker->site->templateFolder . 'fonts/' . $font; } else { die('The font ' . $font . ' was not found!'); } $text = self::PREVIEW_TEXT; $image = SH_TEMPIMAGES_FOLDER . md5(microtime()); $imageBuilder = $this->linker->imagesBuilder; list($size) = $imageBuilder->getFontSizeByTextHeight(sh_fonts::FONT_THUMB_TEXT, $font, $height); $dims = $imageBuilder->getDimensions($text, $size, $font); $imageBuilder->createImageWithBackground($image, sh_colors::RGBStringToRGBArray('FFFFFF'), $dims['width'], $dims['height']); $imageBuilder->addText($text, $image, $dims['left'], $dims['top'], $font, $size, '000000'); $contentType = mime_content_type($image); header('Content-type: ' . $contentType); readfile($image); unlink($image); return true; }
public function addText($text, $image, $x, $y, $font, $fontSize, $fontColor, $transparency = 0, $addReflect = false) { $srcImage = imagecreatefrompng($image); $width = imagesx($srcImage); $box = $this->getDimensions($text, $fontSize, $font); $textHeight = $box['height']; imagesavealpha($srcImage, true); imagealphablending($srcImage, false); $color = sh_colors::RGBStringToRGBArray($fontColor); $r = 120; $color['R']; $g = rand(0, 255); $color['G']; $b = rand(0, 255); $color['B']; $newColor = imagecolorallocatealpha($srcImage, $r, $g, $b, $transparency); if ($addReflect == true) { $reflect = imagecreatetruecolor($width, $textHeight / 2); $textColor2 = imagecolorallocatealpha($reflect, $r, $g, $b, 0); $transparentColor2 = imagecolorallocatealpha($reflect, 0, 0, 0, 127); imagefill($reflect, 0, 0, $transparentColor2); imagecolortransparent($reflect, $transparentColor2); // writes the text imagettftext($reflect, $fontSize, 0, $x, $textHeight / 2, $textColor2, $font, $text); $cpt = 0; $open = false; // Gets the pixels that are colored in the first line for ($a = 1; $a < $width + 1; $a++) { for ($b = 1; $b < $textHeight / 2 - 1; $b++) { $color = imagecolorat($reflect, $a, $b); $colorrgb = imagecolorsforindex($reflect, $color); if ($colorrgb['alpha'] < 45) { $trans = $colorrgb['alpha'] + 127 - $b * 1.5 / ($textHeight / 2) * 45; if ($trans < 127) { $tempColor = imagecolorallocatealpha($srcImage, $colorrgb['red'], $colorrgb['green'], $colorrgb['blue'], $trans); imagesetpixel($srcImage, $a, $y + $textHeight / 2 - $b, $tempColor); } } } } imageDestroy($reflect); } // writes the text imagettftext($srcImage, $fontSize, 0, $x, $y, $newColor, $font, $text); imagepng($srcImage, $image); imageDestroy($srcImage); return true; }