【问题标题】:getimagesize() don't returns correct height/width (from iPad/iPhone pictures)getimagesize() 不返回正确的高度/宽度(来自 iPad/iPhone 图片)
【发布时间】:2015-10-12 10:51:08
【问题描述】:

我有以下代码

$fn = 'temp_' . (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);

file_put_contents(

    '/www/htdocs/upload/' . $fn,
    file_get_contents('php://input')

);

$tempFile = '/www/htdocs/upload/' . $fn;

error_log( 'tempFile: ' . json_encode( getimagesize($tempFile) ) );

问题是,如果我上传纵向或横向图片,最大尺寸将始终是宽度。

人像图片:

[2015 年 10 月 12 日 11:57:41 欧洲/巴黎] tempFile: {"0":3264,"1":2448,"2":2,"3":"width=\"3264\ " height=\"2448\"","bits":8,"channels":3,"mime":"image/jpeg"}

风景图片:

[2015 年 10 月 12 日 11:58:06 欧洲/巴黎] tempFile: {"0":1200,"1":896,"2":2,"3":"width=\"1200\ " height=\"896\"","bits":8,"channels":3,"mime":"image/jpeg"}

我还有第二个问题:上传的tempFile是好的,但是当我要创建resized图片的时候,把纵向图片旋转做横向图片

$image = imagecreatefromjpeg( $tempFile );

$newPicture = imagecreatetruecolor( $width, $height );

imagecopyresampled($newPicture, $image, 0, 0, 0, 0, $width, $height, $width, $height);

imagejpeg($newPicture, "newPicture.jpg", 90);

编辑:我只有 iPad/iPhone 图片有这个问题。

【问题讨论】:

  • 这不是你的问题。这就是 iOS 在其文件系统上存储图片的方式。

标签: php


【解决方案1】:

问题在于 exif 数据中有一个名为 Orientation (http://sylvana.net/jpegcrop/exif_orientation.html) 的属性

如果设置为 6(第 0 行 = 右侧,第 0 列 = 顶部),图片需要旋转 -90 度。

$image = imagecreatefromjpeg( $img );

$sizes = getimagesize($img);
$exif = exif_read_data( $img );

if ( isset( $exif["Orientation"] ) ) {

    if ( $exif["Orientation"] == 6 ) {

        // photo needs to be rotated

        $image = imagerotate( $image , -90, 0 );

        $newWidth = $sizes[1];
        $newHeight = $sizes[0];

        $sizes[0] = $newWidth;
        $sizes[1] = $newHeight;

    }

}

【讨论】:

    【解决方案2】:

    在不上传图像但使用一些已知的本地图像的情况下,以下以正确的方向生成了“newPicture.jpg”版本。您使用的代码使用 $width$height 但这些代码似乎没有分配到任何地方 - 大概您试图从 getimagesize 获取这些代码?

    <?php
        $fn = 'temp_' . ( isset( $_SERVER['HTTP_X_FILENAME'] ) ? $_SERVER['HTTP_X_FILENAME'] : '' );
        if( $fn ){
    
            $target='/www/htdocs/upload/'.$fn;
            $src=@file_get_contents( 'php://input' );
            $bytes=@file_put_contents( $target, $src );
    
            if( $src && $bytes ) {
                $info=getimagesize( $target );
                list( $width, $height, $type, $attr ) = $info;
    
                error_log( 'tempFile: ' . json_encode( $info ) );
    
                $image = imagecreatefromjpeg( $target );
                $newPicture = imagecreatetruecolor( $width, $height );
                imagecopyresampled( $newPicture, $image, 0, 0, 0, 0, $width, $height, $width, $height );
                imagejpeg( $newPicture, "newPicture.jpg", 90 );
                imagedestroy( $image );
            }
        }
    ?>
    

    全面测试

    /test/uploadhandler.php
    -----------------------
    
    <?php
        $filename = ( isset( $_SERVER['HTTP_X_FILENAME'] ) ? $_SERVER['HTTP_X_FILENAME'] : false );
        if( $filename ){
            $rnd=uniqid('temp_');
            $outputdir='c:/temp/images/';
            $target=$outputdir.$rnd.$filename;
    
            $src=@file_get_contents( 'php://input' );
            /* Create the image using raw data */
            $bytes=@file_put_contents( $target, $src );
    
            if( $src && $bytes ) {
    
                $info=getimagesize( $target );
                list( $width, $height, $type, $attr ) = $info;
    
                /* Generate image using php functions */
                $image = imagecreatefromjpeg( $target );
                $newPicture = imagecreatetruecolor( $width, $height );
                imagecopyresampled( $newPicture, $image, 0, 50, 0, 50, $width, $height, $width, $height );
                imagejpeg( $newPicture, $outputdir."resampled.jpg", 75 );
                imagedestroy( $image );
            }   
        }
    ?>
    
    javascript
    ----------
    <script type='text/javascript'>
        function sotest_perform_upload( e ){
            /* 
                uploader is a prototype function to upload files via XHR
                which results in the use of the 'PUT' method. The various 
                options shown below are irrelevant for the purposes of the
                test and the function 'uploader' is not shown here.
            */
            var oUploader=new uploader({
                type:'image',
                usedir:false,
                debug:true,
                preview:false,
                rename:false,
                handler:'/test/uploadhandler.php'
            });
            oUploader.upload(e);    
        }
        function initialise(){
            document.getElementById('usrfile').onchange=sotest_perform_upload;
        }
        document.addEventListener( 'DOMContentLoaded', initialise, false );
    </script>
    
    
    
    html
    ----
    
        <form method="post" enctype="multipart/form-data">
            <input type="hidden" value="2621440" name="MAX_FILE_SIZE" id="MAX_FILE_SIZE">
            <input type="file" id='usrfile' name="usrfile">
    
            <canvas height="154" width="160" id="preview"></canvas>
            <progress value="0" max="100" id="progbar"></progress>
            <output id="output"></output>
        </form>
    

    【讨论】:

    • 是的,我有 list($width, $height, $type, $attr) = getimagesize($tempFile) 从上传的文件中获取宽度/高度
    • 我刚刚尝试了上面的代码(或多或少)上传图片,纵向和横向都生成了尺寸正确的图片。
    • 哪个操作系统?我在 2 个不同的 debian 服务器上尝试过
    • 经过一些测试,我只有 iPad/iPhone 图片有这个问题。
    猜你喜欢
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2019-01-28
    • 1970-01-01
    相关资源
    最近更新 更多