当前位置:   article > 正文

php获取图片格式(图片后缀)_php 获取图片后缀

php 获取图片后缀

getimagesize(真实格式,支持base64

代码:

<?php
/*
@desc:获取图片真实后缀
@param   name    文件名
@return  suffix  文件后缀
*/   
function getimgsuffix($name) {
$info = getimagesize($name);
$suffix = false;
if($mime = $info['mime']){
    $suffix = explode('/',$mime)[1];
}
return $suffix;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试:

$suffix = getimgsuffix('http://192.168.8.81/public/image/01.jpg');
echo $suffix;
  • 1
  • 2

输出:

jpeg
  • 1

二进制方法(真实格式)

代码:

<?php
/*
@desc:获取文件真实后缀
@param   name    文件名
@return  suffix  文件后缀
*/   
function getfilesuffix($name) {
$file = fopen($name, "rb");
$bin = fread($file, 2); // 只读2字节
fclose($file);
$info = @unpack("C2chars", $bin);
$code = intval($info['chars1'] . $info['chars2']);
$suffix = "unknow";
if($code == 255216){
    $suffix = "jpg";
}elseif($code == 7173){
    $suffix = "gif";
}elseif($code == 13780){
    $suffix = "png";
}elseif($code == 6677){
    $suffix = "bmp";
}elseif($code == 7798){
    $suffix = "exe";
}elseif($code == 7784){
    $suffix = "midi";
}elseif($code == 8297){
    $suffix = "rar";
}elseif($code == 7368){
    $suffix = "mp3";
}elseif($code == 0){
    $suffix = "mp4";
}elseif($code == 8273){
    $suffix = "wav";
}
return $suffix;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

测试:

$suffix = getfilesuffix('http://192.168.8.81/public/image/01.jpg');
echo $suffix;
  • 1
  • 2

输出:

jpg
  • 1

strrchr strrpos substr

<?php
    $filename = "phpddt.jpg";
    echo substr(strrchr($filename,"."),1);
?>
  • 1
  • 2
  • 3
  • 4

<?php
    $filename = "phpddt.jpg";
    echo substr($filename,strrpos($filename,".")+1);
?>
  • 1
  • 2
  • 3
  • 4

explode array_pop

<?php
    $filename = "phpddt.jpg";
    $arr = explode(".",$filename);
    echo array_pop($arr);
?>
  • 1
  • 2
  • 3
  • 4
  • 5

pathinfo

<?php
    $filename = "phpddt.jpg";
    $info = pathinfo($filename);
    echo $info['extension'];
?>
  • 1
  • 2
  • 3
  • 4
  • 5

<?php
    $filename = "phpddt.jpg";
    echo pathinfo($filename,PATHINFO_EXTENSION);
?>
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/534896
推荐阅读
相关标签
  

闽ICP备14008679号