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

php上传或者压缩图片后图片出现倒转或者反转的问题

  //制作缩略图$image = new \Think\Image();$yimg = "/home2/zytx/Public/" . $file['savepath'] . $file['savename'];$image->open($yimg);$exif = exif_read_data($yimg);if (isset($exif['Orientation']) && $exif['Orientation'] == 6) {$sourceImage = imagecreatefromjpeg($yimg);// 顺时针旋转90度$newImage = imagerotate($sourceImage, -90, 0);$tempFilePath = tempnam(sys_get_temp_dir(), 'rotated_');imagejpeg($newImage, $tempFilePath);imagedestroy($newImage);} else {$tempFilePath = $yimg; // 如果没有EXIF旋转信息,直接使用原始图片路径作为临时文件路径}// 按照原图的比例生成一个最大为150*120的缩略图并保存为thumb.jpg$thumbPath = '/home2/zytx/Public/thumb/' . $floadName . '/';if (!is_dir($thumbPath)) {mkdir($thumbPath, 0777, true); //创建目录}

获取图片是否倾斜

$exif = exif_read_data($yimg);

旋转图片
imagerotate()

以下是一些处理图片反转的方法

<?php
// 创建源图像
$sourceImage = imagecreatefromjpeg('source.jpg'); // 或其他格式
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);// 方法1:使用 imageflip(PHP 5.5+,推荐)
$newImage = imagecreatefromjpeg('source.jpg');
imageflip($newImage, IMG_FLIP_VERTICAL); // 垂直翻转
// imageflip($newImage, IMG_FLIP_HORIZONTAL); // 水平翻转
// imageflip($newImage, IMG_FLIP_BOTH); // 水平+垂直翻转// 输出图像
header('Content-Type: image/jpeg');
imagejpeg($newImage);// 释放内存
imagedestroy($sourceImage);
imagedestroy($newImage);
?>

2

<?php
// 创建源图像
$sourceImage = imagecreatefromjpeg('source.jpg');
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);// 正确创建新图像
$newImage = imagecreatetruecolor($width, $height);// 保持透明度(如果是PNG)
if (function_exists('imagealphablending')) {imagealphablending($newImage, false);imagesavealpha($newImage, true);
}// 垂直翻转
imagecopyresampled($newImage,      // 目标图像$sourceImage,   // 源图像0, 0,          // 目标起始坐标 (dx, dy)0, $height - 1, // 源起始坐标 (sx, sy) - 从底部开始$width, $height, // 目标宽高 (dw, dh)$width, -$height // 源宽高 (sw, sh) - 注意高度为负数
);// 输出图像
header('Content-Type: image/jpeg');
imagejpeg($newImage);// 释放内存
imagedestroy($sourceImage);
imagedestroy($newImage);
?>

3反转函数

