在 PHP 中设置图像的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7881726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting size of an image in PHP
提问by Sharpzain120
I am trying to set size of an image in PHP but it's not working..
我正在尝试在 PHP 中设置图像的大小,但它不起作用..
echo "<img src=".$media."width=200 height=200";
$media is the src link. Without the width and height attributes, it works perfectly well. I think that 200
must be enclosed in double quotations but I am unable to do that. Any ideas on how to solve this problem?
$media 是 src 链接。没有宽度和高度属性,它工作得很好。我认为这200
必须用双引号括起来,但我无法做到这一点。关于如何解决这个问题的任何想法?
回答by Rob W
width
is currently concatenated with your file name. Use:
width
当前与您的文件名连接。用:
echo '<img src="'.$media.'" width="200" height="200" />';
The />
closing tag is necessary to correctly render your image element.
The quotes around tag names are recommended. Omitting these quotes will only cause issues when:
该/>
结束标记是要正确渲染你的图像元素。
建议在标签名称周围使用引号。省略这些引号只会在以下情况下导致问题:
- The attribute value contain spaces, or
- You forgot to add a space after the attribute value
- 属性值包含空格,或
- 您忘记在属性值后添加空格
回答by Marc B
回答by Jules
That is because that is not proper HTML.
那是因为那不是正确的 HTML。
In your code you'd get something like:
在您的代码中,您会得到如下内容:
<img src=image.jpgwidth=200 height=200
And what you need is:
而你需要的是:
<img src="image.jpg" width="200" height="200" />
So do:
所以这样做:
echo '<img src="' . $media . '" width="200" height="200" />';
回答by Logan Serman
echo '<img src="' . $media . '" width="200" height="200" />';
echo '<img src="' . $media . '" width="200" height="200" />';
回答by Kibbee
You should be able to make it work with the following code
您应该能够使用以下代码使其工作
echo "<img src=\"".$media."\" width=200 height=200>";