0
我有一個上傳腳本,用於將圖像上傳到我的服務器,以便在網頁上的幻燈片中顯示它們。不幸的是,上傳的iPhone圖像以錯誤的方向顯示。 這就是爲什麼我想在上傳圖片時旋轉圖片: 這可能嗎?如何在上傳時使用php來旋轉圖像?
這是我的代碼:
<?php
$upload_folder = 'uploads/prints/'; //Das Upload-Verzeichnis
$filename = pathinfo($_FILES['datei']['name'], PATHINFO_FILENAME);
$extension = strtolower(pathinfo($_FILES['datei']['name'], PATHINFO_EXTENSION));
//check extensions
$allowed_extensions = array('png', 'jpg', 'jpeg', 'gif');
if(!in_array($extension, $allowed_extensions)) {
die("Ungültige Dateiendung. Nur png, jpg, jpeg und gif-Dateien sind erlaubt");
}
//is there an error?
if(function_exists('exif_imagetype')) {
$allowed_types = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detected_type = exif_imagetype($_FILES['datei']['tmp_name']);
if(!in_array($detected_type, $allowed_types)) {
die("Nur der Upload von Bilddateien ist gestattet");
}
}
//Upload path
$new_path = $upload_folder.$filename.'.'.$extension;
echo "newpath1 $new_path <br>";
//new name if filename exists
if(file_exists($new_path)) { //if yes add a number
$id = 1;
do {
$new_path = $upload_folder.$filename.'_'.$id.'.'.$extension;
$id++;
} while(file_exists($new_path));
}
echo "newpath_2 $new_path <br>";
$FILES=$filename.".".$extension;
$exif_data=exif_read_data($_FILES['datei']['tmp_name']);
$Orientation=$exif_data['Orientation'];
$image=imagecreatefromstring($filename);
if(!empty($exif_data['Orientation'])) {
switch($exif_data['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
;
$image = imagerotate($image,-90,0);
move_uploaded_file($image);
echo 'Bild erfolgreich in die Schleife gelaufen : <a href="'.$new_path.'">'.$new_path.'</a><br>';
break;
}
}
//if no rotation needed
move_uploaded_file($_FILES['datei']['tmp_name'], $new_path);
echo 'Bild erfolgreich hochgeladen: <a href="'.$new_path.'">'.$new_path.'</a>';
?>
如果沒有需要旋轉,一切都很好。 我不知道如何結合move_uploaded_file和imagecreatefromstring或如何將創建的圖像存儲在我的上傳文件夾中。 感謝任何提示和幫助
我已經在代碼中使用它,但我不知道如何旋轉圖像或將旋轉保存在我的上傳文件夾中... – Flostens