0

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">
Legin76
  • 492
  • 3
  • 11
  • Typo? `$image1 = 'http://www.fullurl.com/thumbnail-image.jpg'];` should be `$image1 = 'http://www.fullurl.com/thumbnail-image.jpg';` – Jesse de gans Apr 06 '17 at 12:57
  • That's where I removed the code that inserted the image url. I've removed that from the example. Unfortunately not so simple. Thanks though. – Legin76 Apr 06 '17 at 13:31

2 Answers2

0

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

Community
  • 1
  • 1
JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • Yes I've checked with this.. I get the same when I manually put in the urls.. I also found that if I uploaded an image it was fine but when I used the built in resizer on the existing images it broke. The urls did not change. and the images still worked in the browsuer – Legin76 Apr 06 '17 at 13:28
0

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

Jesse de gans
  • 1,432
  • 1
  • 14
  • 27