图片上传工作,缩略图是否有偏差?

时间:2015-07-05 21:26:14

标签: php image-processing thumbnails image-uploading

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

请注意我使用的是旧语法,我知道它很丑,但我很懒,而且很有效。

<? 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>

1 个答案:

答案 0 :(得分:0)

从PHP文档中,comment 112902 on imagecreatefromjpeg的引用说明了这一点:

  

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

您的缩略图可能只是显示其原始方向。假设您的图像查看器实际上尊重该标志。

如果输入不是jpeg,你也可能不应该使用imagecreatefromjpeg()