我有一个网站,用户可以从手机上传照片,但是从手机上传的所有照片在上传时都显示在左侧90度。实时网站为www.uneraportoj.com
。我现在的问题是exeif,但我尝试使用插件,但无法正常工作。建议任何帮助。
共享脚本代码是:
<?php
include_once('help.php');
if(isset($_POST['submit'])){
$title = $_POST['title'];
$content = $_POST['content'];
$posted = $_POST['posted'];
$date = date("Y-m-d H:i:s");
$ip = $_SERVER["REMOTE_ADDR"];
$rand = rand(1,1000000);
if(empty($title)){
echo "Titulli eshte bosh.";
}else if(empty($content)){
echo "Permbajtja eshte bosh.";
}else if(empty($_FILES['image']['name'])){
echo "Imazhi eshte bosh.";
}else if (
($_FILES['image']['type'] == 'image/gif')
|| ($_FILES['image']['type'] == 'image/jpeg')
|| ($_FILES['image']['type'] == 'image/pjpeg')
|| ($_FILES['image']['type'] == 'image/png')
&& ($_FILES['image']['size'] < 200000)
){
$part = $rand.'_'.$_FILES['image']['name'];
if ($_FILES["image"]["error"] > 0) {
echo "return code" . $_FILES['image']['error'];
//Code from plugin start
if($_FILES['image']){
// If the image is jpg and has orientation data, make sure we orientate correctly before uploading
if($image->exif('Orientation'))
$image = orientate($image, $image->exif('Orientation'));
}
}else if(move_uploaded_file($_FILES['image']['tmp_name'],'images/'. $part.'')){
if(empty($posted)){
$posted = 'Anonim';
}
$sql = "INSERT INTO ***(title, content, date, image, posted, ip) VALUES (:title, :content, :date, :image, :posted, :ip)";
$query = $db->prepare($sql);
$results = $query->execute(array(
':title' => htmlentities ($title),
':content' => htmlentities ($content),
':date' => $date,
':image' => $part,
':posted' => htmlentities ($posted),
':ip' => $ip
));
echo "<div id='ok'>Lajmi u raportua me sukses. Kontrollojeni <a href='index.php'>ketu</a> .</div>";
}
}else{
echo "<div id='ok'>Imazhi nuk eshte i sakte. (Vetem jpg/png)</div>";
}
}
?>
插件代码:
<?php /**
* Orientate an image, based on its exif rotation state
*
* @param Intervention\Image\Image $image
* @param integer $orientation Image exif orientation
* @return Intervention\Image\Image
*/
$image = $_FILES['image'];
function orientate($image, $orientation)
{
switch ($orientation) {
// 888888
// 88
// 8888
// 88
// 88
case 1:
return $image;
// 888888
// 88
// 8888
// 88
// 88
case 2:
return $image->flip('h');
// 88
// 88
// 8888
// 88
// 888888
case 3:
return $image->rotate(180);
// 88
// 88
// 8888
// 88
// 888888
case 4:
return $image->rotate(180)->flip('h');
// 8888888888
// 88 88
// 88
case 5:
return $image->rotate(-90)->flip('h');
// 88
// 88 88
// 8888888888
case 6:
return $image->rotate(-90);
// 88
// 88 88
// 8888888888
case 7:
return $image->rotate(-90)->flip('v');
// 8888888888
// 88 88
// 88
case 8:
return $image->rotate(90);
default:
return $image;
}
}
?>
答案 0 :(得分:1)
我发现这个youtube视频对exif数据的图像oriantaion非常有帮助 看看这个 https://www.youtube.com/watch?v=HjHSgGFqAtE
print 'exif orientation';
$original_filename = 'car1.jpg';
$exif_data = exif_read_data($original_filename);
// displays exif data
// print '<pre>';
// print_r($exif_data);
// print '<pre>';
$orientation = orientation($exif_data);
$degrees = orientation_flag($orientation);
print '<pre>';
print_r($orientation);
print '</pre>';
print '<pre>';
print_r($degrees);
print '</pre>';
$image_data = imagecreatefromjpeg($original_filename);
$image_rotate = imagerotate($image_data, $degrees, 0);
$rotated_filename = 'rotated_' . $original_filename;
imagejpeg($image_rotate, $rotated_filename);
imagedestroy($image_data);
imagedestroy($image_rotate);
// finds orientation value in exif data
function orientation($exif_data) {
// search array for orientation
foreach($exif_data as $key => $val) {
// print '<pre>';
// print_r($key);
// print '<pre>';
if(strtolower($key) == 'orientation') {
return $val;
}
}
}
// gets orientation data and returns degrees needed for rotation
function orientation_flag($orientation) {
switch($orientation):
case 1:
return 0;
case 8:
return 90;
case 3:
return 180;
case 6:
return 270;
endswitch;
}
?>
<img src="car1.jpg" width="400"/>
<br>
<img src="rotated_car1.jpg" width="400"/>
答案 1 :(得分:0)
您在if(error)语句下进行了函数调用,这意味着只有在出现错误时才会旋转。在图像通过错误检查后将其移动到最终位置。
if ($_FILES["image"]["error"] > 0) {
echo "return code" . $_FILES['image']['error'];
}else if(move_uploaded_file($_FILES['image']{'tmp_name'],'images/'. $part.'')){
if(file_exists('images/'. $part.'')){
/* read exif data (returns it as an array) */
$exif_read = exif_read_data('images/'. $part.'');
/* if exif contains orientation property (some images don't) */
if(!empty($exif_read['Orientation'])){
$orientation_data = $exif_read['Orientation'];
$image = orientate($image, $Orientation_data);
}
修改强>
我复制并粘贴了用于图片上传的确切代码。对于我正在做的事情,我只需要担心方向3,6和8,但如果您认为自己需要它们,可以添加其余部分。在imagerotate()之后使用PHP的imageflip()函数(阅读如何使用here)
<?php
require("../db_credentials.php");
if($_FILES['file']['name']){
$name = htmlspecialchars($_FILES['file']['name']);
$ext = end((explode(".", $name)));
$ext = strtolower($ext);
//if no errors...
if(!$_FILES['file']['error']){
//now is the time to modify the future file name and validate the file
$new_file_name = date('ymdHisu'). ".". $ext;
if($_FILES['file']['size'] > (6144000)){
$valid_file = false;
echo 'Oops! Your file\'s size is to large.';
}
elseif($ext !== "jpg" && $ext !== "png" && $ext !== "jpeg" && $ext != "gif" && $ext !== "bmp") {
$valid_file = false;
echo "Your file must be in jpg, jpeg, png, gif, or bmp formats.";
}
else{
$valid_file = true;
}
//if the file has passed the test
if($valid_file){
//move it to where we want it to be
move_uploaded_file($_FILES['file']['tmp_name'], 'images/'.$new_file_name);
$exif_read = exif_read_data("images/".$new_file_name);
if(!empty($exif_read['Orientation'])){
$orientation_data = exif_read_data("images/".$new_file_name)['Orientation'];
}
if(isset($orientation_data) && $orientation_data !== 1){
$path = "../images/". $new_file_name;
$buffer = ImageCreateFromJPEG($path);
$exif = exif_read_data($path);
if(!empty($exif['Orientation'])){
switch($exif['Orientation']){
case 8:
$buffer = imagerotate($buffer,90,0);
break;
case 3:
$buffer = imagerotate($buffer,180,0);
break;
case 6:
$buffer = imagerotate($buffer,-90,0);
break;
}
imagejpeg($buffer, $path, 90);
}
}
}
}
//if there is an error...
else
{
//set that to be the returned message
echo 'Ooops! Your upload triggered the following error: '.$_FILES['file']['error'];
}
}
?>