<?php
function flipImage($sourcePath, $flipType = 'vertical') {// 获取图像信息$imageInfo = getimagesize($sourcePath);$imageType = $imageInfo[2];// 根据图像类型创建源图像switch ($imageType) {case IMAGETYPE_JPEG:$sourceImage = imagecreatefromjpeg($sourcePath);break;case IMAGETYPE_PNG:$sourceImage = imagecreatefrompng($sourcePath);break;case IMAGETYPE_GIF:$sourceImage = imagecreatefromgif($sourcePath);break;default:return false;}$width = imagesx($sourceImage);$height = imagesy($sourceImage);// 创建新图像$newImage = imagecreatetruecolor($width, $height);// 处理透明度if ($imageType == IMAGETYPE_PNG || $imageType == IMAGETYPE_GIF) {imagealphablending($newImage, false);imagesavealpha($newImage, true);$transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);imagefill($newImage, 0, 0, $transparent);}// 根据翻转类型进行翻转switch ($flipType) {case 'vertical':// 垂直翻转for ($y = 0; $y < $height; $y++) {imagecopy($newImage, $sourceImage, 0, $y, 0, $height - $y - 1, $width, 1);}break;case 'horizontal':// 水平翻转for ($x = 0; $x < $width; $x++) {imagecopy($newImage, $sourceImage, $x, 0, $width - $x - 1, 0, 1, $height);}break;case 'both':// 水平+垂直翻转for ($y = 0; $y < $height; $y++) {for ($x = 0; $x < $width; $x++) {$color = imagecolorat($sourceImage, $x, $y);imagesetpixel($newImage, $width - $x - 1, $height - $y - 1, $color);}}break;}// 输出图像header('Content-Type: image/jpeg');imagejpeg($newImage);// 释放内存imagedestroy($sourceImage);imagedestroy($newImage);return true;
}// 使用示例
flipImage('source.jpg', 'vertical');
?>
PHP中旋转图片,推荐使用 imagerotate() 函数,这是最简单的方法。对于你遇到的问题,我提供两种解决方案:方案一:使用 imagerotate()(推荐)
php复制
// 逆时针旋转90度
$rotated = imagerotate($sourceImage, 90, 0);// 如果需要保存透明度(PNG/GIF)
imagealphablending($rotated, false);
imagesavealpha($rotated, true);// 输出或保存图片
imagepng($rotated, 'output.png');
imagedestroy($rotated);
方案二:修正 imagecopyresampled() 用法
你的代码问题在于参数设置错误。正确的逆时针90度旋转代码:php复制
// 获取原图尺寸
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);// 创建新图片(注意:宽高要对调)
$newImage = imagecreatetruecolor($height, $width);// 保持透明度(如果是PNG/GIF)
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
$transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagefilledrectangle($newImage, 0, 0, $height, $width, $transparent);// 逆时针旋转90度的正确参数
for($y = 0; $y < $height; $y++) {for($x = 0; $x < $width; $x++) {$rgb = imagecolorat($sourceImage, $x, $y);imagesetpixel($newImage, $y, $width - $x - 1, $rgb);}
}// 或者使用 imagecopyresampled 一次性复制
// imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $height, $width, $width, $height);
完整示例
php复制
// 加载原图
$sourceImage = imagecreatefromjpeg('input.jpg'); // 或 imagecreatefrompng()// 方法1:使用 imagerotate
$rotated = imagerotate($sourceImage, 90, 0);
imagejpeg($rotated, 'output.jpg');// 方法2:手动旋转
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);
$newImage = imagecreatetruecolor($height, $width);// 如果是PNG,保持透明
if($imageType == 'png') {imagealphablending($newImage, false);imagesavealpha($newImage, true);
}// 逐像素复制并旋转
for($y = 0; $y < $height; $y++) {for($x = 0; $x < $width; $x++) {$rgb = imagecolorat($sourceImage, $x, $y);imagesetpixel($newImage, $y, $width - $x - 1, $rgb);}
}// 输出
imagejpeg($newImage, 'rotated.jpg');// 释放内存
imagedestroy($sourceImage);
imagedestroy($newImage);```
http://www.dtcms.com/a/264527.html

相关文章:

  • Hyper-YOLO: When Visual Object Detection Meets Hypergraph Computation
  • 在Ubuntu上多网卡配置HTTP-HTTPS代理服务器
  • c语言中的函数II
  • 今日学习:音视频领域入门文章参考(待完善)
  • 数据结构:数组(Array)
  • 文心快码答用户问|Comate AI IDE专场
  • 文心4.5开源模型部署实践
  • 使用Vue3实现输入emoji 表情包
  • 阿里云AppFlow AI助手打造智能搜索摘要新体验
  • 基于开源链动2+1模式AI智能名片S2B2C商城小程序的场景零售创新研究
  • 【Unity】MiniGame编辑器小游戏(八)三国华容道【HuarongRoad】
  • Active-Prompt:让AI更智能地学习推理的革命性技术
  • BlenderBot对话机器人大模型Facebook开发
  • Spring Framework 中 Java 配置
  • 51单片机外部引脚案例分析
  • 环境土壤物理Hydrus1D2D模型实践技术应用及典型案例分析
  • Docker Desktop导致存储空间不足时的解决方案
  • 【QT】ROS2 Humble联合使用QT教程
  • 【Unity】MiniGame编辑器小游戏(九)打砖块【Breakout】
  • 纹理贴图算法研究论文综述
  • 二、jenkins之idea提交项目到gitlab、jenkins获取项目
  • 将大仓库拆分为多个小仓库
  • 前端请求浏览器提示net::ERR_UNSAFE_PORT的解决方案
  • WPF路由事件:冒泡、隧道与直接全解析
  • 【Harmony】鸿蒙企业应用详解
  • 小型水电站综合自动化系统的介绍
  • 计算机组成笔记:缓存替换算法
  • QT6 源(147)模型视图架构里的表格窗体 QTableWidget 的范例代码举例,以及其条目 QTableWidgetItem 类型的源代码。
  • Re:从零开始的 磁盘调度进程调度算法(考研向)
  • Node.js 安装使用教程