php - PHP ImageMagick - 如何使用自动选择的字体大小功能在行和块内居中
问题描述
PHP 5.4。欢迎大家,我应用了一个基于源的解决方案,其中设置了一个文本块,并且根据文本下载 zip 的长度自动选择文本的大小 现在有必要文本在指定的框和行内居中,在屏幕截图中清楚地显示,并且还可以正确旋转文本
截图:
function autofit_text_to_image( $canvas_image_filename = false, $dest_filename = 'output.jpg', $text = '', $starting_font_size = 60, $max_width = 500, $max_height = 500, $x_pos = 0, $y_pos = 0, $font_file = false,$font_color = 'black',$rotate = 0, $line_height_ratio = 1, $dest_format = 'jpg' ) {
// Bail if any essential parameters are missing
if ( ! $canvas_image_filename || ! $dest_filename || empty($text) || ! $font_file || empty($font_color) || empty($max_width) || empty($max_height) ) return false;
// Do we have a valid canvas image?
if ( ! file_exists($canvas_image_filename) ) return;
$canvas_handle = fopen( $canvas_image_filename, 'rb' );
// Load image into Imagick
$NewImage = new Imagick();
$NewImage->readImageFile($canvas_handle);
// Instantiate Imagick utility objects
$draw = new ImagickDraw();
$pixel = new ImagickPixel( $font_color );
// Load Font
$font_size = $starting_font_size;
$draw->setFont($font_file);
$draw->setFontSize($font_size);
$draw->setFillColor($font_color);
//$draw->setFillColor('red');
// Holds calculated height of lines with given font, font size
$total_height = 0;
$countall=0;
// Run until we find a font size that doesn't exceed $max_height in pixels
while (( 0 == $total_height || $total_height > $max_height )) {
$countall=$countall+1;
if ( $total_height > 0 ) $font_size--; // we're still over height, decrement font size and try again
$draw->setFontSize($font_size);
// Calculate number of lines / line height
// Props users Sarke / BMiner: http://stackoverflow.com/questions/5746537/how-can-i-wrap-text-using-imagick-in-php-so-that-it-is-drawn-as-multiline-text
//$words = preg_split('%\s%', $text, -1, PREG_SPLIT_NO_EMPTY);
$words =preg_split( '%\s%', $text, -1, PREG_SPLIT_NO_EMPTY );
$lines = array();
$i = 0;
$line_height = 0;
$one=1;
while ( count($words) > 0 ) {
$one=$one+1;
$metrics = $NewImage->queryFontMetrics( $draw, implode(' ', array_slice($words, 0, ++$i)));
$line_height = max( $metrics['textHeight'], $line_height );
if ( $metrics['textWidth'] > $max_width || count($words) < $i ) {
// this indicates long words and forces the font to decrease in the first loop
if($i == 1){
$total_height = $max_height + 1;
continue 2;
}
$lines[] = implode( ' ', array_slice($words, 0, --$i) );
$words = array_slice( $words, $i );
$i = 0;
}
}
$total_height = count($lines) * $line_height * $line_height_ratio;
if ( $total_height === 0 ) return false; // don't run endlessly if something goes wrong
}
// Writes text to image
for( $i = 0; $i < count($lines); $i++ ) {
$NewImage->annotateImage( $draw, $x_pos, $y_pos + ($i * $line_height * $line_height_ratio), $rotate, $lines[$i] );
}
$NewImage->setImageFormat($dest_format);
$result = $NewImage->writeImage($dest_filename);
return $result;
}
//The file on which we impose the image
$canvas_image_filename = 'test.jpeg';
$text = 'Lorem ipsum dolor sit amet, sed deleniti intellegebat cu, et ius molestiae abhorreant reprehendunt. Ex eius mollis adipiscing vix, audire theophrastus has et, malorum numquam sea et.
Ut vel vidit falli, in error inermis argumentum per, et doctus necessitatibus vel.
Ut has tota gloriatur, eum ad tale voluptua deserunt. Mel iriure deleniti forensibus at.';
// We load the font
$text ="Little text";
$font_file = 'Oswald-Regular.ttf';
$font_color = 'black';
//image
// Directory from the root
$dirfile='/home/xxxxx/xxxxxxx.com/htdocs/www/xxxx/';
$gimg=str_replace(".", "",microtime(TRUE));
if (!file_exists($dirfile.'test/')) {
if (!mkdir($dirfile.'test/', 0755, true)) { die('Eror');}
}
$file_save=$dirfile.'test/'.$gimg.".jpg";
$result=autofit_text_to_image($canvas_image_filename, $file_save, $text, 60, 399, 290, 350, 128, $font_file,$font_color,0);
解决方案
推荐阅读
- c++ - 如何在 C++20 范围内将投影与变换结合起来
- visual-studio-code - VS Code 使用 AWS SAM template.yml 显示错误
- npm - 从 tgz 文件安装 npm - 当 Internet 断开连接时
- javascript - 雪花声明在程序中不起作用
- java - 休眠:无法将数据取回 Map<>
- python - 如何为非连续日期的季度 HPR 定义百分比变化函数
- kotlin - 重新打开使用 kotlin 以编程方式创建并由 Gone 关闭的 ScrollView
- vue.js - 当输入字段可见时,Vue 将焦点放在输入字段上
- amazon-web-services - AWS Typescript CDK,尝试将 kinesis 流作为来源添加到 firehose
- r - 提取时间序列的最后一次观察 - “开始”不能在“结束”之后