Permalink
Browse files

🔖 Add Center Padding Calculater And Drawer

  • Loading branch information...
KingsleyXie committed Jul 28, 2018
1 parent 3a29454 commit 8c188f795f457a9e7e89122eed882559dea9da39
Showing with 128 additions and 104 deletions.
  1. +43 −26 SendBirthdayEmails.php
  2. +0 −12 Tools/TextBox.php
  3. +0 −66 Tools/TextDataParser.php
  4. +39 −0 Tools/TextPaddingParser.php
  5. +46 −0 Tools/TextSizeParser.php
@@ -1,12 +1,12 @@
<?php
require_once 'FontLib\Autoloader.php';
require_once 'Tools\FontChecker.php';
require_once 'Tools\TextBox.php';
require_once 'Tools\TextDataParser.php';
require_once 'Tools\TextSizeParser.php';
require_once 'Tools\TextPaddingParser.php';
use Tools\FontChecker;
use Tools\TextBox;
use Tools\TextDataParser;
use Tools\TextSizeParser;
use Tools\TextPaddingParser;
class SendBirthdayEmails
{
@@ -19,40 +19,57 @@ public function generateImage($name, $dir) {
$font_path_alternate = $asset_path . '/alt.ttf';
$checker = new FontChecker;
$parser = new TextDataParser;
$parser = new TextSizeParser;
$td = null;
$font_size = 0;
if ($checker->isStringValid($name, $font_path)) {
// If current font supports the text
// Calculate Text Related Data(Size & Position)
$td = $parser->calc($name);
$font_size = $parser->getSize($name);
} else {
$font_path = $font_path_alternate;
$td = $parser->calcAltFont($name);
$font_size = $parser->getAltSize($name);
}
// imagettftext(
// $image,
// $td->size,
// 0,
// $td->paddingLeft,
// $td->paddingTop,
// $color,
// $font_path,
// str_replace('·', "\n", $name)
// );
imagerectangle(
$parser = new TextPaddingParser;
$padding = $parser->getPadding($font_size, $font_path, str_replace('·', "\n", $name));
if (true) {
imagerectangle(
$image,
TextPaddingParser::$RECT['left'],
TextPaddingParser::$RECT['top'],
TextPaddingParser::$RECT['right'],
TextPaddingParser::$RECT['bottom'],
$color
);
$box = imagettfbbox($font_size, 0, $font_path, $name);
imagerectangle(
$image,
$padding['lower_left_x'],
$padding['lower_left_y'],
$padding['upper_right_x'],
$padding['upper_right_y'],
$color
);
}
imagettftext(
$image,
TextBox::$RECT['left'],
TextBox::$RECT['top'],
TextBox::$RECT['right'],
TextBox::$RECT['bottom'],
$color
$font_size,
0,
$padding['lower_left_x'],
$padding['lower_left_y'],
$color,
$font_path,
str_replace('·', "\n", $name)
);
imagepng($image, "$dir/$name.png");
imagedestroy($image);
}
}
$obj = new SendBirthdayEmails;
$obj->generateImage('董建堃', './');
// $obj->generateImage('董建堃', './');
$obj->generateImage('赵五六', './');

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,39 @@
<?php
namespace Tools;
class TextPaddingParser
{
public static $RECT = [
'left' => 630,
'top' => 80,
'right' => 860,
'bottom' => 200
];
public function getPadding($size, $font, $name, $center =true) {
$innerBox = imagettfbbox($size, 0, $font, $name);
$rect = TextPaddingParser::$RECT;
$outterHeight = $rect['bottom'] - $rect['top'];
$innerHeight = abs($innerBox[5] - $innerBox[1]);
$outterWidth = $rect['right'] - $rect['left'];
$innerWidth = abs($innerBox[4] - $innerBox[0]);
$ans = [
'lower_left_x' => $rect['left'],
'lower_left_y' => ($rect['bottom'] - ($outterHeight - $innerHeight) / 2)
];
$ans['upper_right_x'] = $ans['lower_left_x'] + $innerWidth;
$ans['upper_right_y'] = $ans['lower_left_y'] - $innerHeight;
if ($center) {
$offset = ($outterWidth - $innerWidth) / 2;
$ans['lower_left_x'] += $offset;
$ans['upper_right_x'] += $offset;
}
return $ans;
}
}
@@ -0,0 +1,46 @@
<?php
namespace Tools;
class TextSizeParser
{
public function charLen($text) {
return count(preg_split('//u', str_replace('·', '', $text), null, PREG_SPLIT_NO_EMPTY));
}
public function parseIndex($text) {
$len = $this->charLen($text);
if ($len < 5) return $len - 2;
if (strpos($text, '·') !== false) {
if ($len <= 10) return 4;
return 5;
}
return 3;
}
public function getSize($text) {
$fontSize = [
12, // == 2 characters => '董华'
65, // == 3 characters => '董建华'
12, // == 4 characters => '董张宝华'
12, // > 4 characters => '恶魔喵喵喵'
12, // <= 10 characters with interval => '尼古拉斯·赵四'
32 // < 10 characters with interval => '拉斯特洛夫斯基·赵五六七八'
];
return $fontSize[$this->parseIndex($text)];
}
public function getAltSize($text) {
$fontSize = [
17, // == 2 characters => '董堃'
17, // == 3 characters => '董建堃'
17, // == 4 characters => '董张宝堃'
17, // > 4 characters => '恶魔喵堃喵'
17, // <= 10 characters with interval => '尼古拉斯·赵堃'
17 // < 10 characters with interval => '拉斯特洛堃斯基·赵五六七八'
];
return $fontSize[$this->parseIndex($text)];
}
}

0 comments on commit 8c188f7

Please sign in to comment.