Home  >  Article  >  Backend Development  >  How to change the image suffix to jpg when uploading images in PHP

How to change the image suffix to jpg when uploading images in PHP

coldplay.xixi
coldplay.xixiOriginal
2020-09-01 15:24:083754browse

PHP在图片上传时改变图片后缀为jpg的方法:首先获取上传的图片后缀,并获取图片在电脑上的临时存储位置;然后获取临时存储文件的后缀;最后将后缀转换为jpg,代码为【case 'image/jpg'】。

How to change the image suffix to jpg when uploading images in PHP

【相关学习推荐:php图文教程

PHP在图片上传时改变图片后缀为jpg的方法:

1、为了统一图片部分的后缀,方面通过拼接地址的方式找到它。

比如只给个md5(id)的值,要通过它去找到对应的图片,如果图片后缀多种多样的话,不方便找到和管理它

2、也是为了安全。

      就算别人上传可执行文件到服务器或者在图片上动手脚,我们可以在转换的时候全部改成图片资源再重新保存,规避掉这方面的攻击。

3、具体转换实现

主要是使用php的GD库以及相关的函数来转换:

    if(!file_exists($file_path)) { //不存在才上传,存在的话,就直接用,不用再上传了
            $ext = $arr['type'];   //获取上传的图片后缀
            $filePath = $arr["tmp_name"];  //获取图片在电脑上的临时存储位置
            $filePath_suffix = substr($filePath,strripos($filePath,".")+1);  //获取临时存储文件的后缀
           if($filePath_suffix == 'tmp'){ // 如果读取的临时文件是tmp结尾,无法打开图片资源,则直接使用move_uploaded_file函数
               //这部分需要的是一个图片资源,所以使用fopen和file_get_contents包括直接修改tmp文件的后缀名都失败了,所以选择上传
              $result =  move_uploaded_file($filePath, $file_path);
           }else {
               switch ($ext) {
                   case 'image/png':
                       $im = imagecreatefrompng($filePath);
                       break;
                   case 'image/gif':
                       $im = imagecreatefromgif($filePath);
                       break;
                   case 'image/jpeg':
                       $im = imagecreatefromjpeg($filePath);
                       break;
                   case 'image/jpg':
                       $im = imagecreatefromjpeg($filePath);
                       break;
                   default:
                       exit("上传文件格式不正确");
                       break;
               }
               $result = imagejpeg($im, $file_path);
               imagedestroy($im);
           }
           }

具体的可以看看注释

4、关于上传图片的临时文件后缀是.tmp的问题

博主在上传的时候,发现报错:

"imagecreatefrompng(): 'C:\\Users\\ZY\\AppData\\Local\\Temp\\php9A06.tmp' is not a valid PNG file

      经过打印发现,$_FILES['tem_name']可能会返回123.tmp文件,如果是这种文件的话,是无法通过自带的打开图片资源函数打开。这种情况下,tmp文件只是生成的临时文件,可以用fopen,file_get_contents等打开,但是打开的资源不是图片资源因此采用:move_uploaded_file($filePath, $file_path); 直接保存。

5、关于$_FILES[‘tmp_name’]解释

(1) $_FILES['excel']['tmp_name'] 表示的就是上传临时文件的绝对路径

(2) 上传临时文件的生存周期与处理上传的php程序相同(即程序结束,临时文件消失)

(3) move_uploaded_file 函数可使临时文件提前消失

6、获取图片后缀的问题

通过$_FILES获取的后缀名是image/png这样的,如果只是想要png的后缀,需要进行截取等操作,其实不用这么麻烦的,可以使用:

(1) getimagesize()函数,

(2)pathinfo函数

 $fileParts = pathinfo($_FILES['images']['name']);
var_dump($fileParts['extension']);  //获取后缀名

相关学习推荐:php编程(视频)

The above is the detailed content of How to change the image suffix to jpg when uploading images in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn