在不上传图像但使用一些已知的本地图像的情况下,以下以正确的方向生成了“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>