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

<?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);
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页