我有以下代码
$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是好的,但是当我想要创建一个调整大小的图片时,肖像图片会被旋转以完成风景图片。
$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图片有这个问题。
发布于 2015-10-13 10:11:38
问题是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;
}
}发布于 2015-10-12 11:22:48
在不上传图像的情况下,使用一些已知的本地图像,下面将生成正确方向的"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>https://stackoverflow.com/questions/33079284
复制相似问题