如何基于exif旋转文件夹中的所有图像,并使用php覆盖旧照片

如何基于exif旋转文件夹中的所有图像,并使用php覆盖旧照片,php,html,image,Php,Html,Image,我有一组照片通过ftp上传到/photos(一些是.JPG.JPG.png等),但不是所有的都正确旋转我有一个php脚本,可以告诉我需要旋转多少才能正确旋转(景观/肖像),但我似乎无法解决如何保存它们或进行实际旋转 我试过了 //define image path $filename="image.jpg"; // Load the image $source = imagecreatefromjpeg($filename); // Rotate $rotate = imagerotate(

我有一组照片通过ftp上传到/photos(一些是.JPG.JPG.png等),但不是所有的都正确旋转我有一个php脚本,可以告诉我需要旋转多少才能正确旋转(景观/肖像),但我似乎无法解决如何保存它们或进行实际旋转

我试过了

//define image path
$filename="image.jpg";

// Load the image
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

//and save it on your server...
imagejpeg($rotate, "myNEWimage.jpg");
以及:


我当前的代码:



没有错误消息。此脚本位于照片文件夹内(出于调试原因),但它可以输出照片,而不旋转并保存它们

您的代码不符合逻辑,您需要对
$文件使用
$rotate

查看主要文档

我找到了一个解决方案,在某人的eles代码中添加一点,使其扫描文件夹并编辑所有照片,我相信我可以提高效率



我曾尝试在foreach内部的
$I
上使用
$rotate
,但仍然无法使其工作。我不知道你说我的代码不是什么意思logic@SirCharles您已经使用了
foreach
double来处理
$files
应该更正它,然后使用
$rotate
每一个文件的第二个都是用于调试的,以确保保存旋转的文件我遇到的问题是
$rotate
不会旋转,然后我就不知道了如何正确保存输出
<?php
    $files = glob('{**.jpg,*.JPG,*.png, *.PNG, *.PNG}',GLOB_BRACE);
    echo '<pre>'; print_r($files); echo '</pre>';
    foreach ($files as $i)
    {
        // GET Rotate
        $exif = @exif_read_data($i,0,true);
        $orientation = @$exif['IFD0']['Orientation'];
        if($orientation == 7 || $orientation == 8) {
            $degrees = 90;
            echo " | 90";
        } elseif($orientation == 5 || $orientation == 6) {
            $degrees = 270;
            echo " | 270";
        } elseif($orientation == 3 || $orientation == 4) {
            $degrees = 180;
            echo " | 180";
        } else {
            $degrees = 0;
            echo " | 0";
        }
        $filename= $i;
        $source = imagecreatefromjpeg($filename);
        $rotate = imagerotate($source, $degrees, 0);
        //and save it on your server...
        imagejpeg($rotate, $i);
    }
echo '<pre>'; print_r($files); echo '</pre>';
echo"ROTATED:   ";
foreach ($files as $i){
    printf("<img style='max-height: 100px;'src='%s'/>", basename($i));
}

?>
<?php
    $files = glob('{**.jpg,*.JPG,*.png, *.PNG, *.PNG}',GLOB_BRACE);
    echo '<pre>'; print_r($files); echo '</pre>';
    foreach ($files as $i)
    {
        // GET Rotate
        $exif = @exif_read_data($i,0,true);
        $orientation = @$exif['IFD0']['Orientation'];
        if($orientation == 7 || $orientation == 8) {
            $degrees = 90;
            echo " | 90";
        } elseif($orientation == 5 || $orientation == 6) {
            $degrees = 270;
            echo " | 270";
        } elseif($orientation == 3 || $orientation == 4) {
            $degrees = 180;
            echo " | 180";
        } else {
            $degrees = 0;
            echo " | 0";
        }
}
echo '<pre>'; print_r($files); echo '</pre>';
echo"ROTATED:   ";
foreach ($files as $i){
    printf("<img style='max-height: 100px;'src='%s'/>", basename($i));
}

