Php 使用smarty模板显示具有尺寸的图像

Php 使用smarty模板显示具有尺寸的图像,php,smarty,Php,Smarty,我正在使用smarty模板动态呈现页面;这是我在php文件中分配变量的方式: $imgsrc1、$imgsrc2、$imgsrc3包含上载图像文件的位置 $smarty->assign('foto1', $imgsrc1); $smarty->assign('foto2', $imgsrc2); $smarty->assign('foto3', $imgsrc3); 在模板文件中,我以以下方式使用它们: <img src='{$foto1}' border='1' wi

我正在使用smarty模板动态呈现页面;这是我在php文件中分配变量的方式: $imgsrc1、$imgsrc2、$imgsrc3包含上载图像文件的位置

$smarty->assign('foto1', $imgsrc1);
$smarty->assign('foto2', $imgsrc2);
$smarty->assign('foto3', $imgsrc3);
在模板文件中,我以以下方式使用它们:

<img src='{$foto1}' border='1' width="230" height="250">
<img src='{$foto2}' border='1' width="630" height="850">
<img src='{$foto3}' border='1' width="130" height="150">

我知道这不是显示图像的理想方式。这就是工作;只有高度和宽度不被考虑;将显示原始高度和宽度。我想我应该使用{html_image},但我不知道如何分配并使用它在模板中显示

编辑:
请注意,这不仅仅是图像的显示,我想控制它们的大小。例如,无论上传文件的原始属性如何,image1的固定大小必须为230 x 250,image2的固定大小必须为630 x 850,image3的固定大小必须为130 x 150。

您已经暗示了最简单的方法,使用

如果您不想更改php代码

$smarty->assign('foto1', $imgsrc1);
$smarty->assign('foto2', $imgsrc2);
$smarty->assign('foto3', $imgsrc3);
…然后您可能想将smarty模板更改为

{html_image file=$foto1}
{html_image file=$foto2}
{html_image file=$foto3}
这简化了您手头的任务,因为您让smarty来发现实际的图像大小

生成的html代码如下所示:

<img width="230" height="250" alt="" src="/path/to/your_image1.jpg">
<img width="630" height="850" alt="" src="/path/to/your_image2.jpg">
<img width="130" height="150" alt="" src="/path/to/your_image3.jpg">
$newArray = array();
$imagesArray = [$imgsrc1,$imgsrc2];
for(i = 0;i<count($imagesArray);$i++;){
   list($width, $height) = getimagesize($imgsrc1);
    $newArray[$i]['src'] = $imgsrc1;
    $newArray[$i]['width'] = $width;
    $newArray[$i]['height'] = $height;
}
$smarty->assign('imagesArray', $newArray);
    {foreach from=$imagesArray key=myId item=i}
        <img src='{$i.src}' border='1' width="{$i.width}" height="{$i.height}">
    {/foreach}
smarty模板代码:

{foreach $images as $img} 
    <img src='{$img.path}' border='1' width="{$img.wdth}" height="{$img.hght}">
{/foreach}
{foreach$images as$img}
{/foreach}
请记住,我在这里使用数组只是为了更好地可视化依赖关系。这当然可以在不使用数组的情况下完成。

在php循环中,如下所示:

<img width="230" height="250" alt="" src="/path/to/your_image1.jpg">
<img width="630" height="850" alt="" src="/path/to/your_image2.jpg">
<img width="130" height="150" alt="" src="/path/to/your_image3.jpg">
$newArray = array();
$imagesArray = [$imgsrc1,$imgsrc2];
for(i = 0;i<count($imagesArray);$i++;){
   list($width, $height) = getimagesize($imgsrc1);
    $newArray[$i]['src'] = $imgsrc1;
    $newArray[$i]['width'] = $width;
    $newArray[$i]['height'] = $height;
}
$smarty->assign('imagesArray', $newArray);
    {foreach from=$imagesArray key=myId item=i}
        <img src='{$i.src}' border='1' width="{$i.width}" height="{$i.height}">
    {/foreach}
$newArray=array();
$imagesArray=[$imgsrc1,$imgsrc2];
对于(i=0;iassign('imagesArray',$newArray);
然后在smarty循环中,如下所示:

<img width="230" height="250" alt="" src="/path/to/your_image1.jpg">
<img width="630" height="850" alt="" src="/path/to/your_image2.jpg">
<img width="130" height="150" alt="" src="/path/to/your_image3.jpg">
$newArray = array();
$imagesArray = [$imgsrc1,$imgsrc2];
for(i = 0;i<count($imagesArray);$i++;){
   list($width, $height) = getimagesize($imgsrc1);
    $newArray[$i]['src'] = $imgsrc1;
    $newArray[$i]['width'] = $width;
    $newArray[$i]['height'] = $height;
}
$smarty->assign('imagesArray', $newArray);
    {foreach from=$imagesArray key=myId item=i}
        <img src='{$i.src}' border='1' width="{$i.width}" height="{$i.height}">
    {/foreach}
{foreach from=$imagesArray key=myId item=i}
{/foreach}

没有测试代码,可能有一些小的语法问题。

为了理解您的问题,让我用我的话重新表述一下:您希望宽度和高度与变量中的图像一致
$fotoX
?是的,还请让我知道如何分配它,我明白了您的意思。这不仅仅是显示图像。我想控制它们的大小。例如,无论上传文件的原始属性如何,image1的固定大小必须为230 x 250,image2的固定大小必须为6300 x 850,image3的固定大小必须为130 x 150。这是否解决了您的问题:$imagesArray=array('0'=>array('src'=>$imgsrc1,'width'=>'200','height'=>'300'),'1'=>array('src'=>$imgsrc2,'width'=>'400','height'=>'500'));然后在smarty中循环它。我认为smarty不会让人弄乱图像属性;它只显示本机的高度和宽度。有一些插件允许图像大小调整;我正在检查。我希望能够控制属性(高度和宽度)图像的。已使用我在查询中发布的代码显示其本机大小的图像。我希望为每个图像指定一个固定的高度和宽度属性。在这种情况下,请在您的问题中对此进行澄清(如果尚未这样做)。我稍后会更新我的答案。我认为smarty不会让人弄乱图像属性;它只显示本机的高度和宽度。有一些插件允许图像大小调整;我正在检查。已更新,但我觉得这是一种相当静态的方法。