4

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);
3
  • you have to allow browser open imgSrc, otherwise the js cannot get anything, also, in real server, you have to consider delay as well Commented Feb 18, 2012 at 11:30
  • "Protected" how? Through a password dialog? Commented Feb 18, 2012 at 11:30
  • It probably checks for a required cookie or session variable. Commented Feb 18, 2012 at 11:33

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I've already changed in my code (the problem still persists, but now it only show the size if it succeded to get to the image).
I found a way to do it with your code. Instead of returning the value, that didn't work, I put the resize code (the reason why I needed the original size) directly into the onload function. As soon as the image is loaded with the original size, it resizes to the excepted size. Thank you so much for your answer.
2

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.

2 Comments

He already has a special PHP file as described (unless he edited the question?)
Actually, that would work. However, I was thinking it would be easier to change something in the image.php file in order to allow js to get the image size; I don't understant why it can't. I'm going to add the image.php code in the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.