4

我发现这段代码可以在 javascript 上获取图像大小:

function getImgSize(imgSrc)
{
    var newImg = new Image();
    newImg.src = imgSrc;
    var height = newImg.height;
    var width = newImg.width;
    alert ('The image size is '+width+'*'+height);
}

它工作得很好,但我需要获得受保护图像的大小;为了访问图像,我使用页面 image.php?id=IMAGE_ID,它可以工作,因为在这个页面中我检查了权限并将图像发回。但是当我把这个链接放在javascript函数上时,为了得到它的大小,它就不起作用了。任何帮助(如果我放置图像的直接链接,它也不起作用,因为它在 .htaccess 文件中被阻止)?

包含图像的文件夹还包含一个 .htaccess 文件,它可以访问所有内容。为了获取图像,我使用了这个 PHP 页面:

图片.php:

//check if the user has permission
//if not, show a image with the text 'no permission'
//if it's ok
$filename = "images\\fotos\\" . $imgl;
$image = imagecreatefromjpeg($filename);
header('Content-type: image/jpeg');
imagejpeg($image, null, 100);
imagedestroy($image);
4

2 回答 2

2

正确的方法是:

var newImg = new Image();

newImg.onload = function ()
{
    var height = newImg.height;
    var width = newImg.width;
    alert ('The image size is '+width+'*'+height);
};

newImg.src = imgSrc;
于 2012-02-18T11:31:23.403 回答
2

如果它被 .htaccess 阻止,您将无能为力。这意味着在任何情况下都无法从服务器外部访问它。

您可以解决您编写获取图像大小的特殊 php 文件然后通过 AJAX 调用此文件的问题。但是,这需要额外的服务器资源。

于 2012-02-18T11:29:53.747 回答