Created
July 2, 2014 14:42
-
-
Save daliborgogic/2eac49ad1ec53de93770 to your computer and use it in GitHub Desktop.
multi line
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 | |
function multilineText($image, $font_size, $color, $font, $text, $start_x, $start_y, $max_width) { | |
$words = explode(" ", $text); | |
$string = ""; | |
$tmp_string = ""; | |
$font_size = "16"; | |
for($i = 0; $i < count($words); $i++) { | |
$tmp_string .= $words[$i]." "; | |
//check size of string | |
$dim = imagettfbbox($font_size, 0, $font, $tmp_string); | |
if($dim[4] < ($max_width - $start_x)) { | |
$string = $tmp_string; | |
$curr_width = $dim[4]; | |
} else { | |
$i--; | |
$tmp_string = ""; | |
//left | |
// $start_xx = $start_x + round(($max_width - $curr_width - $start_x) / 2); | |
// imagettftext($image, $font_size, 0, $start_x, $start_y, $color, $font, $string); | |
//right | |
//$start_xx = round(($max_width - $curr_width - $start_x) ); | |
//imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string); | |
//center | |
$start_xx = $start_x + round(($max_width - $curr_width - $start_x) / 2); | |
imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string); | |
$string = ""; | |
// line height lol | |
$start_y += abs($dim[5]) * 1.2; | |
$curr_width = 0; | |
} | |
} | |
// left | |
// $start_xx = $start_x - round(($max_width - $dim[4] - $start_x) / 2); | |
// imagettftext($image, $font_size, 0, $start_x, $start_y, $color, $font, $string); | |
// right | |
//$start_xx = round(($max_width - $curr_width - $start_x) ); | |
//imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string); | |
//center | |
$start_xx = $start_x + round(($max_width - $curr_width - $start_x) / 2); | |
imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string); | |
} | |
// Create a image | |
$im = imagecreatetruecolor(410, 410); | |
$red = imagecolorallocate($im, 255, 0, 0); | |
$black = imagecolorallocate($im, 0, 0, 0); | |
$white = imagecolorallocate($im, 255, 255, 255); | |
// Set the background to be white | |
imagefilledrectangle($im, 1, 1, 408, 408, $white); | |
// Path to our font file | |
$font = 'C:\drive\font\a\Adobe Garamond Italic\adobe_garamond_italic-webfont.ttf'; | |
$text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugiat eum itaque officiis, suscipit, recusandae corporis. Deserunt magnam voluptates omnis, excepturi."; | |
multilineText($im, 18, $red, $font, $text, 15,60, 393); | |
// Output to browser | |
header('Content-Type: image/png'); | |
imagepng($im); | |
imagedestroy($im); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment