Javascript 从 php 生成的图像中获取图像大小

发布于 2025-01-07 00:41:33 字数 830 浏览 0 评论 0原文

我发现这段代码可以在 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 页面:

Image.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);

I found this code to get the image size on 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);
}

It works perfectly, but I need to get the size of a image that is protected; in order to access the image, I use the page image.php?id=IMAGE_ID, and it works, because in this page I check the permissions and send the image back. But when I put this link on the javascript function, in order to get its size, it doesn't work. Any help (if I put the direct link of the image it does'n work neither, because it is blocked in the .htaccess file)?

The folder that contains the images also contains a .htaccess file that denny access for everthing. To get the image, I use this PHP page:

Image.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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

耳钉梦 2025-01-14 00:41:33

正确的方法是:

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;

The correct way to do this is:

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;
念﹏祤嫣 2025-01-14 00:41:33

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

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

If it is blocked by .htaccess, you cannot do anything about it. That means it won't be accessible from outside the server under any circumstance.

You can solve the problem that you write special php file that gets the image size and then you call this file by AJAX. However, this requires aditional server resources.

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