2

我正在编写一个 PHP 脚本来动态调整图像大小。图像 ID(来自 MySQL 数据库)是这样传入的:“http://localhost/getimage/1.htm”。

当我使用上面的 URL 直接进入脚本时,图像被完美地吐出。但是由于某种原因(仅在 Chrome 中),当我将该 URL 链接到标签时,它开始表现得很奇怪。当页面第一次加载图像加载正常,但随后加载栏旋转约 5 秒,图像突然消失,Chrome 显示“加载资源失败”错误。

有谁知道可能是什么原因造成的,如果是这样,如何阻止它?我认为它可能与 AdBlocker 有关,但我已禁用它并且它仍在发生。

干杯。

编辑:这是我正在使用的代码:

    header('Content-Type:'.$file['type']);
    header('Content-Length: ' . $file['bytes']);

    // Get size of original image
    list($o_width, $o_height) = getimagesize($file['src']);

    // Default width and height
    if (is_null($width)) {
        $width = $o_width;
    }

    if (is_null($height)) {
        $height = $width;
    }

    // Create image frame
    $image_p = imagecreatetruecolor($width, $height);

    // Generate image depending on source type
    switch ($file['type']) {
        case "image/jpeg":
        default:
            $image = imagecreatefromjpeg($file['src']);
            break;
        case "image/gif":
            $image = imagecreatefromgif($file['src']);
            break;
        case "image/png":
            $image = imagecreatefrompng($file['src']);
            break;
    }

    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $o_width, $o_height);

    // Output
    imagejpeg($image_p, null, 100);
    exit;

As you can see I am setting the content types. If it was the htm extension, confusing it, would that explain why the image loads correctly, and then unloads itself?

4

1 回答 1

1

this is not Chrome only, but IMHO it is wrong that you have this at the beginning: header('Content-Length: ' . $file['bytes']); because you don't know the actual file size, since you create the image on the fly with imagejpeg($image_p, null, 100); Try commenting out the header('Content-Length... line, it might fix it.

于 2012-05-01T10:46:44.543 回答