【问题标题】:Laravel intervention image not returning an extensionLaravel 干预图像不返回扩展名
【发布时间】:2014-06-05 15:21:35
【问题描述】:

当我尝试使用带有 laravel 的干预图像库保存图像时,它可以工作,但是缺少扩展。

当我死并转储 Image::make() 方法的输出时,我得到了这个:

  object(Intervention\Image\Image)[304]
  public 'resource' => resource(9, gd)
  public 'type' => int 2
  public 'width' => int 480
  public 'height' => int 640
  public 'dirname' => string '/tmp' (length=4)
  public 'basename' => string 'phpJHlKbK' (length=9)
  public 'extension' => null
  public 'filename' => string 'phpJHlKbK' (length=9)
  public 'mime' => string 'image/jpeg' (length=10)
  protected 'original' => null
  public 'encoded' => null

正在上传的文件有一个扩展名,但我无法访问它,因为它认为该文件不存在。有什么想法吗?

【问题讨论】:

    标签: image file-upload laravel


    【解决方案1】:

    干预图像文档说:

    The current filename extension of the image file, if instance was created from file.  
    

    因此建议将“mime”用于“未知”可能是原始发布数据图像文件的方法:

    $mime = $image->mime();  //edited due to updated to 2.x
    if ($mime == 'image/jpeg')
        $extension = '.jpg';
    elseif ($mime == 'image/png')
        $extension = '.png';
    elseif ($mime == 'image/gif')
        $extension = '.gif';
    else
        $extension = '';
    

    【讨论】:

      【解决方案2】:

      当您使用 PHP 上传文件时,它们会被重命名为 /tmp/phpJHlKbK,因此没有可用的扩展名。

      不过,Intervention Image 2.0(上周发布)会在保存时检查 MIME 类型(如果没有可用的扩展名)。

      【讨论】:

      • 可以通过$_FILES['uploaded_file']['name']获取上传文件的文件名并提取扩展名。
      猜你喜欢
      • 2015-09-15
      • 2017-02-20
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 2014-12-13
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多