File indexing completed on 2024-12-29 05:27:25

0001 <?php
0002 
0003 namespace YoHang88\LetterAvatar;
0004 
0005 use Intervention\Image\Gd\Font;
0006 use Intervention\Image\Gd\Shapes\CircleShape;
0007 use Intervention\Image\ImageManager;
0008 
0009 class LetterAvatar
0010 {
0011     /**
0012      * Image Type PNG
0013      */
0014     const MIME_TYPE_PNG = 'image/png';
0015 
0016     /**
0017      * Image Type JPEG
0018      */
0019     const MIME_TYPE_JPEG = 'image/jpeg';
0020 
0021     /**
0022      * @var string
0023      */
0024     private $name;
0025 
0026 
0027     /**
0028      * @var string
0029      */
0030     private $nameInitials;
0031 
0032 
0033     /**
0034      * @var string
0035      */
0036     private $shape;
0037 
0038 
0039     /**
0040      * @var int
0041      */
0042     private $size;
0043 
0044     /**
0045      * @var ImageManager
0046      */
0047     private $imageManager;
0048 
0049     /**
0050      * LetterAvatar constructor.
0051      * @param string $name
0052      * @param string $shape
0053      * @param int    $size
0054      */
0055     public function __construct( $name,  $shape ,  $size )
0056     {
0057         $this->setName($name);
0058         $this->setImageManager(new ImageManager());
0059         $this->setShape($shape);
0060         $this->setSize($size);
0061     }
0062 
0063     /**
0064      * @param string $name
0065      */
0066     private function setName( $name)
0067     {
0068         $this->name = $name;
0069     }
0070 
0071 
0072     /**
0073      * @param ImageManager $imageManager
0074      */
0075     private function setImageManager( $imageManager)
0076     {
0077         $this->imageManager = $imageManager;
0078     }
0079 
0080     /**
0081      * @param string $shape
0082      */
0083     private function setShape( $shape)
0084     {
0085         $this->shape = $shape;
0086     }
0087 
0088 
0089     /**
0090      * @param int $size
0091      */
0092     private function setSize( $size)
0093     {
0094         $this->size = $size;
0095     }
0096 
0097 
0098     /**
0099      * @return \Intervention\Image\Image
0100      */
0101     private function generate()
0102     //private function generate(): \Intervention\Image\Image
0103     {
0104         $isCircle = $this->shape === 'circle';
0105 
0106         $this->nameInitials = $this->getInitials($this->name);
0107         $color = $this->stringToColor($this->name);
0108 
0109         $canvas = $this->imageManager->canvas(480, 480, $isCircle ? null : $color);
0110 
0111         if ($isCircle) {
0112             $canvas->circle(480, 240, 240, function (CircleShape $draw) use ($color) {
0113                 $draw->background($color);
0114             });
0115 
0116         }
0117 
0118         $canvas->text($this->nameInitials, 240, 240, function (Font $font) {
0119             $font->file(__DIR__ . '/fonts/arial-bold.ttf');
0120             $font->size(220);
0121             $font->color('#fafafa');
0122             $font->valign('middle');
0123             $font->align('center');
0124         });
0125 
0126         return $canvas->resize($this->size, $this->size);
0127     }
0128 
0129     /**
0130      * @param string $name
0131      * @return string
0132      */
0133     private function getInitials( $name)
0134     //private function getInitials(string $name): string
0135     {
0136         $nameParts = $this->break_name($name);
0137 
0138         if(!$nameParts) {
0139             return '';
0140         }
0141 
0142         $secondLetter = $nameParts[1] ? $this->getFirstLetter($nameParts[1]) : '';
0143 
0144         return $this->getFirstLetter($nameParts[0]) . $secondLetter;
0145 
0146     }
0147 
0148     /**
0149      * @param string $word
0150      * @return string
0151      */
0152     //private function getFirstLetter_(string $word): string
0153     private function getFirstLetter_( $word)
0154     {
0155         return mb_strtoupper(trim(mb_substr($word, 0, 1, 'UTF-8')));
0156     }
0157 
0158     /**
0159      * @param string $word
0160      * @return string
0161      */
0162     // private function getFirstLetter(string $word): string
0163     private function getFirstLetter( $word)   
0164     {
0165         return trim(mb_substr($word, 0, 1, 'UTF-8'));
0166     }
0167 
0168     /**
0169      * Save the generated Letter-Avatar as a file
0170      *
0171      * @param        $path
0172      * @param string $mimetype
0173      * @param int    $quality
0174      * @return bool
0175      */
0176     // public function saveAs($path, $mimetype = 'image/png', $quality = 90): bool
0177     public function saveAs($path, $mimetype = 'image/png', $quality = 90)
0178     {
0179         $allowedMimeTypes = [
0180             'image/png',
0181             'image/jpeg'
0182         ];
0183 
0184         if (empty($path) || empty($mimetype) || !\in_array($mimetype, $allowedMimeTypes, true)) {
0185             return false;
0186         }
0187 
0188         return \is_int(@file_put_contents($path, $this->generate()->encode($mimetype, $quality)));
0189     }
0190 
0191     /**
0192      * @return string
0193      */
0194     // public function __toString(): string
0195     public function __toString()
0196     {
0197         return (string)$this->generate()->encode('data-url');
0198     }
0199 
0200     /**
0201      * Explodes Name into an array.
0202      * The function will check if a part is , or blank
0203      *
0204      * @param string $name Name to be broken up
0205      * @return array Name broken up to an array
0206      */
0207     // private function break_name(string $name): array
0208     private function break_name( $name)
0209     {
0210         $words = \explode(' ', $name);
0211         $words = array_filter($words, function($word) {
0212             return $word!=='' && $word !== ',';
0213         });
0214         return array_values($words);
0215     }
0216 
0217     /**
0218      * @param string $string
0219      * @return string
0220      */
0221     // private function stringToColor(string $string): string
0222     private function stringToColor( $string)
0223     {
0224         // random color
0225         $rgb = substr(dechex(crc32($string)), 0, 6);
0226         // make it darker
0227         // $darker = 1;
0228         // list($R16, $G16, $B16) = str_split($rgb, 2);
0229         // $R = sprintf('%02X', floor(hexdec($R16) / $darker));        
0230         // $G = sprintf('%02X', floor(hexdec($G16) / $darker));
0231         // $B = sprintf('%02X', floor(hexdec($B16) / $darker));
0232         // return '#' . $R . $G . $B;
0233 
0234         // ======== nr3 ==============
0235         $darker = 2;
0236         list($R16, $G16, $B16) = str_split($rgb, 2);
0237         $R = sprintf('%02X', floor(hexdec($R16)));
0238         $G = sprintf('%02X', floor(hexdec($G16) / $darker));
0239         $B = sprintf('%02X', floor(hexdec($B16) / $darker));
0240         return '#' . $R . $G . $B;
0241 
0242         // // ========nr1================
0243         // $darker = 2;
0244         // list($R16, $G16, $B16) = str_split($rgb, 2);
0245         // $R = sprintf('%02X', floor(hexdec($R16) / $darker));
0246         // $G = sprintf('%02X', floor(hexdec($G16) / $darker));
0247         // $B = sprintf('%02X', floor(hexdec($B16) / $darker));
0248         // return '#' . $R . $G . $B;
0249     }
0250 
0251 }