Search code examples
phpgetimagesize

PHP - how to get mime type from getimagesize instead of a number


I am using PHP's getimagesize() function to get the height/width/type from an image URL. Currently, for a jpg file, $type is returning 2. How can I get it to return the actual mime type instead?

<?php
    list($width, $height, $type) = getimagesize('https://loremflickr.com/cache/resized/65535_51564349748_2ceac19a11_z_640_360_nofilter.jpg');
    echo 'Width: ' . $width . ' Height: ' . $height . ' Type: ' . $type;

This returns: Width: 640 Height: 360 Type: 2

Expected: Width: 640 Height: 360 Type: image/jpeg


Solution

  • The documentation is pretty clear about what the function returns. Looks like you need to assign the entire return value into a local variable (e.g. $size = getimagesize(...), and then get the mime type from that like $size['mime'].