压缩到600宽度,并且图片不旋转90度
/*判断图片是否存在 存在则保存图片路径 系统进程结束之后单独上传图片上传图片*/
$photo=input('photo','');
$img_url = '';//图片路径
$base64_image = str_replace(' ', '+', $photo);
//post方式接收的数据, 加号会被替换为空格, 需要重新替换回来, 若不是post数据, 不需要执行
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)) {
$image_data = base64_decode(str_replace($result[1], '', $base64_image));
$im = imagecreatefromstring($image_data);
if ($im !== false) {
$ext = strtolower($result[2]);
if ($ext == 'jpeg') $ext = 'jpg'; // 统一后缀
// ========== 1. 修正 JPEG 的方向(EXIF Orientation) ==========
if ($ext == 'jpg' && function_exists('exif_read_data')) {
// 将图片二进制数据写入临时文件(exif_read_data 需要读取文件)
$tempFile = tmpfile();
fwrite($tempFile, $image_data);
$tempMeta = stream_get_meta_data($tempFile);
$tempPath = $tempMeta['uri'];
$exif = @exif_read_data($tempPath);
fclose($tempFile);
if (!empty($exif['Orientation'])) {
$orientation = $exif['Orientation'];
// 根据 Orientation 值旋转/翻转图片
switch ($orientation) {
case 3:
$im = imagerotate($im, 180, 0);
break;
case 6:
$im = imagerotate($im, -90, 0);
break;
case 8:
$im = imagerotate($im, 90, 0);
break;
// 如果需要支持镜像(2,4,5,7),可继续补充,但绝大多数情况只用到 3,6,8
}
}
}
// ========== 2. 尺寸压缩(等比例限制最大宽度600) ==========
$max_width = 600;
$src_w = imagesx($im);
$src_h = imagesy($im);
if ($src_w > $max_width) {
$dst_w = $max_width;
$dst_h = intval($src_h * ($max_width / $src_w));
$im2 = imagecreatetruecolor($dst_w, $dst_h);
// 透明背景处理(PNG/GIF 保留透明通道)
if ($ext == 'png') {
imagealphablending($im2, false);
imagesavealpha($im2, true);
$trans = imagecolorallocatealpha($im2, 0, 0, 0, 127);
imagefilledrectangle($im2, 0, 0, $dst_w, $dst_h, $trans);
} elseif ($ext == 'gif') {
$trans_index = imagecolortransparent($im);
if ($trans_index >= 0) {
imagepalettecopy($im, $im2);
imagefill($im2, 0, 0, $trans_index);
imagecolortransparent($im2, $trans_index);
}
}
imagecopyresampled($im2, $im, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
imagedestroy($im);
$im = $im2;
}
// ========== 3. 保存文件(质量压缩) ==========
$dir = './uploads/member/' . date("Ymd");
if (!is_dir($dir)) mkdir($dir, 0777, true);
$picname = date("his") . '_' . rand(10000, 99999);
$picdir = $picname . '.' . $ext;
$image_url = $dir . '/' . $picdir;
switch ($ext) {
case 'jpg':
imagejpeg($im, $image_url, 80);
break;
case 'png':
imagepng($im, $image_url, 6);
break;
case 'gif':
imagegif($im, $image_url);
break;
case 'webp':
imagewebp($im, $image_url, 80);
break;
default:
file_put_contents($image_url, $image_data);
}
imagedestroy($im);
$img_url = $image_url;
}
}保存原文件
/*判断图片是否存在 存在则保存图片路径 系统进程结束之后单独上传图片上传图片*/
$photo=input('photo','');
$img_url = '';//图片路径
$base64_image = str_replace(' ', '+', $photo);
//post方式接收的数据, 加号会被替换为空格, 需要重新替换回来, 若不是post数据, 不需要执行
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)) {
//定义图片储存文件目录
$dir = './uploads/member/'.date("Ymd",time());
//定义文件名称
$picname = date("his") . '_' . rand(10000, 99999);
if (!is_dir($dir)) {
//如果不存在就创建该目录
mkdir($dir, 0777, true);
}
//获取图片后缀
if ($result[2] == 'jpeg') {
$picdir = $picname . '.jpg';
} else {
$picdir = $picname . '.' . $result[2];
}
//图片名称
$image_url = $dir . '/' . $picdir;
//储存图片
if (file_put_contents($image_url, base64_decode(str_replace($result[1], '', $base64_image)))) {
$img_url = $image_url;
}
}在线base64转换工具网站 https://mantools.top/index/mtindex/bs64toimg.html