代码之家  ›  专栏  ›  技术社区  ›  Sandeepan Nath

getimagesize无法使用PHP版本5.3.29获取远程webp文件的大小

  •  1
  • Sandeepan Nath  · 技术社区  · 10 年前

    以下是代码片段-

    list($campaign_image_width, $campaign_image_height, $campaign_image_type, $campaign_image_attr)=getimagesize($campaign_image);
    

    其中$campaign_image包含第三方图像的url。

    问题

    此url的$campaign_image_width为空-

    https://lh3.googleusercontent.com/VRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0=h256-rw

    我不确定这是由于不支持的格式导致的getimagesize()的限制,还是由于图像的可访问性问题。

    注释-

    -结尾处的=h256 rw似乎告诉服务器返回不同大小的图像版本。

    -我发现,如果我尝试使用firefox浏览器打开文件,它不会显示图像,而是要求下载webp文件(谷歌似乎是一种图像格式)。 谷歌chrome通常会打开文件并显示图像。

    2 回复  |  直到 6 年前
        1
  •  4
  •   h2ooooooo    10 年前

    由于您的服务器已经在下载该文件,您不妨自己下载(如果问题是它无法正确地为webp下载)。您可以使用GD方法轻松做到这一点 imagecreatefromwebp 具有 imagesx imagesy :

    <?php
    
    $url = 'https://lh3.googleusercontent.com/VRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0=h256-rw';
    
    $img = imagecreatefromwebp($url);
    
    $width = imagesx($img);
    $height = imagesy($img);
    
    var_dump($width, $height);
    

    注: imagecreatefromwebp() PHP 5.5中首次引入,所以请确保安装了GD扩展的最低版本是5.5。

    如果可能,您可以在服务器上安装谷歌自己的webp转换器作为二进制文件:

    https://developers.google.com/speed/webp/docs/compiling#building

    在本例中,您运行的是基于Fedora的Amazon linux,因此使用 yum 作为包管理器,您应该能够运行以下命令:

    sudo yum install libwebp;
    

    一旦您安装了它,您可以确保 safemode 通过使用支持二进制文件 safe_mode_exec_dir 以及以下执行方法之一:

    一旦您运行了例如JPG的转换,就可以运行常用的PHP工具来获取图像尺寸:

    $hnd = imagecreatefromjpeg('convertedImage.jpg');
    
    $width = imagesx($hnd);
    $height = imagesy($hnd);
    
        2
  •  1
  •   ksimka    10 年前

    我认为这是因为不支持的格式。尝试 imagetypes 了解支持的内容。

    $bits = imagetypes();
    

    退房 this post ,这可能会有所帮助。安装后,您可以

    $image = new Imagick($originalFilepath);
    $origImageDimens = $image->getImageGeometry(); 
    $origImgWidth = $origImageDimens['width']; 
    $origImgHeight = $origImageDimens['height'];   
    
    推荐文章