{"id":513,"date":"2012-03-19T11:12:07","date_gmt":"2012-03-19T09:12:07","guid":{"rendered":"https:\/\/polyetilen.lt\/?p=513"},"modified":"2023-05-02T09:34:26","modified_gmt":"2023-05-02T06:34:26","slug":"text-wrap-for-imagettftext","status":"publish","type":"post","link":"https:\/\/polyetilen.lt\/en\/text-wrap-for-imagettftext","title":{"rendered":"Text wrap for imagettftext"},"content":{"rendered":"<p>One line text does not always fit into a given width of the picture, then you need to word wrap the text to a new line, depending on the width. If only the width is less than one word, then you will need a shaman's drum and witchcraft. PHP code to create a picture of the text with a given width:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/content type\r\nheader('Content-Type: image\/png');\r\n\r\n\/\/font\r\n$font = 'arial.ttf';\r\n\/\/font size\r\n$font_size = 10;\r\n\/\/image width\r\n$width = 100;\r\n\/\/text margin\r\n$margin = 5;\r\n\r\n\/\/text\r\n$text = &quot;That is why where when why and how, and now why where when why and how?&quot;;\r\n\r\n\/\/explode text by words\r\n$text_a = explode(' ', $text);\r\n$text_new = '';\r\nforeach($text_a as $word){\r\n\t\/\/Create a new text, add the word, and calculate the parameters of the text\r\n\t$box = imagettfbbox($font_size, 0, $font, $text_new.' '.$word);\r\n\t\/\/if the line fits to the specified width, then add the word with a space, if not then add word with new line\r\n\tif($box&#x5B;2] &gt; $width - $margin*2){\r\n\t\t$text_new .= &quot;\\n&quot;.$word;\r\n\t} else {\r\n\t\t$text_new .= &quot; &quot;.$word;\r\n\t}\r\n}\r\n\/\/trip spaces\r\n$text_new = trim($text_new);\r\n\/\/new text box parameters\r\n$box = imagettfbbox($font_size, 0, $font, $text_new);\r\n\/\/new text height\r\n$height = $box&#x5B;1] + $font_size + $margin * 2;\r\n\r\n\/\/create image\r\n$im = imagecreatetruecolor($width, $height);\r\n\r\n\/\/create colors\r\n$white = imagecolorallocate($im, 255, 255, 255);\r\n$black = imagecolorallocate($im, 0, 0, 0);\r\n\/\/color image\r\nimagefilledrectangle($im, 0, 0, $width, $height, $white);\r\n\r\n\/\/add text to image\r\nimagettftext($im, $font_size, 0, $margin, $font_size+$margin, $black, $font, $text_new);\r\n\r\n\/\/return image\r\nimagepng($im);\r\n\/\/frees any memory associated with image\r\nimagedestroy($im);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One line text does not always fit into a given width of the picture, then you need to word wrap the text to a new line, depending on the width. If only the width is &hellip; <a href=\"https:\/\/polyetilen.lt\/en\/text-wrap-for-imagettftext\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"http:\/\/polyetilen.lt\/?p=267","footnotes":""},"categories":[8],"tags":[97,98,99,34,100],"class_list":["post-513","post","type-post","status-publish","format-standard","hentry","category-programavimas","tag-imagettfbbox","tag-imagettftext","tag-perkelimas","tag-php","tag-tekstas","en-US"],"_links":{"self":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":1,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/513\/revisions\/514"}],"wp:attachment":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/media?parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/categories?post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/tags?post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}