Skip to content

Commit

Permalink
add auto split for chinese traditional text / #9
Browse files Browse the repository at this point in the history
  • Loading branch information
antonlukin committed Jul 22, 2023
1 parent aa8d45b commit 6ae4173
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Draw text on image. Possible options:
- **opacity**: Text opacity from 0 to 100.
- **horizontal**: Horizontal alignment. Can be left/right/center/justify.
- **vertical**: Vertical alginment. Can be top/center/bottom/justify.
- **fontpath**: Path to .ttf font file.
- **fontpath**: Path to .ttf or .otf font file.
- **debug**: Draws text box rectangle if true.

Use boundary to get actual dimensions of the drawn text box. See detailed examples below.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "antonlukin/poster-editor",
"version": "5.8",
"version": "5.9",
"description": "Wrapper for PHP's GD Library for easy image manipulation",
"keywords": ["php", "image", "text", "gd"],
"homepage": "https://github.com/antonlukin/poster-editor",
Expand Down
15 changes: 8 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions example/chinese.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Chinese separators example.
* php version 7.1
*
* @category PHP
* @package PosterEditor
* @author Anton Lukin <[email protected]>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @link https://github.com/antonlukin/poster-editor
*/

require_once __DIR__ . '/../vendor/autoload.php';

try {
$image = new PosterEditor\PosterEditor();
$image->make('images/bridge.jpg')->crop(
900, 600,
array(
'x' => '0',
'y' => '100'
)
);

$image->grayscale()->brightness(-40);

$image->text(
"大家小時候都有寫過紀念冊嗎?通過紀念冊上的文字和圖案,的回憶記錄下來。今年我們希望製作出所有星詠。透過成員互相分享與回憶,化為各地星詠者對星街的支持,以及星詠者之間的連繫與羈絆。",
array(
'x' => 20,
'y' => 0,
'width' => 860,
'horizontal' => 'start',
'vertical' => 'center',
'fontpath' => 'fonts/notosans-tc-regular.otf',
'lineheight' => 1.75,
'fontsize' => 18,
'color' => '#ffffff',
'opacity' => 1,
)
);

$image->show();

} catch(Exception $e) {
echo $e->getMessage();
}
Binary file added example/fonts/notosans-tc-regular.otf
Binary file not shown.
Binary file added example/samples/chinese.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 66 additions & 41 deletions src/PosterEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @package PosterEditor
* @author Anton Lukin <[email protected]>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @version Release: 5.8
* @version Release: 5.9
* @link https://github.com/antonlukin/poster-editor
*/
class PosterEditor
Expand Down Expand Up @@ -207,8 +207,8 @@ public function canvas($width, $height, $options = array())
/**
* Sends HTTP response with current image in given format and quality.
* @param string $format Optional. File image extension. By default used type from make or insert function.
* @param int $quality Optional. Define optionally the quality of the image. From 0 to 100. Default: 90.
* @param string $format Optional. File image extension. By default used type from make or insert function.
* @param int $quality Optional. Define optionally the quality of the image. From 0 to 100. Default: 90.
*
* @return void
*/
Expand Down Expand Up @@ -244,7 +244,7 @@ public function show($format = null, $quality = 90)
/**
* Save the image.
*
* @param string $path Path to the file where to write the image data.
* @param string $path Path to the file where to write the image data.
* @param int $quality Optional. Define optionally the quality of the image. From 0 to 100. Default: 90.
* @param string $format Optional. File image extension. By default use from path.
*
Expand Down Expand Up @@ -385,9 +385,9 @@ public function downsize($width = null, $height = null)
* Cut out a rectangular part of the current image with given width and height.
* Define optional x,y coordinates to move the top-left corner of the cutout to a certain position.
*
* @param int $width Width of the rectangular cutout.
* @param int $height Height of the rectangular cutout.
* @param array $options Optional. List of crop coords. By default crop from center.
* @param int $width Width of the rectangular cutout.
* @param int $height Height of the rectangular cutout.
* @param array $options Optional. List of crop coords. By default crop from center.
*
* @return $this
*/
Expand Down Expand Up @@ -415,9 +415,9 @@ public function crop($width, $height, $options = array())
* The method will find the best fitting aspect ratio on the current image automatically,
* cut it out and resize it to the given dimension.
*
* @param int $width Target image width.
* @param int $height Target image height.
* @param string $position Optional. Crop position.
* @param int $width Target image width.
* @param int $height Target image height.
* @param string $position Optional. Crop position.
*
* @return $this
*/
Expand Down Expand Up @@ -479,11 +479,11 @@ public function fit($width, $height, $position = 'center')
/**
* Draw a line from x,y point 1 to x,y point 2 on current image.
*
* @param int $x1 X-Coordinate of the starting point.
* @param int $y1 Y-Coordinate of the starting point.
* @param int $x2 X-Coordinate of the end point.
* @param int $y2 Y-Coordinate of the end point.
* @param array $options Optional. List of line options.
* @param int $x1 X-Coordinate of the starting point.
* @param int $y1 Y-Coordinate of the starting point.
* @param int $x2 X-Coordinate of the end point.
* @param int $y2 Y-Coordinate of the end point.
* @param array $options Optional. List of line options.
*
* @return $this
*/
Expand Down Expand Up @@ -513,11 +513,11 @@ public function line($x1, $y1, $x2, $y2, $options = array())
/**
* Draw a colored rectangle on current image.
*
* @param int $x X-Coordinate of the starting point.
* @param int $y Y-Coordinate of the starting point.
* @param int $width Width in pixels.
* @param int $height Height in pixels.
* @param array $options Optional. List of line options.
* @param int $x X-Coordinate of the starting point.
* @param int $y Y-Coordinate of the starting point.
* @param int $width Width in pixels.
* @param int $height Height in pixels.
* @param array $options Optional. List of line options.
*
* @return $this
*/
Expand Down Expand Up @@ -551,11 +551,11 @@ public function rectangle($x, $y, $width, $height, $options = array())
/**
* Draw an ellipse.
*
* @param int $x X-Coordinate of the center point.
* @param int $y Y-Coordinate of the center point.
* @param int $width Width in pixels.
* @param int $height Height in pixels.
* @param array $options Optional. List of line options.
* @param int $x X-Coordinate of the center point.
* @param int $y Y-Coordinate of the center point.
* @param int $width Width in pixels.
* @param int $height Height in pixels.
* @param array $options Optional. List of line options.
*
* @return $this
*/
Expand Down Expand Up @@ -771,7 +771,7 @@ public function text($text, $options = array(), &$boundary = array())
$text = $this->wrapText($text, $options);

