调整大小后图像不显示

发布于 2024-07-13 07:32:31 字数 2099 浏览 6 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妄想挽回 2024-07-20 07:32:31

我不得不说,看起来 $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?

桜花祭 2024-07-20 07:32:31

首先,要获得正确的错误消息,请将代码更改为:

$size = getimagesize($lastImg);
echo "SIZE = ".$size."";
if ($size) {
    $imageWidth  = $size[0];
    $imageHeight = $size[1];
    $wRatio = $imageWidth / $maxWidth;
    $hRatio = $imageHeight / $maxHeight;
    $maxRatio = max($wRatio, $hRatio);

    if ($maxRatio > 1) {
        $outputWidth = $imageWidth / $maxRatio;
        $outputHeight = $imageHeight / $maxRatio;
    } else {
        $outputWidth = $imageWidth;
        $outputHeight = $imageHeight;
    }
} else {
    die(print_r(error_get_last()));
}

之后,我们可以查看错误并尝试进一步调查。

First, to get the right error message, change your code to:

$size = getimagesize($lastImg);
echo "SIZE = ".$size."";
if ($size) {
    $imageWidth  = $size[0];
    $imageHeight = $size[1];
    $wRatio = $imageWidth / $maxWidth;
    $hRatio = $imageHeight / $maxHeight;
    $maxRatio = max($wRatio, $hRatio);

    if ($maxRatio > 1) {
        $outputWidth = $imageWidth / $maxRatio;
        $outputHeight = $imageHeight / $maxRatio;
    } else {
        $outputWidth = $imageWidth;
        $outputHeight = $imageHeight;
    }
} else {
    die(print_r(error_get_last()));
}

After that, we can look at the error and try to investigate further.

不必你懂 2024-07-20 07:32:31

第一。 做一个

print_r($row) 

在你的下面

while ($row = mysql_fetch_array($result))

,看看数组 PIC_URL 是否设置在那里

,第二你真的应该做一个安全的查询。

$sql="SELECT * FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";

应该

$sql = sprintf("SELECT * FROM AUCTIONS WHERE ARTICLE_NO = %d", mysql_real_escape_string($pk));

与其他 SQL 查询相同。

1st. do a

print_r($row) 

just below your

while ($row = mysql_fetch_array($result))

And see if the array PIC_URL is set there

2nd you really should do a safe Query.

$sql="SELECT * FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";

Should be

$sql = sprintf("SELECT * FROM AUCTIONS WHERE ARTICLE_NO = %d", mysql_real_escape_string($pk));

Same with the other SQL query.

挽心 2024-07-20 07:32:31

尝试:

$lastImg = $row['PIC_URL'];
var_dump($row);
die();

看看 $row['PIC_URL'] 中是否有任何内容。

Try:

$lastImg = $row['PIC_URL'];
var_dump($row);
die();

to see if you have anything in $row['PIC_URL'].

笨死的猪 2024-07-20 07:32:31

您有一些相互冲突的事情:

$size = getimagesize($lastImg);

echo "<img src='".$lastImg."' ...>"

getimagesize 需要文件名,而 需要 URL。

您可以尝试这样的操作:

$root = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);

$size = getimagesize($root . $lastImg);

[编辑] 抱歉。 DOCUMENT_ROOT 不正确。

You have a few things that conflict with each other:

$size = getimagesize($lastImg);

echo "<img src='".$lastImg."' ...>"

getimagesize expects a filename while <img> expects a URL.

You might try something like this:

$root = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);

$size = getimagesize($root . $lastImg);

[EDIT] Sorry. DOCUMENT_ROOT wasn't right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文