当前位置: 首页 > news >正文

php重写上传图片成jpg图片

 最近上传图片时发现用户经常上传一些后缀名为jpg格式而实际图片格式为png的图片。导致生成合成图片时报错。因此写了重写图片为jpg图片的方法。如下所示:

/**
 * 重写图片成jpg图片
 * @param $sourceFile 原始图片文件
 * @param $targetDir  目标目录
 * @param $quality  图片质量
 * @return string
 * @throws Exception
 */
function convertImageToJpg($sourceFile, $targetDir, $quality = 85) {
    ini_set('memory_limit', '256M');

    // 检查GD库是否可用
    if (!extension_loaded('gd') || !function_exists('gd_info')) {
        //throw new Exception('GD库未安装');
        return ['code'=>4001,'msg'=>'GD库未安装'];
    }

    // 验证源文件
    if (!file_exists($sourceFile)) {
      //  throw new Exception('源文件不存在');
        return ['code'=>4002,'msg'=>'源文件不存在'];
    }

    // 获取图片信息
    $imageInfo = @getimagesize($sourceFile);
    if (!$imageInfo) {
        //throw new Exception('无效的图片文件');
        return ['code'=>4003,'msg'=>'无效的图片文件'];
    }


     if($imageInfo[0] > 4000 || $imageInfo[1]  > 4000){
         return ['code'=>4000,'msg'=>'图片分辨率不可大于4000*4000像素'];
     }

    // 生成目标路径
    $filename = pathinfo($sourceFile, PATHINFO_FILENAME);
    $targetPath = rtrim($targetDir, '/') . '/' . $filename . '.jpg';

    // 根据MIME类型创建图像资源
    $mimeType = $imageInfo['mime'];
    switch ($mimeType) {
        case 'image/jpeg'://如果是jpg,不用重写,直接返回路径
            return ['code'=>200,'msg'=>'成功','data'=>['filePath'=>$targetPath]];
            $image = imagecreatefromjpeg($sourceFile);
            break;
        case 'image/png':
            $image = imagecreatefrompng($sourceFile);
            break;
        case 'image/gif':
            $image = imagecreatefromgif($sourceFile);
            break;
        case 'image/webp':
            $image = imagecreatefromwebp($sourceFile);
            break;
        case 'image/bmp':
            $image = imagecreatefrombmp($sourceFile);
            break;
        default:
           // throw new Exception('不支持的图片格式: ' . $mimeType);
            return ['code'=>4004,'msg'=>'不支持的图片格式'. $mimeType];
    }


    // 处理透明背景(PNG/GIF)
    if (in_array($mimeType, ['image/png', 'image/gif'])) {
        $width = imagesx($image);
        $height = imagesy($image);

        // 创建新画布并填充白色背景
        $jpg = imagecreatetruecolor($width, $height);
        $white = imagecolorallocate($jpg, 255, 255, 255);
        imagefill($jpg, 0, 0, $white);
        imagecopy($jpg, $image, 0, 0, 0, 0, $width, $height);
        imagedestroy($image);
        $image = $jpg;
    }

    // 保存JPG文件
    if (!imagejpeg($image, $targetPath, $quality)) {
        //throw new Exception('JPG文件保存失败');
        return ['code'=>4005,'msg'=>'JPG文件保存失败'];
    }

    // 释放内存
    imagedestroy($image);

    //return $targetPath;
    return ['code'=>200,'msg'=>'成功','data'=>['filePath'=>$targetPath]];
}

相关文章:

  • Sui 如何支持各种类型的 Web3 游戏
  • UI 自动化测试框架介绍
  • vue项目启动时报错:error:0308010C:digital envelope routines::unsupported
  • 火语言RPA--Excel插入空行
  • SpringCloud-使用FFmpeg对视频压缩处理
  • MyBatis中的日志和映射器说明
  • nvm安装、管理node多版本以及配置环境变量【保姆级教程】
  • 详解分布式ID实践
  • 解决 Ubuntu 中 Docker 安装时“无法找到软件包”错误
  • 现场可以通过手机或者pad实时拍照上传到大屏幕的照片墙现场大屏电子照片墙功能
  • Renesas RH850 IAR编译时变量分配特定内存
  • Python的那些事第二十八篇:数据分析与操作的利器Pandas
  • OpenMetadata Service与Ingestion模块接口交互全解析
  • 【开关电源】汽车前端电源保护电路设计
  • DeepSeek安装部署笔记(一)
  • upload-labs
  • 直播美颜SDK的底层技术解析:图像处理与深度学习的结合
  • 双重差分学习笔记
  • 一文了解大模型性能评测数据、指标以及框架
  • 计算机视觉(CV)
  • 免费b2b网站大全免费黄页大灰狼/推广普通话文字素材
  • 技术支持 沧州辉煌网络-网站建设/mac923水蜜桃923色号
  • 美团外卖网站开发/seo技术团队
  • 别人做的网站域名到期怎么办/培训网站
  • 电脑租赁平台哪个好/台州网站seo
  • 长治网站制作哪家好/建网站seo