【问题标题】:Well this is new. Uploading photos from smartphones not possible?嗯,这是新的。无法从智能手机上传照片?
【发布时间】:2014-05-02 04:05:14
【问题描述】:

所以我正在创建一个响应式网站,可以选择将图像上传到页面。 php 脚本基本上是调整图像大小并在数据库中存储拇指文件路径。原始图像和拇指图像也存储在网站文件夹中。我正在使用 GD 库。

不管怎样,我只是在做一个从我的 iPhone 上传照片到网站的测试。它确实上传了图像。但是它有两个问题。

  1. 上传照片需要的时间太长。
  2. 上传完成后,照片是横向的。如果照片是纵向的,它将作为横向上传。很奇怪。

谁能帮我解决这两个问题?

更新代码

if (isset($_FILES['image'])) {

    if (empty($_FILES['image']['name'])) {

            ?><div class="add-errors">Please choose an image!</div><?php 

    }   else {


        function getOrientedImage($imagePath){
            $image = imagecreatefromstring(file_get_contents($imagePath));
            $exif = exif_read_data($imagePath);
            if(!empty($exif['Orientation'])) {
                switch($exif['Orientation']) {
                    case 8:
                        $image = imagerotate($image,90,0);
                        break;
                    case 3:
                        $image = imagerotate($image,180,0);
                        break;
                    case 6:
                        $image = imagerotate($image,-90,0);
                        break;
                }
            }
            return $image;
        }

        $name       =   $_FILES['image']['name'];
        $temp       =   $_FILES['image']['tmp_name'];
        $type       =   $_FILES['image']['type'];
        $size       =   $_FILES['image']['size'];
        $ext        =   strtolower(end(explode('.', $name)));
        $size2      =   getimagesize($temp);
        $width      =   $size2[0];
        $height     =   $size2[1];
        $upload     =   md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ));

        // Restrictions for uploading
        $maxwidth   =   6000;
        $maxheight  =   6000;
        $allowed    =   array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');

        // Recognizing the extension
        switch($type){

            // Image/Jpeg
            case 'image/jpeg':
                    $ext= '.jpeg';
            break;

            // Image/Jpg
            case 'image/jpg':
                    $ext= '.jpg';
            break;

            // Image/png
            case 'image/png':
                    $ext= '.png';
            break;

            // Image/gif
            case 'image/gif':
                    $ext= '.gif';
            break;
        }

        // upload variables
        $path           =   $userDir . $upload . $ext;
        $thumb_path     =   $userDir . 'thumb_' . $upload . $ext;

        // check if extension is allowed.
        if (in_array($type, $allowed)) {

            // Checking if the resolution is FULLHD or under this resolution.
            if ($width <= $maxwidth && $height <= $maxheight) {
                if ($size <= 5242880) {

                    // check the shape of the image
                    if ($width == $height) {$shape = 1;}
                    if ($width > $height) {$shape = 2;}
                    if ($width < $height) {$shape = 2;}

                    //Adjusting the resize script on shape.
                    switch($shape) {

                        // Code to resize a square image.
                        case 1:
                            $newwidth =     690;
                            $newheight =    690;
                        break;

                        // Code to resize a tall image
                        case 2:
                            $newwidth   =   690;
                            $ratio      =   $newwidth / $width;
                            $newheight  =   round($height * $ratio);

                        break;

                    }

                    // Resizing according to extension.
                    switch ($type) {

                        // Image/Jpeg   
                        case 'image/jpeg';
                            $img =      getOrientedImage($temp);
                            $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                        imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                        imagejpeg($thumb, $thumb_path);
                        break;

                        // Image/Jpg    
                        case 'image/jpg';
                            $img =      getOrientedImage($temp);
                            $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                        imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                        imagejpeg($thumb, $thumb_path);
                        break;

                        // Image/png    
                        case 'image/png';
                            $img =      getOrientedImage($temp);
                            $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                        imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                        imagepng($thumb, $thumb_path);
                        break;

                        // Image/gif    
                        case 'image/gif';
                            $img =      getOrientedImage($temp);
                            $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                        imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                        imagegif($thumb, $thumb_path);
                        break;
                    }


                        // Move the original file aswell.
                        move_uploaded_file($temp, $path);


                } else {
                    ?><div class="add-errors">Your image size is too big!</div><?php
                }
            } else {
                ?><div class="add-errors">Your image resolution exceeds the limit!</div><?php
            }

        } else {
            ?><div class="add-errors">Your have uploaded a forbidden extension!</div><?php

        }

    }

}

【问题讨论】:

  • 这可能已经超出了原始单一简洁问题的范围。我建议将问题提交到论坛。

标签: php image mobile upload


【解决方案1】:

您可能想要查看exif_read_data() 函数,并查看数组中返回的['Orientation'] 值。通常,具有方向传感器的手机或相机将图像存储在一个方向或另一个方向,然后将适当的方向标志添加到图片中的 exif 数据中。然后由图像查看器或图像处理器决定是否在显示或处理图像之前旋转原始图片。

该页面的 cmets 中有一些很好的示例。

根据该页面上的示例之一构建的函数:

<?php
    function getOrientedImage($imagePath){
        $image = imagecreatefromstring(file_get_contents($imagePath));
        $exif = exif_read_data($imagePath);
        if(!empty($exif['Orientation'])) {
            switch($exif['Orientation']) {
                case 8:
                    $image = imagerotate($image,90,0);
                    break;
                case 3:
                    $image = imagerotate($image,180,0);
                    break;
                case 6:
                    $image = imagerotate($image,-90,0);
                    break;
            }
        }
        return $image;
    }
?>

另外,关于上传时间,如果设备使用手机信号塔传输数据,您的上传速度可能只是下载速度的一小部分。为了比较起见,大多数社交网络应用程序会在上传之前调整图像大小,而您的网页可能不会。由于手机可以拍摄 8 兆像素或更大的照片,这会增加大量数据。

【讨论】:

  • 太棒了。我现在正在查看这些示例。
  • 由于我的网站不是一个应用程序,我认为没有办法事先调整它的大小。
  • 我认为使用 Javascript 库是可能的,但我不能肯定地说。
  • 实际上,这看起来很有希望:link
  • Js 在这一点上并不是我的强项。但我会检查一下。与此同时,我找到了一些关于方向的答案。我需要帮助来实施它。这是我的原始图像调整大小和上传代码。 codepad.viper-7.com/HXaHLW 这是我从这里得到的代码 codepad.viper-7.com/mDqKCy php.net/manual/en/function.exif-read-data.php。如何正确地将其添加到我的代码中?
【解决方案2】:

1) 不确定您是否可以对此采取任何措施,请上传用户选择的图片。在应用程序中,我可能会在传输之前缩小图像。

2) 无论您在查看照片(或生成缩略图)时正在查看什么内容,都可能不尊重照片中的方向标志。

【讨论】:

  • 典型的 iphone 图片在 3mb 和 4mb 之间。难怪上传需要一段时间。不幸的是,在上传之前没有办法缩小它。如果它提供了选择较小版本的选项,例如通过电子邮件发送照片时的方式。
猜你喜欢
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
相关资源
最近更新 更多