根据Exif数据和宽度/高度旋转上载的图像

时间:2019-04-08 10:58:07

标签: php exif

我一直试图将一些代码放在一起,但是我的PHP不好。我在这里阅读了一些类似的解决方案,但无法将它们全部一起使用。

我正在尝试从手机拍摄上传的图像,根据exif数据旋转它,然后将其保存到服务器。然后使用相同的图像,检查它是否为横向并旋转为纵向。然后缩放到1200px宽x 1800px高。然后保存到另一个文件夹。

    <?php
    function image_fix_orientation($filename) {
    $exif = exif_read_data($filename);
    if (!empty($exif['Orientation'])) {
        $image = imagecreatefromjpeg($filename);
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($image, 180, 0);
                break;

            case 6:
                $image = imagerotate($image, 90, 0);
                break;

            case 8:
                $image = imagerotate($image, -90, 0);
                break;
        }

        imagejpeg($image, $filename, 90);
    }
}


    $currentDir = getcwd();
    $uploadDirectory = "/uploads/";
    $printDirectory = "/prints/";

    $errors = []; // Store all foreseen and unforseen errors here

    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions

    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));

    $uploadPath = $currentDir . $uploadDirectory . basename($fileName);   
    $printPath = $currentDir . $uploadDirectory . $printDirectory . basename($fileName);   

    if (isset($_POST['submit'])) {

        $filename = $_FILES['myfile']['name'];
        $filePath = $_FILES['myfile']['tmp_name'];

        //get the image sizes
        $sizes = getimagesize($filePath);

        $width=$sizes[0];
        $height=$sizes[1];

        $source = imagecreatefromjpeg($filePath);
        $source = image_fix_orientation($source);
        // save a copy to the uploads folder for slideshow
    imagejpeg($source, $uploadPath, 90);

        if($width>$height){
            //rotate
            $image1 = imagescale($source, 1800, 1200);
            $image = imagerotate($image1, 90, 0);

            }else{
            //leave it
           $image = imagescale($source, 1200, 1800);
        }   

        //save in /uploads
        $didUpload = imagejpeg($image, $printPath, 100); 

            if ($didUpload) {


        echo "<h1>Thanks for your Photo!</h1><br>";
        echo "<p>Collect it from the printer.<p><br>";


            } else { echo "An error occurred somewhere. Try again or contact the admin"; }

} 

我正在尝试: -允许用户通过html表单上传图片 -使用exif检查图像的方向,必要时旋转图像,使其显示为拍摄时的样子 -将副本保存在服务器上 -检查宽度>高度,如果为true,则旋转 -缩放至1200px宽x 1800px高 -将此图像保存到其他文件夹。 (此操作无效,因为未保存任何图片)

0 个答案:

没有答案