简体   繁体   English

使用php或javascript / jquery获取图像大小-getimagesize失败

[英]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. 我使用的网站是php,可以在上传图片时调整图片的大小。 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. 我尝试使用getimagesize,但它仅适用于尚未由系统调整大小的图像。 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. 当前已上传约1200张图像,因此无法重做。

My current method is using php as follows. 我当前的方法如下使用php。

<?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. 如果使用以下命令,则会得到bool(false)作为输出。

$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. 我很乐意根据需要尝试替代的php,jquery / javascript。 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! 检查URL! Are they valid? 他们有效吗? And check if allow_url_fopen is set to true on your server. 并检查您的服务器上allow_url_fopen是否设置为true

Else this can help: https://stackoverflow.com/a/25231517/4916265 否则,这可以帮助您: 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. PHP docs网站上的james dot所指出的,getimagesize将在检查所需信息之前下载整个图像。 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. 他编写了一个函数,可通过流传输字节来获取JPEG的大小,直到找到合适的数据来报告宽度和高度为止。

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 可以在这里找到代码: http : //php.net/manual/zh/function.getimagesize.php#88793

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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