调整大小后图像不显示
我有以下 php 代码,为简洁起见,已对其进行了剪裁。 在声明 $lastImg 后,我在代码中遇到了问题,特别是 size 似乎从未被分配任何值,因此输出宽度和输出高度保持空白。 我一直无法找出造成这种情况的原因。
pic_url 只是数据库中的一个字符串,并且肯定指向有效的图像。 当我在没有调整大小的代码的情况下运行页面时,图像显示完美。
<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"];
else
die("You should have a 'cmd' parameter in your URL");
$pk = $_GET["pk"];
$con = mysql_connect("localhost", "someuser", "notreal");
if(!$con)
{
die('Connection failed because of' . mysql_error());
}
mysql_query('SET NAMES utf8');
mysql_select_db("somedb",$con);
if($cmd == "GetAuctionData")
{
$sql="SELECT * FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";
$sql2="SELECT ARTICLE_DESC FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";
$htmlset = mysql_query($sql2);
$row2 = mysql_fetch_array($htmlset);
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$columns = array('FINISHED', 'WATCH', 'PRIVATE_AUCTION', 'REVISED', 'PAYPAL_ACCEPT', 'PRE_TERMINATED', 'PIC_XXL', 'PIC_DIASHOW');
foreach($columns as $column)
{
$$column = $row[$column] ? 'YES' : 'NO';
}
$lastImg = $row['PIC_URL'];
$maxWidth = 250;
$maxHeight = 300;
$size = getimagesize($_SERVER['DOCUMENT_ROOT'] . $lastImg);
var_dump($size);
echo "SIZE = ".$size."";
if ($size) {
$imageWidth = $size[0];
$imageHeight = $size[1];
$wRatio = $imageWidth / $maxWidth;
$hRatio = $imageHeight / $maxHeight;
$maxRatio = max($wRatio, $hRatio);
else {
die(print_r(error_get_last())); }
else if ($maxRatio > 1) {
$outputWidth = $imageWidth / $maxRatio;
$outputHeight = $imageHeight / $maxRatio;
} else {
$outputWidth = $imageWidth;
$outputHeight = $imageHeight;
}
}
echo "<h1>".$row['ARTICLE_NAME']."</h1>
<div id='rightlayer'>
<img src='".$lastImg."' width='".$outputWidth."' height='".$outputHeight."'>
</div>";
}
}
mysql_free_result($result);
I have the following php code, which has been snipped for brevity. I am having trouble with the code after $lastImg is declared, specifically size never seems to get assigned any value, and the hence outputwidth and outputheight remain blank. I have been unable to spot the cause of this.
pic_url is just a string in the database, and definitely points to a valid image. When I run my page without the code to resize, the image is displayed perfectly.
<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"];
else
die("You should have a 'cmd' parameter in your URL");
$pk = $_GET["pk"];
$con = mysql_connect("localhost", "someuser", "notreal");
if(!$con)
{
die('Connection failed because of' . mysql_error());
}
mysql_query('SET NAMES utf8');
mysql_select_db("somedb",$con);
if($cmd == "GetAuctionData")
{
$sql="SELECT * FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";
$sql2="SELECT ARTICLE_DESC FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";
$htmlset = mysql_query($sql2);
$row2 = mysql_fetch_array($htmlset);
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$columns = array('FINISHED', 'WATCH', 'PRIVATE_AUCTION', 'REVISED', 'PAYPAL_ACCEPT', 'PRE_TERMINATED', 'PIC_XXL', 'PIC_DIASHOW');
foreach($columns as $column)
{
$column = $row[$column] ? 'YES' : 'NO';
}
$lastImg = $row['PIC_URL'];
$maxWidth = 250;
$maxHeight = 300;
$size = getimagesize($_SERVER['DOCUMENT_ROOT'] . $lastImg);
var_dump($size);
echo "SIZE = ".$size."";
if ($size) {
$imageWidth = $size[0];
$imageHeight = $size[1];
$wRatio = $imageWidth / $maxWidth;
$hRatio = $imageHeight / $maxHeight;
$maxRatio = max($wRatio, $hRatio);
else {
die(print_r(error_get_last())); }
else if ($maxRatio > 1) {
$outputWidth = $imageWidth / $maxRatio;
$outputHeight = $imageHeight / $maxRatio;
} else {
$outputWidth = $imageWidth;
$outputHeight = $imageHeight;
}
}
echo "<h1>".$row['ARTICLE_NAME']."</h1>
<div id='rightlayer'>
<img src='".$lastImg."' width='".$outputWidth."' height='".$outputHeight."'>
</div>";
}
}
mysql_free_result($result);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不得不说,看起来 $row['PIC_URL'] 可能没有指向有效的图像,这是 getimagesize 失败的唯一原因。 AUCTIONS 中的数据库结构是什么样的?
I'd have to say it looks like $row['PIC_URL'] might not point to a valid image, thats the only reason getimagesize would fail. What does your database structure in AUCTIONS look like?
首先,要获得正确的错误消息,请将代码更改为:
之后,我们可以查看错误并尝试进一步调查。
First, to get the right error message, change your code to:
After that, we can look at the error and try to investigate further.
第一。 做一个
在你的下面
,看看数组 PIC_URL 是否设置在那里
,第二你真的应该做一个安全的查询。
应该
与其他 SQL 查询相同。
1st. do a
just below your
And see if the array PIC_URL is set there
2nd you really should do a safe Query.
Should be
Same with the other SQL query.
尝试:
看看 $row['PIC_URL'] 中是否有任何内容。
Try:
to see if you have anything in $row['PIC_URL'].
您有一些相互冲突的事情:
getimagesize
需要文件名,而需要 URL。
您可以尝试这样的操作:
[编辑] 抱歉。
DOCUMENT_ROOT
不正确。You have a few things that conflict with each other:
getimagesize
expects a filename while<img>
expects a URL.You might try something like this:
[EDIT] Sorry.
DOCUMENT_ROOT
wasn't right.