我正在尝试上传图像并修复图像的旋转,然后将其更新到服务器,我必须使用此链接中的答案:here 但方向错误的图像仍然不起作用
php 中的代码:
//START Update the user image
if(isset($_POST['user_image_id'])){
$upload_image_id = $_POST['user_image_id'];
$user_image = $_FILES['user_image']['name'];
$filePath = $_FILES['user_image']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($_FILES['user_image']['tmp_name']));
$exif = exif_read_data($filePath);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
//create image target to upload
$image_target = $folder_target.basename($_FILES['user_image']['name']);
//upload the image the and update the name of the image
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $image_target)){
if ($conn->query("UPDATE `users` SET `image`='$user_image' WHERE `id` LIKE '$upload_image_id'")){
echo 1;
}else{
echo "not update the name, Proplem";
}
}else{
echo 'image not uploaded';
}
}
我使用图片库“https://github.com/Al-Alloush/JavaScript-Load-Image”来固定图片在浏览器中的方向,但是当我提交图片并将其上传到服务器时,它的方向仍然相同
请帮忙,:)
最佳答案
旋转后您还没有保存图像。上传前保存如下。
imagejpeg($image, $filePath);
//upload the image the and update the name of the image
if(move_uploaded_file($filePath, $image_target)){
....
}
Note:
imagejpeg
should be used for images withJPG
extension. ForPNG
, useimagepng
.
关于php - 如何修复图像的方向并使用 PHP 上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48467336/