图片上传工作,缩略图是倾斜的


Image upload works, thumbnail is skewed?

当上传到我的服务器时,常规图像上传得很好,但对于一些图像,当创建缩略图时,图像会旋转,我不知道为什么或我的代码发生了什么?

请注意,我使用的是旧语法,我知道它很难看,但我很懒,它很管用。

<? session_start(); ?>
<? include('header.inc.php'); ?>
<?
    if(isset($_POST['upload_Submit'])) {
     $_SESSION['success'] = 0;
     $upPath = "/home/dolphina/public_html/images/";
     $fileName = uniqid(time().date(mdY));
     $_SESSION['upload_Error'] = 0;
     $error_Log = "<ul>";
      if ($_FILES['upload_Image']['size'] > 10000000) {
       $error_Log .= "<li>image is greater than 10 megabyte</li>";
        $_SESSION['upload_Error'] = 1;
      }
      if (($_FILES['upload_Image']['type'] == "image/gif") || ($_FILES['upload_Image']['type'] == "image/pjpeg") || ($_FILES['upload_Image']['type'] == "image/jpeg") || ($_FILES['upload_Image']['type'] == "image/png")) {
       if($_FILES['upload_Image']['type'] == "image/gif") {
        $fileExt = ".gif";
        }
       if($_FILES['upload_Image']['type'] == "image/pjpeg") {
         $fileExt = ".jpg";
       }
       if($_FILES['upload_Image']['type'] == "image/jpeg") {
        $fileExt = ".jpeg";
       }
       if($_FILES['upload_Image']['type'] == "image/png") {
         $fileExt = ".png";
       }
      } else {
      $error_Log .= "<li>invalid image type</li>";
      $_SESSION['upload_Error'] = 1;
    }
    if(!$_POST['upload_Caption']) {
      $error_Log .= "<li>no caption entered</li>";
      $_SESSION['upload_Error'] = 1;
    }
    if(!$_POST['upload_Password']) {
      $error_Log .= "<li>no password entered</li>";
      $_SESSION['upload_Error'] = 1;
    }
    if($_POST['upload_Password'] != "3") {
      $error_Log .= "<li>wrong password</li>";
      $_SESSION['upload_Error'] = 1;
    }
   $error_Log .= "</ul>";
    if($_SESSION['upload_Error'] == 1) {
      echo $error_Log;
      $_SESSION['upload_Error'] = 0;
    } else {
      //COPIES TEMP FILE TO PATH
    copy($_FILES['upload_Image']['tmp_name'], $upPath."pics/".$fileName.$fileExt);
    $first=ImageCreateFromJPEG($upPath."pics/".$fileName.$fileExt);
  echo "GOT HERE";
//CREATES AND COPIES THUMBNAIL TO PATH
function make_thumb($src, $dest, $desired_width) {
  /* read the source image */
  $source_image = imagecreatefromjpeg($src);
  $width = imagesx($source_image);
  $height = imagesy($source_image);
  /* find the "desired height" of this thumbnail, relative to the desired width  */
  $desired_height = floor($height * ($desired_width / $width));
  /* create a new, "virtual" image */
  $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
  /* copy source image at a resized size */
  imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
  /* create the physical thumbnail image to its destination */
  imagejpeg($virtual_image, $dest);
}
make_thumb($upPath."pics/".$fileName.$fileExt, $upPath."thumb/thumbnail_".$fileName.$fileExt, 300);
//INSERT INTO DATABASE
      $upImage = "INSERT INTO `images` (`url`, `thumb`, `date`, `time`, `ip`, `caption`) VALUES ('images/pics/".$fileName.$fileExt."', 'images/thumb/thumbnail_".$fileName.$fileExt."', CURDATE(), CURTIME(), '".$_SERVER['REMOTE_ADDR']."', '".$_POST['upload_Caption']."')";
      $upFINAL = mysqli_query($mysql_conn, $upImage);
      $_SESSION['success'] = 1;
    }
  }
  if($_SESSION['success'] == 1) { echo("<div>SUCCESS</div>");}
?>
<style>
ul { padding: 0; margin: 0; margin-left: 4px; }
</style>
<form method="POST" enctype="multipart/form-data" action="upload.php">
<div>File</div>
<div><input type="file" name="upload_Image" size="40"></div>
<div>Caption</div>
<div><input type="text" name="upload_Caption" size="40"></div>
<div><input type="password" name="upload_Password" size="40"></div>
<div><input type="submit" name="upload_Submit" value="Upload Image"></div>
</form>
</div>

在PHP文档中,引用了imagecreatefromjpeg上注释112902中的一句话:

imagecreatefromjpeg((
此函数不支持EXIF方向数据。使用EXIF旋转的图片在由imagecreatefromjpeg((处理后将以原始方向显示。

缩略图可能只是显示其原始方向。这是假设你的图像查看器真的尊重这面旗帜。

此外,如果输入不是jpeg,则可能不应该使用imagecreatefromjpeg()