在前端上传似乎工作,虽然所有文件都是成功创建的,但它们都是全黑的。我确定这是一件很简单的事情,我做错了但是我已经盯着它看了一段时间,我只是没有得到它。
我在php中也没有受过训练,而且我一直在努力教自己。
我一直按比例调整图像大小,有些宽度和高度确实有实数,这有什么不同吗?我应该将它们舍入到最近的像素吗?
以下是发生错误的代码部分。我想我知道引起问题的那一行,所以我用“// error”标记了它。
//Resize proportionally
for($i=0;$i<$image_count;$i++){
$size = getimagesize($_FILES["fileToUpload"]["tmp_name"][$i]);
$ratio = $size[0]/$size[1]; // width/height
echo "<BR>Original width = ".$size[0]." Original height = ".$size[1]."<BR>";
if( $ratio > 1) {
$width = 500;
$height = 500/$ratio;
}
else {
$width = 500*$ratio;
$height = 500;
}
echo "<BR>new width = ".$width." new height = ".$height."<BR>";
// resample
$image_p[$i] = imagecreatetruecolor($width, $height);
$image[$i] = imagecreatefromjpeg($_FILES["fileToUpload"]["tmp_name".$i][$i]); //error
imagecopyresampled($image_p[$i], $image[$i], 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
//echo "<BR>image".$i." has been resized and resampled";
}
我还编辑了我的php.ini文件以包含以下内容。
1. ; Maximum size of POST data that PHP will accept.
2. post_max_size = 700M
3.
4. ; Maximum number of files. Added by DJ
5. max_file_uploads=500
6.
7. ; Maximum allowed size for uploaded files. Added by DJ
8. upload_max_filesize = 50M
任何帮助将不胜感激。
答案 0 :(得分:0)
最初我不确定脚本的预期结束,因为发布的代码只是一部分,但如果我现在已经正确理解,那么图像处理的最终目标是保存上传文件的修改版本?< / p>
<?php
define('KB',1024);
define('MB',pow(KB,2));
define('GB',pow(KB,3));
define('TB',pow(KB,4));
function uploaderror( $error ){
switch( $error ) {
case UPLOAD_ERR_INI_SIZE: return "The uploaded file exceeds the upload_max_filesize directive in php.ini";
case UPLOAD_ERR_FORM_SIZE: return "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
case UPLOAD_ERR_PARTIAL: return "The uploaded file was only partially uploaded";
case UPLOAD_ERR_NO_FILE: return "No file was uploaded";
case UPLOAD_ERR_NO_TMP_DIR: return "Missing a temporary folder";
case UPLOAD_ERR_CANT_WRITE: return "Failed to write file to disk";
case UPLOAD_ERR_EXTENSION: return "File upload stopped by extension";
default: return "Unknown upload error";
}
}
function size( $i ) {
if ( is_numeric( $i ) ) {
if( $i > 0 && $i < KB ) {
return "{$i} bytes";
} elseif( $i >= KB && $i < MB ) {
return round( ( $i / KB ),2 )."Kb";
} elseif( $i >= MB && $i < GB ) {
return round( ( $i / MB ),2 )."Mb";
} elseif( $i >= GB && $i < TB ) {
return round( ( $i / GB ),2 )."Gb";
} elseif( $i >= TB ) {
return round( ( $i / TB ),2 )."Tb";
} else {
#Computer will die
}
}
}
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_FILES ) ){
$errors=array();
$feedback=array();
/* maximum width or height */
$maxdim=500;
/* Obtain reference to files collection */
$files=(object)$_FILES['fileToUpload'];
/* location images will be saved to */
$outputdir='c:/temp/fileuploads/2/';
/* Process all files */
if( count( $files->name ) > 1 ){
/* Iterate through files collection */
foreach( $files->name as $i => $void ){
try{
$name = $files->name[$i];
$size = size( $files->size[$i] );
$type = $files->type[$i];
$tmp = $files->tmp_name[$i];
$error= $files->error[$i];
/* save the file as */
$filename = $outputdir . $name;
if( !$error == UPLOAD_ERR_OK or !is_uploaded_file( $tmp ) ) throw new Exception( uploaderror( $error ) );
else {
list( $w, $h, $t, $a ) = getimagesize( $tmp );
$ratio = $w / $h;
if( $ratio==1 ){
$width=$height=$maxdim;
} else {
$width=ceil( $maxdim * $ratio );
$height=$maxdim;
}
$target=imagecreatetruecolor( $width, $height );
$source=imagecreatefromjpeg( $tmp );
$result=imagecopyresampled( $target, $source, 0, 0, 0, 0, $width, $height, $w, $h );
if( $result ){
/* save the modified image to disk */
$status=imagejpeg( $target, $filename );
$newsize = size( filesize( $filename ) );
/* free resources */
imagedestroy( $target );
imagedestroy( $source );
/* delete uploaded temp file */
@unlink( $tmp );
if( !$status )throw new Exception( 'Unable to save ' . $filename );
else $feedback[]="Success: '{$name}' saved as '{$filename}' was {$size} now {$newsize} - resized from dimensions [ W:{$w}, H:{$h} ] to [ W:{$width}, H:{$height} ] ";
}
}
}catch( Exception $e ){
$errors[]=$e->getMessage();
continue;
}
}
if( !empty( $errors ) ) echo '<pre>',print_r($errors,true),'</pre>';
if( !empty( $feedback ) ) echo '<pre>',print_r($feedback,true),'</pre>';
}
}
?>