PHP base64转图片保存并缩小像素处理

这段PHP代码实现了将Base64编码的图片转换为实际图片并保存到指定文件夹,同时根据医生ID生成文件名。如果文件夹不存在,会自动创建。然后对图片进行压缩,支持JPEG、PNG和GIF格式。压缩后,返回包含结果状态、保存路径和医生ID的JSON数据。
摘要由CSDN通过智能技术生成
<?php
/*
 **函数:function base64_image_content($base64_image_content,$path,$doctorid)
 **功能:
 **  base64格式编码转换为图片并保存对应文件夹 
 **参数:
 ** base64_image_content:base64编码
 ** path:存放文件夹
 ** doctorid:生成文件名
 */
function base64_image_content($base64_image_content,$path,$doctorid){
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
        $type = $result[2];
        $new_file = $path."/".$doctorid;
        if(!file_exists($path)){
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($path, 0700);
        }
        $new_file = $new_file.".{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
            $temp = array(
                "result"            => 1,
                "path"              => '/'.$new_file,
                "doctId"            => $doctorid
            );
            compressedImage($new_file,  "doctor_img_temp"."/".$doctorid.".jpg");//生成后去压缩
            return json_encode($temp);
        }else{
             $temp = array(
                "result"            => 0,
            );
            return json_encode($temp);
        }
    }else{
        $temp = array(
            "result"            => 2,
        );
        return json_encode($temp);
       
    }
}
/*
 **函数:function compressedImage($imgsrc, $imgdst)
 **功能:
 **  base64格式编码转换为图片并保存对应文件夹 
 **参数:
 ** imgsrc:需要修改的图片路径
 ** imgdst :压缩后保存路径
 */
function compressedImage($imgsrc, $imgdst) {
    list($width, $height, $type) = getimagesize($imgsrc);
    
    $new_width = $width;//压缩后的图片宽
    $new_height = $height;//压缩后的图片高
        
    if($width >= 600){
      $per = 600 / $width;//计算比例
      $new_width = $width * $per;
      $new_height = $height * $per;
    }
    
    switch ($type) {
      case 1:
        $giftype = check_gifcartoon($imgsrc);
        if ($giftype) {
          header('Content-Type:image/gif');
          $image_wp = imagecreatetruecolor($new_width, $new_height);
          $image = imagecreatefromgif($imgsrc);
          imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
          //90代表的是质量、压缩图片容量大小
          imagejpeg($image_wp, $imgdst, 90);
          imagedestroy($image_wp);
          imagedestroy($image);
        }
        break;
      case 2:
        header('Content-Type:image/jpeg');
        $image_wp = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefromjpeg($imgsrc);
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        //90代表的是质量、压缩图片容量大小
        imagejpeg($image_wp, $imgdst, 90);
        imagedestroy($image_wp);
        imagedestroy($image);
        break;
      case 3:
        header('Content-Type:image/png');
        $image_wp = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefrompng($imgsrc);
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        //90代表的是质量、压缩图片容量大小
        imagejpeg($image_wp, $imgdst, 90);
        imagedestroy($image_wp);
        imagedestroy($image);
        break;
    }
  }

isset($_REQUEST['base64_img']) ? $base64_img = $_REQUEST['base64_img'] : $base64_img = '';
isset($_REQUEST['doctorID']) ? $doctorID = $_REQUEST['doctorID'] : $doctorID = '';

echo base64_image_content($base64_img,"doctor_img",$doctorID);
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值