Last active
December 3, 2021 23:00
-
-
Save zakariamouhid/c997b1095ddf69c7fda6169fcf83cc7f to your computer and use it in GitHub Desktop.
write arabic text inside box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$input_text = isset($_GET['text']) ? $_GET['text'] : ''; | |
$font = './fonts/Cairo-Regular.ttf'; | |
$bg_image_path = 'photo.png'; | |
$font = realpath($font); | |
require('./I18N/Arabic.php'); | |
$Arabic = new I18N_Arabic('Glyphs'); | |
$text = $Arabic->utf8Glyphs(htmlspecialchars($input_text)); | |
$img = imagecreatefrompng($bg_image_path); | |
// Create some colors | |
$white = imagecolorallocate($img, 255, 255, 255); | |
$grey = imagecolorallocate($img, 128, 128, 128); | |
$red = imagecolorallocatealpha($img, 255, 0, 0, 50); | |
$black = imagecolorallocate($img, 0, 0 , 0); | |
function image_ttf_text_rect( $options ) { | |
$img = $options['image']; | |
$font_size = $options['font_size']; | |
$angle = 0; | |
$x = $options['x']; | |
$y = $options['y']; | |
$w = $options['width']; | |
$h = $options['height']; | |
$background_color = $options['background_color']; | |
$text_color = $options['text_color']; | |
$font = $options['font']; | |
$text = $options['text']; | |
$text_align = $options['text_align']; | |
$margin_x = $options['margin_x']; | |
$margin_y = $options['margin_y']; | |
$space_between_lines = $options['space_between_lines']; | |
$text_words = explode(' ', $text); | |
$text_words = array_reverse( $text_words ); | |
$lines = array( | |
'' | |
); | |
foreach ($text_words as $word) { | |
$last_line = $lines[count($lines) - 1]; | |
if ($last_line) { | |
$new_line = $word . ' ' . $last_line; | |
$box = imagettfbbox($font_size, 0, $font, $new_line); | |
if ($box[2] > $w - $margin_x * 2) { | |
$lines[] = $word; | |
} else { | |
$lines[count($lines) - 1] = $last_line ? $new_line : $word; | |
} | |
} else { | |
$lines[count($lines) - 1] = $word; | |
} | |
} | |
$lines = array_reverse($lines); | |
// imagefilledrectangle($img, $x, $y, $x + $w, $y + $h, $background_color); | |
imagerectangle($img, $x, $y, $x + $w, $y + $h, $background_color); | |
$line_y_offset = 0; | |
for ($i = 0; $i < count($lines); $i++) { | |
$text = $lines[$i]; | |
/* | |
0 lower left corner, X position | |
1 lower left corner, Y position | |
2 lower right corner, X position | |
3 lower right corner, Y position | |
4 upper right corner, X position | |
5 upper right corner, Y position | |
6 upper left corner, X position | |
7 upper left corner, Y position | |
*/ | |
$bbox = imagettfbbox($font_size, $angle, $font, $text); | |
$line_x = $x + $margin_x; | |
if ($text_align === 'right') { | |
$line_x = $x + $w - ($bbox[2] - $bbox[0]) - $margin_x; | |
} else if ($text_align === 'center') { | |
$line_x = $x + $w / 2 - ($bbox[2] - $bbox[0]) / 2; | |
} | |
$line_y = $y + $h - $bbox[1] - $line_y_offset - $margin_y; | |
$line_y_offset += $bbox[1] - $bbox[7] + $space_between_lines; | |
imagettftext($img, $font_size, $angle, $line_x, $line_y, $text_color, $font, $text); | |
} | |
} | |
image_ttf_text_rect( [ | |
'image' => $img, | |
'font_size' => isset($_GET['size']) ? floatval($_GET['size']) : 30, | |
'x' => isset($_GET['x']) ? intval($_GET['x']) : 500, | |
'y' => isset($_GET['y']) ? intval($_GET['y']) : 100, | |
'width' => isset($_GET['w']) ? intval($_GET['w']) : 400, | |
'height' => isset($_GET['h']) ? intval($_GET['h']) : 300, | |
'background_color' => $red, | |
'text_color' => $black, | |
'font' => $font, | |
'text' => $text, | |
'text_align' => isset($_GET['text_align']) ? $_GET['text_align'] : 'center', | |
'margin_x' => isset($_GET['margin_x']) ? floatval($_GET['margin_x']) : 20, | |
'margin_y' => isset($_GET['margin_y']) ? floatval($_GET['margin_y']) : 20, | |
'space_between_lines' => isset($_GET['space_between_lines']) ? floatval($_GET['space_between_lines']) : 10, | |
] ); | |
header("Content-type: image/png"); | |
imagepng($img); | |
imagedestroy($img); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment