PHP图片压缩,再转base64传输

' . print_r($img_info, true) . '

'; $fp = fopen($app_img_file, "r"); // 图片是否可读权限 if ($fp) { $filesize = filesize($app_img_file); $content = fread($fp, $filesize); $file_content = base64_encode($content); // base64编码 switch ($img_info[2]) { //判读图片类型 case 1: $img_type = "gif"; break; case 2: $img_type = "jpg"; break; case 3: $img_type = "png"; break; } $img_base64 = $file_content;//合成图片的base64编码 // file_put_contents('/usr/local/paperless/apache/11.txt',$img_base64. PHP_EOL, FILE_APPEND); } fclose($fp); } return $img_base64; //返回图片的base64 } /** * 压缩图片大小 * @param $img_file * @param int $percent * @param int $quality * @param int $new_width 指定的宽度 * @param int $new_height 指定的高度 */ function imgcompress($img_file,$percent=1,$quality=100,$new_width=0,$new_height=0){ list($width, $height, $type, $attr) = getimagesize($img_file); $type = image_type_to_extension($type,false); $fun = "imagecreatefrom".$type; $image = $fun($img_file); if ($new_height==0 && $new_width==0){ //如果没有指定宽高,则按比例计算 $new_width = $width * $percent; $new_height = $height * $percent; } $image_thump = imagecreatetruecolor($new_width,$new_height); //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度 imagecopyresampled($image_thump,$image,0,0,0,0,$new_width,$new_height,$width,$height); imagedestroy($image); // unlink($img_file); $image = $image_thump; $funcs = "image".$type; if ($type == 'jpeg'){ $funcs($image,$img_file,$quality); }else{ $funcs($image,$img_file); } } imgcompress($outpath,1,70,1024,600); //图片转base64存储 $img_base64 = img_to_base64($outpath); unlink($outpath);

你可能感兴趣的:(PHP,php,开发语言)