Javascript 从 php 生成的图像中获取图像大小
我发现这段代码可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的方法是:
The correct way to do this is:
如果它被 .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.