微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 这是新的.无法从智能手机上传照片?

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

无论如何,我只是在测试从iphone上传照片到网站.它确实上传了图像.但是它有两个问题.

>上传照片需要太长时间.
>完成上传后,照片会侧向定向.如果照片是肖像照片,则会将其上传为横向照片.奇怪的.

有谁可以帮我解决这两个问题?

更新代码

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

        }

    }

}

解决方法:

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

页面评论中有一些很好的例子.

从该页面上的一个示例构建的函数

<?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;
    }
?>

此外,关于上传时间,如果设备使用手机信号塔传输数据,您的上传速度可能只是下载速度的一小部分.为了比较,大多数社交网络应用程序在上传之前调整图像大小,而您的网页可能不会.由于手机需要800万像素或更高的照片,因此会增加大量数据.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