?>
<?PHP
/**
 * Document   : EXIF Image Rotate Class
 * OG Author     : josephtinsley
 * Edited by    : Sir Charles (Added for each loop to edit more than one file <3)
 * Description: PHP class that detects a JPEG image current orientation and rotates a image using the images EXIF data.
 * http://twitter.com/josephtinsley
*/
class RotateImage {
    /*
     * @param string $setFilename - Set the original image filename
     * @param array $exifData - Set the original image filename
     * @param string $savedFilename - Set the rotated image filename
     */

    private $setFilename    = "";
    private $exifData       = "";
    private $degrees        = "";

    public function __construct($setFilename) 
    {
        try{
            if(!file_exists($setFilename))
            {
                throw new Exception('File not found.');
            } 
            $this->setFilename = $setFilename;
        } catch (Exception $e ) {
            die($e->getMessage());
        } 
    }
    /*
     * EXTRACTS EXIF DATA FROM THE JPEG IMAGE
     */
    public function processExifData() 
    {
        $orientation = 0;

        $this->exifData = exif_read_data($this->setFilename);

        foreach($this->exifData as $key => $val)
        {
            if(strtolower($key) == "orientation" )
            {
                $orientation = $val;
                break;
            }
        }
        if( $orientation == 0 )
        {
            $this->_setOrientationDegree(1);
        }

        $this->_setOrientationDegree($orientation); 
    } 
    /*
     * DETECTS AND SETS THE IMAGE ORIENTATION
     * Orientation flag info  http://www.impulseadventure.com/photo/exif-orientation.html
     */
    private function _setOrientationDegree($orientation)
    {
       switch($orientation):
           case 1: 
               $this->degrees = 0;
               break;
           case 8:
               $this->degrees = 90;
               break;
           case 3:
               $this->degrees = 180;
               break;
           case 6:
               $this->degrees = 270;
               break;
       endswitch;
       $this->_rotateImage();
    }  
    /*
     * ROTATE THE IMAGE BASED ON THE IMAGE ORIENTATION
     */
    private function _rotateImage() 
    {
        if($this->degrees < 1 )
        {
            return FALSE;
        }
        $image_data = imagecreatefromjpeg($this->setFilename);
        return imagerotate($image_data, $this->degrees, 0);  
    } 
    /*
     * SAVE THE IMAGE WITH THE SAME FILENAME
     */
    public function savedFileName($savedFilename) 
    {
        if($this->degrees < 1 )
        {
            return false;   
        }
        $imageResource = $this->_rotateImage();
        if($imageResource == FALSE)
        {
            return false;   
        }
        imagejpeg($imageResource, $savedFilename);  
    } 
} //END CLASS
    $files = glob('{**.jpg,*.JPG,*.png, *.PNG, *.PNG}',GLOB_BRACE); //Get all files in current dir with extenshions .jpg .JPG .png .PNG .PNG (CASE SENSITIVE)
    echo '<pre>'; print_r($files); echo '</pre>';//print the arry so you know what files where edited 
    foreach ($files as $i)
    {
        $imageFile = $i;//just pass on this var in case you want to specific a set file change $i to "FILE_NAME.JPG"
        $savedFile = $imageFile;//if you want to have it under a new file name use $savedFile = "rotated_".$imageFile;

        $rotate = new RotateImage($imageFile);

        $rotate->processExifData();
        $rotate->savedFileName($savedFile);

       // print '<img src="'.$imageFile.'" width="250"/>'."<br>";// this is for if you save it under a diffrent name to see the diffrence (good for debuging)
        print '<img src="'.$savedFile.'" width="250"/>'."<br>"; //!SHOW THE SAVED ROTATED IMAGE
        print '<h1>'.$i.'</h1><br>';//show the image name under the img (good for debbuging)
    }
?>