// Get text lines as array.
$lines = explode("\n", $text);
$lines = explode(PHP_EOL, $text);

// Set default boundary vaules.
$boundary = array_merge(array('width' => 0, 'height' => 0));
Expand All @@ -798,22 +798,23 @@ public function text($text, $options = array(), &$boundary = array())
* Draw single text line on image.
* For justify horizontal alignment split the string word by word and add calclated extraspace.
*
* @param string $line Single text line
* @param array $options List of text settings.
* @param int $width Actual strin length.
* @param int $x X-Coordinate of string starting point.
* @param int $y Y-Coordinate of string starting point.
* @param int $color Text color.
* @param bool $last Is this string is last in the text.
* @param string $line Single text line
* @param array $options List of text settings.
* @param int $width Actual strin length.
* @param int $x X-Coordinate of string starting point.
* @param int $y Y-Coordinate of string starting point.
* @param int $color Text color.
* @param bool $last Is this string is last in the text.
*
* @return $this
*/
protected function drawLine($line, $options, $width, $x, $y, $color, $last) {
if ('justify' !== $options['horizontal']) {
protected function drawLine($line, $options, $width, $x, $y, $color, $last)
{
if ($options['horizontal'] !== 'justify') {
return imagefttext($this->resource, $options['fontsize'], 0, $x, $y, $color, $options['fontpath'], $line);
}

$words = explode(' ', $line);
$words = $this->getWords($line);

// Calc extraspace for justify alignment
$extraspace = $options['fontsize'] / 50;
Expand All @@ -824,7 +825,7 @@ protected function drawLine($line, $options, $width, $x, $y, $color, $last) {

foreach ($words as $index => $word) {
if (count($words) > $index + 1) {
$word = $word . ' ';
$word = $this->removeExtraSpace($word . ' ');
}

$sizes = imagefttext($this->resource, $options['fontsize'], 0, $x, $y, $color, $options['fontpath'], $word);
Expand All @@ -848,7 +849,7 @@ protected function wrapText($text, &$options)
$wrapped = $this->addBreaklines($text, $options);

// Get lines from wrapped text.
$lines = explode("\n", $wrapped);
$lines = explode(PHP_EOL, $wrapped);

// Get text width.
$width = $this->getTextWidth($wrapped, $options);
Expand Down Expand Up @@ -895,10 +896,10 @@ protected function addBreaklines($text, $options, $output = '')
$line = '';

// Split text to words.
$words = explode(' ', $text);
$words = $this->getWords($text);

foreach ($words as $word) {
$sentence = $line . ' ' . $word;
$sentence = $this->removeExtraSpace($line . ' ' . $word);

if (empty($line)) {
$sentence = $word;
Expand All @@ -908,7 +909,7 @@ protected function addBreaklines($text, $options, $output = '')

// Add new line to output.
if ($box[2] > $options['width']) {
$output = $output . $line . "\n";
$output = $output . $line . PHP_EOL;

// Reset line.
$line = $word;
Expand Down Expand Up @@ -1237,6 +1238,30 @@ protected function copyResampled($source, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh
return $this;
}

/**
* Helper function to explode string by words
*
* @param string $string Text string to split to words by space.
*
* @return string
*/
protected function getWords($string)
{
return explode(' ', preg_replace('/([,。?!])/us', '$1 ', $string));
}

/**
* Helper function to remove extraspace added by getWords method
*
* @param string $string Text string to remove extraspaces.
*
* @return string
*/
protected function removeExtraSpace($string)
{
return preg_replace('/([,。?!]) /us', '$1', $string);
}

/**
* Determines if source data is binary data.
*
Expand Down

0 comments on commit 6ae4173

Please sign in to comment.