Skip to content

Commit 9d0a807

Browse files
author
T. Todua
authored
Update text-to-image.php
1 parent 5fb6262 commit 9d0a807

1 file changed

Lines changed: 17 additions & 32 deletions

File tree

text-to-image.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
2+
// Usage: TextToImage("Hiii");
23

3-
function TextToImage_my(
4-
$text,
5-
$separate_line_after_chars=40,
4+
function TextToImage(
5+
$text="hello world",
6+
$newline_after_letters=40,
67
$font='./myfont.ttf',
78
$size=24,
89
$rotate=0,
@@ -11,20 +12,21 @@ function TextToImage_my(
1112
$color=array('red'=>0,'grn'=>0,'blu'=>0),
1213
$bg_color=array('red'=>255,'grn'=>255,'blu'=>255)
1314
){
14-
$amount_of_lines= ceil(strlen($text)/$separate_line_after_chars);
15-
$x=explode("\n", $text); $final='';
16-
foreach($x as $key=>$value){
17-
$returnes='';
18-
do{ $first_part=mb_substr($value, 0, $separate_line_after_chars, 'utf-8');
19-
$value= "\n".mb_substr($value, $separate_line_after_chars, null, 'utf-8');
20-
$returnes .=$first_part;
21-
} while( mb_strlen($value,'utf-8')>$separate_line_after_chars);
22-
$final .= $returnes."\n";
23-
}
24-
$text=$final;
15+
//other version: pastebin(dot).com/XVVUyWGD
16+
$amount_of_lines= ceil(strlen($text)/$newline_after_letters)+substr_count($text, '\n')+1;
17+
$all_lines=explode("\n", $text); $text=""; $amount_of_lines = count($all_lines);
18+
foreach($all_lines as $key=>$value){
19+
while( mb_strlen($value,'utf-8')>$newline_after_letters){
20+
$text_final .= mb_substr($value, 0, $newline_after_letters, 'utf-8')."\n";
21+
$value = mb_substr($value, $newline_after_letters, null, 'utf-8');
22+
}
23+
$text .= mb_substr($value, 0, $newline_after_letters, 'utf-8') . ( $amount_of_lines-1 == $key ? "" : "\n");
24+
}
25+
26+
//
2527
Header("Content-type: image/png");
2628
$width=$height=$offset_x=$offset_y = 0;
27-
if(!is_file($font)) { file_put_contents($font,file_get_contents('https://github.com/edx/edx-certificates/raw/master/template_data/fonts/Arial%20Unicode.ttf')); }
29+
if(!is_file($font)) { file_put_contents($font,file_get_contents('https://github.com/potyt/fonts/raw/master/macfonts/Arial%20Unicode%20MS/Arial%20Unicode.ttf')); }
2830

2931
// get the font height.
3032
$bounds = ImageTTFBBox($size, $rotate, $font, "W");
@@ -57,20 +59,3 @@ function TextToImage_my(
5759
// output PNG object.
5860
imagePNG($image);
5961
}
60-
61-
//======helper function==========
62-
if(!function_exists('mb_substr_replace')){
63-
function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = "UTF-8") {
64-
if (extension_loaded('mbstring') === true){
65-
$string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
66-
if ($start < 0) { $start = max(0, $string_length + $start); }
67-
else if ($start > $string_length) {$start = $string_length; }
68-
if ($length < 0){ $length = max(0, $string_length - $start + $length); }
69-
else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length; }
70-
if (($start + $length) > $string_length){$length = $string_length - $start;}
71-
if (is_null($encoding) === true) { return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length); }
72-
return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
73-
}
74-
return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
75-
}
76-
}

0 commit comments

Comments
 (0)