调整大小问题:基于用户输入创建动态图像:Imagick:PHP:自动调整大小
Resize issue : Dynamic Image creation on the base of user Input : Imagick : PHP : Resize Auto
我正在根据用户输入创建一个动态图像,其中我有 2 个文本框和 1 个以表单形式上传的图像,
<?php
/* Create some objects */
$customImage = new Imagick('test.png');//dynamic image
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( '#FFF' );
/* New image */
$image->newImage(800, 75, $pixel);
/* Black text */
$draw->setFillColor('#a28430');
/* Font properties */
$draw->setFont('Roboto-Black.ttf');
$draw->setFontSize( 30 );
/* Create text */
$image->compositeImage($customImage,Imagick::COMPOSITE_DEFAULT, 10, 20);
$image->annotateImage($draw, 10, 50, 0, "First Line");// dynaimc text
$image->annotateImage($draw, 10, 70, 0, "second line");// dynamic text
/* Give image a format */
$image->setImageFormat('png');
/* Output the image with headers */
header('Content-type: image/png');
echo $image;
当前图像输出为
我上传的示例图片
我已经有了预定义字体,所以我使用 at as Roboto-Black.ttf
和 font-size ,这里我 $customImage
获取动态图像(用户上传的地方)图像和 First Line
和 Second Line
我变得充满活力 test.png
也是充满活力的,
所以,当我创建图像时,我正在定义图像尺寸 $image->newImage(800, 75, $pixel);
有时图像尺寸太大,而 test.png
很小,所以在创建图像后有什么方法可以创建结果图像为“自动”调整大小,因此它将使用动态字体和动态图像自动调整?
有 [resizeImage][3]
,但作为参数,它需要高度和宽度,我想要“自动”
与其尝试动态调整大小,不如选择一个固定的对象并缩放用户提供的内容以匹配它可能更容易。
对于上传,它看起来像这样:
define('IDEAL_WIDTH', 800);
define('IDEAL_HEIGHT', 800);
$customImage = new Imagick('test.png');//dynamic image
$customImage->scaleImage(IDEAL_WIDTH, IDEAL_HEIGHT);
对于您绘制文字的图像,它将是:
$image->newImage(IDEAL_WIDTH, IDEAL_HEIGHT, $pixel);
您需要稍微调整绘制文本的位置,但这应该很容易,因为您在固定区域工作。
这个的其他版本会变得有点疯狂,并询问用户某些事情,例如他们想要文本的地方以及要使用的字体大小。
与其说是答案,不如说是“提示”!我不使用 Imagick,因为我更喜欢 Imagemagick with exec();
本帖底部有一个示例:How do you append images that are already created with appendImage?
在找到上述内容之前,我一直在思考这个问题,您可以将两者结合起来:
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( '#FFF' );
/* New image */
$image->newImage(800, 75, $pixel);
/* Black text */
$draw->setFillColor('white');
/* Font properties */
$draw->setFont('Roboto-Black.ttf');
$draw->setFontSize( 30 );
/* Create text */
$image->annotateImage($draw, 10, 50, 0, "First Line");// dynaimc text
$image->annotateImage($draw, 10, 70, 0, "second line");// dynamic text
// Get the size of the image to esize the text
$size = getimagesize('MB75H.jpg');
// Resize the text - need to calculate the height from the new width
$image->resizeimage($size[0],100,Imagick::FILTER_LANCZOS,1);
// Load the second image
$image = new Imagick('MB75H.jpg');//dynamic image
$image->resetIterator();
$combined = $image->appendImages(true);
/* Give image a format */
$image->setImageFormat('png');
/* Output the image with headers */
header('Content-type: image/png');
echo $combined;
下面的代码将根据最宽的两个注释文本行的几何形状(宽度)调整图像大小。这应该有助于解决您面临的部分挑战。
代码:
<?php
// >> Q: Resize image to the width of a text box (2 lines) using Imagick in PHP
// >> URL:
//
// >> How to get geometry of an image using Imagick in PHP
// >> > https://www.php.net/manual/en/imagick.getimagegeometry.php
//
// >> How to get dimensions of text using Imagick in PHP
// >> > https://www.php.net/manual/en/imagick.queryfontmetrics.php
/* Create some objects */
$customImage = new Imagick('test.png');//dynamic image
$ciGeo=$customImage->getImageGeometry(); // >>
$ciWidth=$ciGeo['width']; // >>
$ciHeight=$ciGeo['height']; // >>
$ciAspect=$ciWidth/$ciHeight; // >> > Aspect ratio (eg. 1=square)
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( '#FFF' );
/* Black text */
$draw->setFillColor('#a28430');
/* Font properties */
$draw->setFont('Roboto-Black.ttf');
$draw->setFontSize( 30 );
// >> Get two annotation text lines
$textLine1 = "First Line";
$textLine2 = "second line";
// >> Get text lines metrics to calculate geometry text box
$metricsL1 = $image->queryFontMetrics($draw, $textLine1);
$metricsL2 = $image->queryFontMetrics($draw, $textLine2);
$textWidth = $metricsL1['textWidth'];
if ($metricsL2['textWidth'] > $metricsL1['textWidth'])
{ $textWidth = $metricsL2['textWidth'];
};
// >> Resize images based on text box width
$diWidth = $textWidth + 20; // >> +20 = 2*10 padding
$diHeight = ($textWidth + 20)*$ciAspect; // >> Height = width * aspect ratio
$customImage->scaleImage($diWidth, $diHeight); // >> Scale
// New image
$image->newImage($diWidth, $diHeight, $pixel); // ++
// Create text
$image->compositeImage($customImage,Imagick::COMPOSITE_DEFAULT, 0, 0);
$image->annotateImage($draw, 10, 50, 0, $textLine1);// dynaimc text >>
$image->annotateImage($draw, 10, 70, 0, $textLine2);// dynamic text >>
// Give image a format
$image->setImageFormat('png');
// Output the image with headers
header('Content-type: image/png');
echo $image;
?>
a sketch
output
我正在根据用户输入创建一个动态图像,其中我有 2 个文本框和 1 个以表单形式上传的图像,
<?php
/* Create some objects */
$customImage = new Imagick('test.png');//dynamic image
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( '#FFF' );
/* New image */
$image->newImage(800, 75, $pixel);
/* Black text */
$draw->setFillColor('#a28430');
/* Font properties */
$draw->setFont('Roboto-Black.ttf');
$draw->setFontSize( 30 );
/* Create text */
$image->compositeImage($customImage,Imagick::COMPOSITE_DEFAULT, 10, 20);
$image->annotateImage($draw, 10, 50, 0, "First Line");// dynaimc text
$image->annotateImage($draw, 10, 70, 0, "second line");// dynamic text
/* Give image a format */
$image->setImageFormat('png');
/* Output the image with headers */
header('Content-type: image/png');
echo $image;
当前图像输出为
我上传的示例图片
我已经有了预定义字体,所以我使用 at as Roboto-Black.ttf
和 font-size ,这里我 $customImage
获取动态图像(用户上传的地方)图像和 First Line
和 Second Line
我变得充满活力 test.png
也是充满活力的,
所以,当我创建图像时,我正在定义图像尺寸 $image->newImage(800, 75, $pixel);
有时图像尺寸太大,而 test.png
很小,所以在创建图像后有什么方法可以创建结果图像为“自动”调整大小,因此它将使用动态字体和动态图像自动调整?
有 [resizeImage][3]
,但作为参数,它需要高度和宽度,我想要“自动”
与其尝试动态调整大小,不如选择一个固定的对象并缩放用户提供的内容以匹配它可能更容易。
对于上传,它看起来像这样:
define('IDEAL_WIDTH', 800);
define('IDEAL_HEIGHT', 800);
$customImage = new Imagick('test.png');//dynamic image
$customImage->scaleImage(IDEAL_WIDTH, IDEAL_HEIGHT);
对于您绘制文字的图像,它将是:
$image->newImage(IDEAL_WIDTH, IDEAL_HEIGHT, $pixel);
您需要稍微调整绘制文本的位置,但这应该很容易,因为您在固定区域工作。
这个的其他版本会变得有点疯狂,并询问用户某些事情,例如他们想要文本的地方以及要使用的字体大小。
与其说是答案,不如说是“提示”!我不使用 Imagick,因为我更喜欢 Imagemagick with exec();
本帖底部有一个示例:How do you append images that are already created with appendImage?
在找到上述内容之前,我一直在思考这个问题,您可以将两者结合起来:
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( '#FFF' );
/* New image */
$image->newImage(800, 75, $pixel);
/* Black text */
$draw->setFillColor('white');
/* Font properties */
$draw->setFont('Roboto-Black.ttf');
$draw->setFontSize( 30 );
/* Create text */
$image->annotateImage($draw, 10, 50, 0, "First Line");// dynaimc text
$image->annotateImage($draw, 10, 70, 0, "second line");// dynamic text
// Get the size of the image to esize the text
$size = getimagesize('MB75H.jpg');
// Resize the text - need to calculate the height from the new width
$image->resizeimage($size[0],100,Imagick::FILTER_LANCZOS,1);
// Load the second image
$image = new Imagick('MB75H.jpg');//dynamic image
$image->resetIterator();
$combined = $image->appendImages(true);
/* Give image a format */
$image->setImageFormat('png');
/* Output the image with headers */
header('Content-type: image/png');
echo $combined;
下面的代码将根据最宽的两个注释文本行的几何形状(宽度)调整图像大小。这应该有助于解决您面临的部分挑战。
代码:
<?php
// >> Q: Resize image to the width of a text box (2 lines) using Imagick in PHP
// >> URL:
//
// >> How to get geometry of an image using Imagick in PHP
// >> > https://www.php.net/manual/en/imagick.getimagegeometry.php
//
// >> How to get dimensions of text using Imagick in PHP
// >> > https://www.php.net/manual/en/imagick.queryfontmetrics.php
/* Create some objects */
$customImage = new Imagick('test.png');//dynamic image
$ciGeo=$customImage->getImageGeometry(); // >>
$ciWidth=$ciGeo['width']; // >>
$ciHeight=$ciGeo['height']; // >>
$ciAspect=$ciWidth/$ciHeight; // >> > Aspect ratio (eg. 1=square)
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( '#FFF' );
/* Black text */
$draw->setFillColor('#a28430');
/* Font properties */
$draw->setFont('Roboto-Black.ttf');
$draw->setFontSize( 30 );
// >> Get two annotation text lines
$textLine1 = "First Line";
$textLine2 = "second line";
// >> Get text lines metrics to calculate geometry text box
$metricsL1 = $image->queryFontMetrics($draw, $textLine1);
$metricsL2 = $image->queryFontMetrics($draw, $textLine2);
$textWidth = $metricsL1['textWidth'];
if ($metricsL2['textWidth'] > $metricsL1['textWidth'])
{ $textWidth = $metricsL2['textWidth'];
};
// >> Resize images based on text box width
$diWidth = $textWidth + 20; // >> +20 = 2*10 padding
$diHeight = ($textWidth + 20)*$ciAspect; // >> Height = width * aspect ratio
$customImage->scaleImage($diWidth, $diHeight); // >> Scale
// New image
$image->newImage($diWidth, $diHeight, $pixel); // ++
// Create text
$image->compositeImage($customImage,Imagick::COMPOSITE_DEFAULT, 0, 0);
$image->annotateImage($draw, 10, 50, 0, $textLine1);// dynaimc text >>
$image->annotateImage($draw, 10, 70, 0, $textLine2);// dynamic text >>
// Give image a format
$image->setImageFormat('png');
// Output the image with headers
header('Content-type: image/png');
echo $image;
?>
a sketch output