首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP中的自动图像格式检测

PHP中的自动图像格式检测
EN

Stack Overflow用户
提问于 2008-10-09 22:09:52
回答 5查看 18.3K关注 0票数 22

我正在寻找一种方法,以采取用户上传的图像,目前是放在一个临时位置例如: /tmp/jkhjkh78,并从它创建一个php图像,自动检测格式。

有没有比使用imagefromjpeg、imagefrompng等的一堆try/catching更聪明的方法呢?

EN

回答 5

Stack Overflow用户

发布于 2008-10-09 22:13:59

这是getimagesize的功能之一。他们可能应该叫它"getimageinfo",但这是为你准备的PHP。

票数 27
EN

Stack Overflow用户

发布于 2014-05-15 23:06:52

   //Image Processing
    $cover = $_FILES['cover']['name'];
    $cover_tmp_name = $_FILES['cover']['tmp_name'];
    $cover_img_path = '/images/';
    $type = exif_imagetype($cover_tmp_name);

if ($type == (IMAGETYPE_PNG || IMAGETYPE_JPEG || IMAGETYPE_GIF || IMAGETYPE_BMP)) {
        $cover_pre_name = md5($cover);  //Just to make a image name random and cool :D
/**
 * @description : possible exif_imagetype() return values in $type
 * 1 - gif image
 * 2 - jpg image
 * 3 - png image
 * 6 - bmp image
 */
        switch ($type) {    #There are more type you can choose. Take a look in php manual -> http://www.php.net/manual/en/function.exif-imagetype.php
            case '1' :
                $cover_format = 'gif';
                break;
            case '2' :
                $cover_format = 'jpg';
                break;
            case '3' :
                $cover_format = 'png';
                break;
            case '6' :
                $cover_format = 'bmp';
                break;

            default :
                die('There is an error processing the image -> please try again with a new image');
                break;
        }
    $cover_name = $cover_pre_name . '.' . $cover_format;
      //Checks whether the uploaded file exist or not
            if (file_exists($cover_img_path . $cover_name)) {
                $extra = 1;
                while (file_exists($cover_img_path . $cover_name)) {
        $cover_name = md5($cover) . $extra . '.' . $cover_format;
                    $extra++;
                }
            }
     //Image Processing Ends

这将使图像名称看起来很酷和独特

票数 6
EN

Stack Overflow用户

发布于 2010-02-28 22:39:14

使用exif_imagetype() (如果可用) ..:

http://www.php.net/manual/en/function.exif-imagetype.php

我非常确定在安装php时,exif函数在默认情况下是可用的(也就是说,您必须明确地排除它们,而不是明确地包括它们

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/189391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档