简体   繁体   中英

Getting images size using php or javascript / jquery - getimagesize fails

I'm using a really good lightbox script but it needs me to output the image sizes. The site I'm using is php and can resize images as they are uploaded. It does not store the images sizes in the database.

The code needs to be displayed as follows.

<a href="http://www.fullurl.com/image.jpg" data-original-url="http://www.fullurl.com/image.jpg" data-original-width="123" data-original-height="456">
    <img src="http://www.fullurl.com/thumbnail-image.jpg" alt="product name" />
</a>

I've tried using getimagesize but it only works on images that have not been resized by the system. I think they are missing some vital info as images that have uploaded but not resized work fine. There are currently about 1200 images already uploaded so redoing them is not an option.

My current method is using php as follows.

<?php
$image1 = 'http://www.fullurl.com/thumbnail-image.jpg';
list($width, $height) = getimagesize($image1);
 ?>

<a href="http://www.fullurl.com/image.jpg" data-original-url="http://www.fullurl.com/image.jpg" data-original-width="123" data-original-height="456">
    <img src="http://www.fullurl.com/thumbnail-image.jpg" alt="product name" />
</a>

If I use the following I get bool(false) as the output.

$path1 = 'http://www.fullurl.com/image.jpg';
$vals_arr1 = getimagesize($path1);
echo "<pre>";var_dump($vals_arr1);echo "</pre>";

I'm not a programmer. My question is how can I output the image info as below? I'm happy to try alternative php, jquery / javascript as is needed. Ideally it needs to be simple and fast.

<a href="http://www.fullurl.com/image.jpg" data-original-url="http://www.fullurl.com/image.jpg" data-original-width="123" data-original-height="456">

This works

$path1 = 'https://jpeg.org/images/jpeg-home.jpg';
$vals_arr1 = getimagesize($path1);
echo "<pre>";var_dump($vals_arr1);echo "</pre>";

The bug must be somewhere else. Check the URLs! Are they valid? And check if allow_url_fopen is set to true on your server.

Else this can help: https://stackoverflow.com/a/25231517/4916265

As noted by james dot on the PHP docs website , getimagesize will download the entire image before it checks for the requested information. This is extremely slow on large images that are accessed remotely. Since the width/height is in the first few bytes of the file, there is no need to download the entire file. He wrote a function to get the size of a JPEG by streaming bytes until the proper data is found to report the width and height.

The way he wrote it will allow a remote image.

The code can be found here: http://php.net/manual/en/function.getimagesize.php#88793

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM