Php 从有旋转问题的智能手机上传照片?
我在上面尝试了“更新代码”,上传的图像确实会旋转,但是由PHP的GD创建的目标图像仍然是横向的,而不是纵向的。因此,我最终得到了一个旋转的肖像图像,在右侧有黑色背景的横向矩形的底部被切掉。我已经研究了代码,不知道为什么。GD创建的目标图像似乎仍在从原始上传图像中提取方向。不确定Php 从有旋转问题的智能手机上传照片?,php,ios,image,Php,Ios,Image,我在上面尝试了“更新代码”,上传的图像确实会旋转,但是由PHP的GD创建的目标图像仍然是横向的,而不是纵向的。因此,我最终得到了一个旋转的肖像图像,在右侧有黑色背景的横向矩形的底部被切掉。我已经研究了代码,不知道为什么。GD创建的目标图像似乎仍在从原始上传图像中提取方向。不确定 if (isset($_FILES['image'])) { if (empty($_FILES['image']['name'])) { ?><div class="add-errors
if (isset($_FILES['image'])) {
if (empty($_FILES['image']['name'])) {
?><div class="add-errors">Please choose an image!</div><?php
} else {
function getOrientedImage($imagePath){
$image = imagecreatefromstring(file_get_contents($imagePath));
$exif = exif_read_data($imagePath);
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;
}
}
return $image;
}
$name = $_FILES['image']['name'];
$temp = $_FILES['image']['tmp_name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$ext = strtolower(end(explode('.', $name)));
$size2 = getimagesize($temp);
$width = $size2[0];
$height = $size2[1];
$upload = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ));
// Restrictions for uploading
$maxwidth = 6000;
$maxheight = 6000;
$allowed = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');
// Recognizing the extension
switch($type){
// Image/Jpeg
case 'image/jpeg':
$ext= '.jpeg';
break;
// Image/Jpg
case 'image/jpg':
$ext= '.jpg';
break;
// Image/png
case 'image/png':
$ext= '.png';
break;
// Image/gif
case 'image/gif':
$ext= '.gif';
break;
}
// upload variables
$path = $userDir . $upload . $ext;
$thumb_path = $userDir . 'thumb_' . $upload . $ext;
// check if extension is allowed.
if (in_array($type, $allowed)) {
// Checking if the resolution is FULLHD or under this resolution.
if ($width <= $maxwidth && $height <= $maxheight) {
if ($size <= 5242880) {
// check the shape of the image
if ($width == $height) {$shape = 1;}
if ($width > $height) {$shape = 2;}
if ($width < $height) {$shape = 2;}
//Adjusting the resize script on shape.
switch($shape) {
// Code to resize a square image.
case 1:
$newwidth = 690;
$newheight = 690;
break;
// Code to resize a tall image
case 2:
$newwidth = 690;
$ratio = $newwidth / $width;
$newheight = round($height * $ratio);
break;
}
// Resizing according to extension.
switch ($type) {
// Image/Jpeg
case 'image/jpeg';
$img = getOrientedImage($temp);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $thumb_path);
break;
// Image/Jpg
case 'image/jpg';
$img = getOrientedImage($temp);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $thumb_path);
break;
// Image/png
case 'image/png';
$img = getOrientedImage($temp);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($thumb, $thumb_path);
break;
// Image/gif
case 'image/gif';
$img = getOrientedImage($temp);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagegif($thumb, $thumb_path);
break;
}
// Move the original file aswell.
move_uploaded_file($temp, $path);
} else {
?><div class="add-errors">Your image size is too big!</div><?php
}
} else {
?><div class="add-errors">Your image resolution exceeds the limit!</div><?php
}
} else {
?><div class="add-errors">Your have uploaded a forbidden extension!</div><?php
}
}
if(isset($\u文件['image'])){
if(空($_文件['image']['name'])){
?>请选择一个图像!您的图像大小太大!您的图像分辨率超出限制!您已上载禁止的扩展名!检查添加此行是否适合您:
$height = $size2[1];
$upload = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ));
//Add this lines to create a swap
$exif = exif_read_data($temp);
if (isset($exif['Orientation']) && in_array($exif['Orientation'], Array(8, 6)))
{ list($width, $height) = array($height, $width); }
// Restrictions for uploading
$maxwidth = 6000;
$maxheight = 6000;
$allowed = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');
它们基本上只是在图像旋转的情况下交换图像的高度和宽度。请注意,你应该考虑将解决方案重构成你可以正确读取的内容,而不会强迫应用程序读取EXIF两次。
< P>检查是否添加了这行对你有用:
$height = $size2[1];
$upload = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ));
//Add this lines to create a swap
$exif = exif_read_data($temp);
if (isset($exif['Orientation']) && in_array($exif['Orientation'], Array(8, 6)))
{ list($width, $height) = array($height, $width); }
// Restrictions for uploading
$maxwidth = 6000;
$maxheight = 6000;
$allowed = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');
它们基本上只是在图像被旋转的情况下交换图像的高度和宽度。请注意,你应该考虑将解决方案重构成可以正确读取的内容,而不会强迫应用程序读取EXIF两次。旋转,但使用非苹果软件不支持的专有标志?听起来像是将图像复制到原始尺寸的图像中。在GD中旋转图像不会导致画布更改格式,因此您实际上必须创建一个高度和宽度互换的新图像。这可能是因为iOS没有使用常规EXIF标志来指定旋转,而是使用非苹果软件不支持的专有标志。这听起来像是将图像复制到原始尺寸的图像中。在GD中旋转图像不会导致画布更改格式,因此您实际上必须创建一个新的图像,其中高度和宽度被调出。这成功了!我纠正了一些拼写错误,但我知道你在做什么。我试图手动执行此操作,但只是不太清楚如何处理此问题。谢谢,塞萨尔!只是缺少另一个“)”和“$”关于高度变量。介意更正答案中的拼写错误以供参考吗?以防有人看到答案^^^我注意到我可以这样做。是的。这成功了!我更正了一些拼写错误,但我知道你在做什么。我试图手动执行此操作,但只是不太清楚如何得出此答案。谢谢,塞萨尔!只是错过了另一个在if行上有r“),在高度变量上有“$”。介意更正答案中的拼写错误以供参考吗?以防有人看到答案^^^我注意到我可以这样做。是的。