【问题标题】:Editing img src style with variables based on specific resolutions使用基于特定分辨率的变量编辑 img src 样式
【发布时间】:2018-06-05 22:37:16
【问题描述】:

我正在尝试做一些我认为会简单得多的事情,但我遇到了问题。

我网页上的默认标题大小是 1500x500,而在较低分辨率(笔记本电脑)上,这个 1500 显然是个问题。我有一个宏需要这个标头分辨率,并且该宏运行并以 1600x900 屏幕分辨率编写。

所以思考过程是这样的:

  • 获取屏幕分辨率。 (完成)

  • 为 1600x900 设置 IF 屏幕分辨率,当我具有该分辨率并执行某项操作时,它会识别出来,否则,执行其他操作。 (完成)

  • 在 if 中更改图像宽度/高度。 (问题)

所以实际做的事情我遇到了问题。

<center>
    <?php
        session_start();
        if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height']))
            {
                echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];
            } 
            else 
            if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) 
            {
                echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
            }
    ?>

    <?php if ($_SESSION['screen_width'] == 1600 && $_SESSION['screen_height'] == 900)
    {
         //Don't change style, this is a resolution we want precise width on
        $width1 = 500 +"px";
        $height1 = 50 +"px";
    }
    else
    {
          //Change style, any other resolution we go for 100%
        $width1 = 100 +"%";
        $height1 = 100 +"%";
    }
    ?>

  //This is the header/image in question. 
  src="https://i.imgur.com/rGEjK9e.png" style.width = width, style.height = 
  height1;>
    </center>

  //This is the old header image code.
 <center>
<img src="https://i.imgur.com/C4KSU7g.png" alt="" style="width:1500px;height:500px;”>
</center>

我让它回显以表明我的 if 有效并且它知道我的屏幕尺寸,但我现在想让它只更改 1 个特定的图像宽度/高度作为该 IF 的结果。

我并没有真正用过这种语言。

【问题讨论】:

  • 最好在客户端使用 CSS 媒体查询。是否有一些要求应该在服务器端完成?
  • 我们所有的网站都是服务器端的。不知道从客户端从哪里开始,并且想法是一旦它起作用,它将被用于其他站点上的脚本。我不知道这是否适用于许多客户的客户端?

标签: php html image src


【解决方案1】:

搞定了。完整代码:

    <center>
<?php
    session_start();
    if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height']))
    {
        //echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];
    }
    else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) 
    {
        $_SESSION['screen_width'] = $_REQUEST['width'];
        $_SESSION['screen_height'] = $_REQUEST['height'];
        header('Location: ' . $_SERVER['PHP_SELF']);
    } 
    else
    {
        //echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
    }
?>

<?php if ($_SESSION['screen_width'] == 1600 && $_SESSION['screen_height'] == 900)
{
     //Don't change style, this is a resolution we want precise width on
    $width1 = "1500"."px";
    $height1 = "500"."px";
    //echo $width1;
    //echo $height1;
    //echo "Header won't change";
}
else
{
      //Change style, any other resolution we go for 100%
    $width1 = "100"."%";
    $height1 = "100"."%";
    //echo $width1;
    //echo $height1;
    //echo "Header will change";
}
$text = "<img src='https://i.imgur.com/rGEjK9e.png' style='width:$width1;height:200px></center>";

preg_match_all("/<img.*>/",$text,$out);
foreach($out as $t1)
{
    foreach($t1 as $img)
    {
        preg_match("/.*height:(.*?);width:(.*?);.*/",$img,$out2);
        $height = $out2[1];
        $width = $out2[2];
        $text = str_replace($out2[0],str_replace("<img","<img width='" . $width1 . "' height='" . $height1 . "'",$out2[0]),$text);
    }
}
echo $text;
?>

修复了一些问题并找到了这个 str_replace。将您有问题的图片链接设置为文本并通过此检查器,回显文本,它会以某种方式工作。不知道为什么我不能自己设置高度/宽度,但它有效,所以我没有抱怨。

我的变量没有添加之前的“px”和“%”,+显然不起作用,你必须使用.,即“100”。“px”

现在标题可以响应除 1600x900 之外的所有分辨率,这是理想的。无论如何,在铬上。快速浏览一下边缘,它似乎无法识别分辨率。对此代码的任何改进持开放态度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2019-05-02
    相关资源
    最近更新 更多