首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getimagesize()不返回正确的高度/宽度(从iPad/iPhone图片)

getimagesize()不返回正确的高度/宽度(从iPad/iPhone图片)
EN

Stack Overflow用户
提问于 2015-10-12 10:51:08
回答 2查看 1.7K关注 0票数 0

我有以下代码

代码语言:javascript
运行
复制
$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日-2015年10月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日-2015年10月11日:58:06欧洲/巴黎tempFile:{"0":1200,"1":896,“2”,"3":"width=\"1200\“height=\”896\“,”位“”:8,“信道”:3,“mime”:“图像/jpeg”}

我有第二个问题:上传的tempFile是好的,但是当我想要创建一个调整大小的图片时,肖像图片会被旋转以完成风景图片。

代码语言:javascript
运行
复制
$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图片有这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-13 10:11:38

问题是exif数据中有一个名为方位(orientation.html)的属性。

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

代码语言:javascript
运行
复制
$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;

    }

}
票数 1
EN

Stack Overflow用户

发布于 2015-10-12 11:22:48

在不上传图像的情况下,使用一些已知的本地图像,下面将生成正确方向的"newPicture.jpg“版本。您所使用的代码使用$width & $height,但是这些代码似乎没有被分配到任何地方--想必您是试图从getimagesize获得这些代码。

代码语言:javascript
运行
复制
<?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 );
        }
    }
?>

全试验

代码语言:javascript
运行
复制
/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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33079284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档