public static function makeTextImage($text, $place_width, $place_height, $text_color = '#000000', $font = null, $margin = 2, $dpi = 300) { $new_text = $text; if (empty($font)) { $font = UserUrl::style(false) . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR . 'arial.ttf'; } $max_font_size = 72; //pt $min_font_size = 14; //pt $place_width_px = Utils::mmToPx($place_width, $dpi); $place_height_px = Utils::mmToPx($place_height, $dpi); $margin_px = Utils::mmToPx($margin, $dpi); $max_font_size_px = Utils::ptToPx($max_font_size, $dpi); $current_font_size = $max_font_size; $current_font_size_px = Utils::ptToPx($current_font_size, $dpi); $rect = imagettfbbox($current_font_size_px, 0, $font, $text); $width_px = $rect[2] - $rect[0]; $height_px = $rect[1] - $rect[5]; while ($width_px + $margin_px * 2 > $place_width_px || $height_px + $margin_px * 2 > $place_height_px) { $current_font_size--; //echo 'current_font_size='.$current_font_size; $current_font_size_px = Utils::ptToPx($current_font_size, $dpi); if ($current_font_size <= $min_font_size) { if ($width_px + $margin_px * 2 > $place_width_px) { $new_text = Utils::makeTextBlock($new_text, $font, $current_font_size_px, $place_width_px); } } $rect = imagettfbbox($current_font_size_px, 0, $font, $new_text); //echo 'place_width_px='.$place_width_px.'<br/>'; //echo 'place_height_px='.$place_height_px.'<br/>'; //print_r($rect); $width_px = $rect[2] - $rect[0]; $height_px = $rect[1] - $rect[5]; } $im = imagecreatetruecolor($place_width_px, $place_height_px); $backgroundColor = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $backgroundColor); $box = new Box($im); $box->setFontFace($font); // http://www.dafont.com/franchise.font $box->setFontColor(Utils::hexColorToArray($text_color)); $box->setFontSize($current_font_size_px); $box->setLeading(1); $box->setBox($margin_px, $margin_px, $place_width_px - $margin_px * 2, $place_height_px - $margin_px * 2); $box->setTextAlign('center', 'center'); $box->draw($new_text); // header("Content-type: image/png"); imagesavealpha($im, TRUE); ob_start(); //Stdout --> buffer imagepng($im); $img = ob_get_contents(); //store stdout in $img2 ob_end_clean(); //clear buffer imagedestroy($im); return $img; }