3

重複の可能性:
アップロード時に画像のサイズを変更するphpは、画像を回転させたくないときに回転させます

私は初めてのアップロード コードを作成し、それをテストしていました。画像のサイズが変更され、正常にアップロードされます。私が持っている唯一の問題は、それが回転することです。

コードは次のとおりです。

$errors = array();
$message = "";

if(isset($_POST["test"])){

$name               = rand(1,999999) . $_FILES["uploadfile"]["name"];
$temp_name          = $_FILES["uploadfile"]["tmp_name"];
$size               = $_FILES["uploadfile"]["size"];
$extension          = strtolower(end(explode('.', $_FILES['uploadfile']['name'])));
$path               =   "testupload/" . $name;

$info               = getimagesize($temp_name);
$originalwidth      = $info[0];
$originalheight     = $info[1];
$mime               = $info["mime"];

$acceptedHeight     = 750;
$acceptedWidth      = 0;

$acceptedMimes = array('image/jpeg','image/png','image/gif');
$acceptedfileSize = 4102314;
$acceptedExtensions = array('jpg','jpeg','gif','png');
echo $size;
// check mimetype
if(!in_array($mime, $acceptedMimes)){$errors[] = "mime type not allowed - The file you have just uploaded was a: " . $extension . " file!";}
if(!$errors){if($size > $acceptedfileSize){$errors[] = "filesize is to big - Your file size of this file is: " . $size;}}
if(!$errors){if(!in_array($extension, $acceptedExtensions)){$errors[] = "File extension not allowed - The file you have just uploaded was a: " . $extension . " file!";}}

if(!$errors){

    // create the image from the temp file.
    if ($extension === 'png'){
        $orig = imagecreatefrompng($temp_name);
    }elseif ($extension === 'jpeg'){
        $orig = imagecreatefromjpeg($temp_name);
    }elseif ($extension === 'jpg'){
        $orig = imagecreatefromjpeg($temp_name);
    }elseif ($extension === 'gif'){
        $orig = imagecreatefromgif($temp_name);
    }

    // work out the new dimensions.
    if ($acceptedHeight === 0){
        $newWidth = $acceptedWidth;
        $newHeight = ($originalheight / $originalwidth) * $acceptedWidth;
    }else if ($acceptedWidth === 0){
        $newWidth = ($originalwidth / $originalheight) * $acceptedHeight;
        $newHeight = $acceptedHeight;
    }else{
        $newWidth = $acceptedWidth;
        $newHeight = $acceptedHeight;
    }

    $originalwidth = imagesx($orig);
    $originalheight = imagesy($orig);


    // make ssure they are valid.
    if ($newWidth  < 1){ $newWidth  = 1; }else{ $newWidth  = round($newWidth ); }
    if ($newHeight < 1){ $newHeight = 1; }else{ $newHeight = round($newHeight); }

    // don't bother copying the image if its alreay the right size.
    if ($originalwidth!== $newWidth || $originalheight !== $newHeight){

        // create a new image and copy the origional on to it at the new size.
        $new = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($new, $orig, 0,0,0,0, $newWidth, $newHeight, $originalwidth, $originalheight);

    }else{

        // phps copy on write means this won't cause any harm.
        $new = $orig;
    }

    // save the image.
    if ($extension === 'jpeg' || $extension === 'jpg'){
        imagejpeg($new, $path, 100);
    }else if ($save_ext === 'gif'){
        imagegif($new, $path);
    }else{
        imagepng($new, $path, round(9 - (100 / (100 / 9))));
    }

    $message = $path;

誰かが私に何が起こっているのか教えてもらえますか?

4

3 に答える 3

1

元の画像が実際に期待どおりの向きになっていることを確認してください。私は一日中画像を扱っており、ほとんどの場合、特定の方向で画像を表示する Windows フォト ビューアー (EXIF で方向の変更を読み取る) ですが、Photoshop で開いた場合は異なります。

于 2012-07-12T14:53:56.670 に答える
0

問題は、おそらく写真を撮影したデバイスからのEXIFデータが画像に埋め込まれていることです。

を使用して画像に回転情報が埋め込まれているかどうかを調査し、次のような関数exif_read_dataで回転を自動修正します。

于 2012-10-08T02:06:06.247 に答える
0

コードを 2 つの画像でテストしました。1 つは横向き (幅 > 高さ)、もう 1 つは縦向き (高さ > 幅) です。コードは機能します。

問題は@Tavocadoが言ったことのようです.新しいカメラには、写真が撮られたときにカメラの向きを検出するセンサーがあり、その情報を写真に保存します. また、新しい写真表示ソフトウェアは、その情報を写真から読み取り、画像を表示する前に回転させます。そのため、常に正しい向き (空が上、地球が下) の画像が表示されます。ただし、PHP 関数 (およびその他の機能) はその情報を使用せず、撮影された画像をそのまま表示します。つまり、自分の縦向きの画像を回転させる必要があります。

画像をブラウザーにロードするだけで (ファイルをアドレス バーにドラッグ)、画像が実際にどのようにファイルに保存されているかを自動回転なしで確認できます。

于 2012-07-12T15:45:17.353 に答える