简体   繁体   English

调整大小问题:基于用户输入的动态图像创建:Imagick:PHP:自动调整大小

[英]Resize issue : Dynamic Image creation on the base of user Input : Imagick : PHP : Resize Auto

I am creating a dynamic Image on the base of User Input where I have 2 textboxes and 1 Image upload in form,我正在基于用户输入创建一个动态图像,其中我有 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;

Current Image output of is当前图像 output 为

在此处输入图像描述

My sample uploaded Image我上传的样本图片

在此处输入图像描述

I already have pre-define font so I am using at as Roboto-Black.ttf and font-size, here I am $customImage getting as dynamic Image(where user uploaded) the Image and First Line and Second Line I am getting dynamic as well as test.png is also dynamic,我已经预先定义了字体,所以我使用的是Roboto-Black.ttf和字体大小,这里我是$customImage获取动态图像(用户上传的位置)图像和First LineSecond Line我变得动态为以及test.png也是动态的,

So, when I create the Image with I am defining the Image size $image->newImage(800, 75, $pixel);因此,当我创建图像时,我正在定义图像大小$image->newImage(800, 75, $pixel); sometimes Image size is too big when test.png is small so after creating an Image is there any way to create the result Image as "auto" resize so it will auto adjust with the dynamic font and dynamic Image?有时当test.png很小时,图像尺寸太大,所以在创建图像后,有什么方法可以将结果图像创建为“自动”调整大小,以便使用动态字体和动态图像自动调整?

There is [resizeImage][3] from the but as a parameter it is expecting the height and width and I want as "auto"[resizeImage][3]但作为参数,它期望高度和宽度,我想要“自动”

Here's code that will perform a resize of the image based on the geometry (width) of the widest of two annotation text lines.下面的代码将根据两个注释文本行中最宽的几何形状(宽度)来调整图像大小。 This should help solve part of the challenges you have.这应该有助于解决您面临的部分挑战。

The code:编码:

<?php

// >> Q: Resize image to the width of a text box (2 lines) using Imagick in PHP
// >> URL: https://stackoverflow.com/questions/62772906/resize-issue-dynamic-image-creation-on-the-base-of-user-input-imagick-php
//
// >> 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草图output

Instead of trying to dynamically size things, it is probably easier to just pick a fixed to work with and scale what the user provides to match that.与其尝试动态调整大小,不如选择一个固定的来使用并缩放用户提供的内容以匹配它可能更容易。

For the upload it would look something like this:对于上传,它看起来像这样:

define('IDEAL_WIDTH', 800);
define('IDEAL_HEIGHT', 800);

$customImage = new Imagick('test.png');//dynamic image
$customImage->scaleImage(IDEAL_WIDTH, IDEAL_HEIGHT);

And for your image where you are drawing text it would be:对于您正在绘制文本的图像,它将是:

$image->newImage(IDEAL_WIDTH, IDEAL_HEIGHT, $pixel);

You'll need to adjust where you are drawing your text slightly, but that should be really easy because you are working in a fixed area.您需要稍微调整绘制文本的位置,但这应该很容易,因为您在固定区域工作。

Other versions of this are to get a little crazier and ask the user for certain things like where they want the text and what font sizes to use.其他版本会变得更疯狂,并询问用户某些事情例如他们想要文本的位置以及要使用的字体大小。

This is more of a "hint" than answer;这更像是一个“提示”而不是答案; I do not use Imagick as I prefer Imagemagick with exec();我不使用 Imagick,因为我更喜欢 Imagemagick 和 exec();

There is an example at the bottom of this thread: How do you append images that are already created with appendImage?此线程底部有一个示例: 您如何使用 appendImage 创建的 append 图像?

I was thinking along the lines of this before I found the above and you may be able to combine the two:在找到上述内容之前,我一直在考虑这一点,您可以将两者结合起来:

/* 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM