简体   繁体   中英

PHP echo syntax, to scale an image

I have the following syntax:

echo "<img src=\"images2/" . $row['image']  . "\" alt=\"\" /><br />"; } 

I would like to a give a specific height and width for the image, but I can't to do it.

You can specify dimensions directly in your HTML <img> tag:

echo "<img src=\"images2/\" . $row['image']  . "\" alt=\"\" height=\"100\" width=\"100\" /><br />";

Note that your posted code has a closing curly brace ( } ) following your echo statement, which will cause a PHP error if there's no corresponding opening curly brace. You're also incorrectly escaping the double-quotes after images2/ , which will result in invalid markup.

A better approach would be to enclose your entire markup in single quotes so that you don't have to escape the double quotes enclosed within:

echo '<img src="images2/' . $row['image'] . '" alt="" height="100" width="100" /><br />';

EDIT:

For modern day markup (depending on your requirements), it's considered vastly preferable to modify image dimensions in CSS, rather than in your <img> tag. You may want to consider implementing something akin to the following:

<style type="text/css">
    img {
        width: 100px;
        height: 100px;
    }

    img.large {
        width: 200px;
        height: 200px;
    }
</style>

<?php

    echo '<img src="images2/' . $row['image'] . '" alt="" /><br />'; // no class attribute, so will default to 100x100  
    echo '<img src="images2/' . $row['image'] . '" alt="" class="large" /><br />'; // class attribute is `large`, so will rescale to 200x200

这应该工作

echo '<img src="images2"'.$row['image'].'\ alt="" width="" height="" /><br />'; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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