我有以下代码
$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 \“”,“位”:8,“频道”:3,“mime”:“image / jpeg”}
风景图片:
[2015年10月12日11:58:06欧洲/巴黎] tempFile:{“0”:1200,“1”:896,“2”:2,“3”:“width = \”1200 \ “height = \”896 \“”,“位”:8,“频道”:3,“mime”:“image / 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图片。
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
在没有上传图像但使用一些已知的本地图像的情况下,以下产生了" 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>