图片上传有效,缩略图歪斜?
Posted
技术标签:
【中文标题】图片上传有效,缩略图歪斜?【英文标题】:Image upload works, thumbnail is skewed? 【发布时间】:2015-09-22 23:14:02 【问题描述】:上传到我的服务器时,常规图像上传正常,但对于某些图像,在创建缩略图时,法师会旋转,我不知道为什么或我的代码发生了什么?
请注意,我使用的是旧语法,我知道它很丑,但我很懒,但它可以工作。
<? 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>
【问题讨论】:
您能否添加一张不适合缩略图处理的图片示例? 看起来你也使用 imagecreatefromjpeg() 而不管输入格式,虽然这应该只是失败而不是输出无聊的图像。 【参考方案1】:在 PHP 文档中,comment 112902 on imagecreatefromjpeg
的引用是这样说的:
imagecreatefromjpeg() 此函数不支持 EXIF 方向数据。使用 EXIF 旋转的图片,经过 imagecreatefromjpeg() 处理后会以原来的方向显示。
您的缩略图可能只是显示其原始方向。这是假设您的图像查看器实际上尊重该标志。
此外,如果输入不是 jpeg,您可能不应该使用 imagecreatefromjpeg()
。
【讨论】:
以上是关于图片上传有效,缩略图歪斜?的主要内容,如果未能解决你的问题,请参考以下文章