Javascript get image size from php generated image

I found this code to get the image size in 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 fine, but I need to get the size of the protected image; do i use image.php to access the image? id = IMAGE_ID, and it works because on this page I check permissions and send the image back. But when I put this link in a javascript function to get its size, it does not work. Any help (if I put a direct link to an image that it does not work because it is locked in a .htaccess file)?

The folder containing the images also contains the .htaccess file, which is available for denny access. 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 ok $filename = "images\\fotos\\" . $imgl; $image = imagecreatefromjpeg($filename); header('Content-type: image/jpeg'); imagejpeg($image, null, 100); imagedestroy($image); 
+4
source share
2 answers

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; 
+2
source

If it is blocked by .htaccess, there is nothing you can do about it. This means that it will not be accessible from outside the server under any circumstances.

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

+2
source

Source: https://habr.com/ru/post/1397176/


All Articles