Justin
Justin

Reputation: 87

Detect image type from file path and file name

I'm trying to echo an image from a folder called avatar_50x50, with the name of the file being the username. Although the extension will always be an image, how will I determine what file type it is?

<img src="../images/users/avatar_50x50/<?php echo $sel_user['username']; ?>"

Upvotes: 0

Views: 687

Answers (2)

MrSil
MrSil

Reputation: 618

Use http://php.net/exif_imagetype

And than use "switch" operator to find right image ext.

$type = exif_imagetype($image);
switch($type) {
  case IMAGETYPE_GIF:
    $ext = '.gif';
  // etc...
}

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39704

you can use glob() and take first element from array if you are sure there is only one image with name as username:

<?php
    $file = glob('../images/users/avatar_50x50/'.$sel_user['username'].'.*');
    echo '<img src="../images/users/avatar_50x50/'.$file[0].'" alt="" />';
?>

Upvotes: 1

Related Questions