Laravel intervention image not returning an extens

2019-06-14 15:33发布

问题:

When I try to save an image using the intervention image library with laravel it works however the extension is missing.

When I die and dump the output of the Image::make() method I get this:

  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

The file that is being uploaded has an extension yet I cannot access it as it believes one doesn't exist. Any ideas?

回答1:

Intervention Image doc says:

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

So suggested way to utilize 'mime' for 'unknown' maybe Raw post data image files:

$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:

When you're uploading files with PHP, they are renamed to something like this /tmp/phpJHlKbK, so there is no extension available.

However Intervention Image 2.0 (which was released last week) checks for the MIME type if no extension is available, while saving.