簡體   English   中英

Javascript從php生成的圖像中獲取圖像大小

[英]Javascript get image size from php generated image

我發現此代碼可以在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);

如果它被.htaccess阻止,你就無法做任何事情。 這意味着在任何情況下都無法從服務器外部訪問它。

您可以解決您編寫獲取圖像大小的特殊php文件然后通過AJAX調用此文件的問題。 但是,這需要aditional服務器資源。

正確的方法是:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM