Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code improvements and upgraded to PHP 7.0 #14

Merged
merged 14 commits into from
Jun 23, 2018
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
}
],
"require": {
"php" : ">=5.5.0",
"php" : ">=7.0.0",
"ext-mbstring" : "*",
"intervention/image": "^2.3"
},
"require-dev": {
"phpunit/phpunit" : "4.*",
"scrutinizer/ocular": "~1.1"
"roave/security-advisories": "dev-master",
"phpunit/phpunit" : "4.*"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 21 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
require_once './../vendor/autoload.php';

use YoHang88\LetterAvatar\LetterAvatar;

$avatar = new LetterAvatar('Max Mustermann', 'circle', 64);
?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Example</title>
</head>
<body>
<img src="<?php echo $avatar;?>" alt="">
</body>
</html>
193 changes: 109 additions & 84 deletions src/LetterAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,94 @@

namespace YoHang88\LetterAvatar;

use Intervention\Image\Gd\Font;
use Intervention\Image\Gd\Shapes\CircleShape;
use Intervention\Image\ImageManager;

class LetterAvatar
{
/**
* Image Type PNG
*/
const MIME_TYPE_PNG = 'image/png';

/**
* Image Type JPEG
*/
const MIME_TYPE_JPEG = 'image/jpeg';

/**
* @var string
*/
protected $name;
private $name;


/**
* @var string
*/
protected $name_initials;
private $nameInitials;


/**
* @var string
*/
protected $shape;
private $shape;


/**
* @var int
*/
protected $size;
private $size;

/**
* @var ImageManager
*/
protected $image_manager;

private $imageManager;

public function __construct($name, $shape = 'circle', $size = '48')
/**
* LetterAvatar constructor.
* @param string $name
* @param string $shape
* @param int $size
*/
public function __construct(string $name, string $shape = 'circle', int $size = 48)
{
$this->setName($name);
$this->setImageManager(new ImageManager());
$this->setShape($shape);
$this->setSize($size);
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @param string $name
*/
public function setName($name)
private function setName(string $name)
{
$this->name = $name;
}

/**
* @return ImageManager
*/
public function getImageManager()
{
return $this->image_manager;
}

/**
* @param ImageManager $image_manager
*/
public function setImageManager(ImageManager $image_manager)
{
$this->image_manager = $image_manager;
}

/**
* @return string
* @param ImageManager $imageManager
*/
public function getShape()
private function setImageManager(ImageManager $imageManager)
{
return $this->shape;
$this->imageManager = $imageManager;
}

/**
* @param string $shape
*/
public function setShape($shape)
private function setShape(string $shape)
{
$this->shape = $shape;
}

/**
* @return int
*/
public function getSize()
{
return $this->size;
}

/**
* @param int $size
*/
public function setSize($size)
private function setSize(int $size)
{
$this->size = $size;
}
Expand All @@ -111,83 +98,121 @@ public function setSize($size)
/**
* @return \Intervention\Image\Image
*/
public function generate()
private function generate(): \Intervention\Image\Image
{
$words = $this->break_words($this->name);

$number_of_word = 1;
$this->name_initials = '';
foreach ($words as $word) {

if ($number_of_word > 2)
break;

$this->name_initials .= mb_strtoupper(trim(mb_substr($word, 0, 1, 'UTF-8')));

$number_of_word++;
}
$isCircle = $this->shape === 'circle';

$this->nameInitials = $this->getInitials($this->name);
$color = $this->stringToColor($this->name);

if ($this->shape == 'circle') {
$canvas = $this->image_manager->canvas(480, 480);
$canvas = $this->imageManager->canvas(480, 480, $isCircle ? null : $color);

$canvas->circle(480, 240, 240, function ($draw) use ($color) {
if ($isCircle) {
$canvas->circle(480, 240, 240, function (CircleShape $draw) use ($color) {
$draw->background($color);
});

} else {

$canvas = $this->image_manager->canvas(480, 480, $color);
}

$canvas->text($this->name_initials, 240, 240, function ($font) {
$canvas->text($this->nameInitials, 240, 240, function (Font $font) {
$font->file(__DIR__ . '/fonts/arial-bold.ttf');
$font->size(220);
$font->color('#ffffff');
$font->color('#fafafa');
$font->valign('middle');
$font->align('center');
});

return $canvas->resize($this->size, $this->size);
}

public function saveAs($path, $mimetype = 'image/png', $quality = 90)
/**
* @param string $name
* @return string
*/
private function getInitials(string $name): string
{
if(empty($path) || empty($mimetype) || $mimetype != "image/png" && $mimetype != 'image/jpeg'){
return false;
$nameParts = $this->break_name($name);

if(!$nameParts) {
return '';
}

return @file_put_contents($path, $this->generate()->encode($mimetype, $quality));
$secondLetter = $nameParts[1] ? $this->getFirstLetter($nameParts[1]) : '';

return $this->getFirstLetter($nameParts[0]) . $secondLetter;

}

public function __toString()
/**
* @param string $word
* @return string
*/
private function getFirstLetter(string $word): string
{
return (string) $this->generate()->encode('data-url');
return mb_strtoupper(trim(mb_substr($word, 0, 1, 'UTF-8')));
}

public function break_words($name) {
$temp_word_arr = explode(' ', $name);
$final_word_arr = array();
foreach ($temp_word_arr as $key => $word) {
if( $word != "" && $word != ",") {
$final_word_arr[] = $word;
}
/**
* Save the generated Letter-Avatar as a file
*
* @param $path
* @param string $mimetype
* @param int $quality
* @return bool
*/
public function saveAs($path, $mimetype = 'image/png', $quality = 90): bool
{
$allowedMimeTypes = [
'image/png',
'image/jpeg'
];

if (empty($path) || empty($mimetype) || \in_array($mimetype, $allowedMimeTypes, true)) {
return false;
}
return $final_word_arr;

return \is_int(@file_put_contents($path, $this->generate()->encode($mimetype, $quality)));
}

protected function stringToColor($string)
/**
* @return string
*/
public function __toString(): string
{
return (string)$this->generate()->encode('data-url');
}

/**
* Explodes Name into an array.
* The function will check if a part is , or blank
*
* @param string $name Name to be broken up
* @return array Name broken up to an array
*/
private function break_name(string $name): array
{
$words = \explode(' ', $name);
$words = array_filter($words, function($word) {
return $word!=='' && $word !== ',';
});
return array_values($words);
}

/**
* @param string $string
* @return string
*/
private function stringToColor(string $string): string
{
// random color
$rgb = substr(dechex(crc32($string)), 0, 6);
// make it darker
$darker = 2;
list($R16, $G16, $B16) = str_split($rgb, 2);
$R = sprintf("%02X", floor(hexdec($R16) / $darker));
$G = sprintf("%02X", floor(hexdec($G16) / $darker));
$B = sprintf("%02X", floor(hexdec($B16) / $darker));
$R = sprintf('%02X', floor(hexdec($R16) / $darker));
$G = sprintf('%02X', floor(hexdec($G16) / $darker));
$B = sprintf('%02X', floor(hexdec($B16) / $darker));
return '#' . $R . $G . $B;
}

}