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);```