如何在上传后调整图像大小

时间:2013-10-11 08:10:09

标签: php image upload resize

我有一张效果很好的图片上传表单。它上传到一个文件夹,但我希望它为上传的文件提供特定的高度和宽度,以便它适合页面。有人可以帮我吗?

谢谢!

    <?php


$uploadpath = 'upload/';     
$max_size = 2000;          
$alwidth = 250;            
$alheight = 400;           
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');        

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       
  $sepext = explode('.', strtolower($_FILES['fileup']['name']));
  $type = end($sepext);       // gets extension
  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     
  $err = '';         


  if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/> Maximale waarden: '. $alwidth. ' x '. $alheight . '&nbsp probeer opnieuw';


  if($err == '') {
    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
      echo "Uw afbeelding is succesvol opgeslagen";
    }
    else echo '<b>Unable to upload the file.</b>';
  }
  else echo $err;
}
?> 
<div style="width:500px;;margin:1em auto; width:333px; text-align:center;">
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
   <input type="file" name="fileup" /><br/>
  <input type="submit" name='submit' value="Upload" /> 
 </form>
</div>

</div>
</div></div>
</div>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

我认为这对你有用,至少在开始时。


    if($extension=="jpg" || $extension=="jpeg" ){

        $uploadedfile = $_FILES['files']['tmp_name'];
        $src = imagecreatefromjpeg($uploadedfile);

    }else if($extension=="png"){

        $uploadedfile = $_FILES['files']['tmp_name'];
        $src = imagecreatefrompng($uploadedfile);

    }else{

        $src = imagecreatefromgif($uploadedfile);

    }       

    $width = imagesx($src);
    $height = imagesy($src);


    $tmp_filename = _file_name_here_;


    // LARGE

    $new_width = YOUR_WIDTH ;
    $new_height = YOUR_HEIGHT ;
    $k_w = 1;
    $k_h = 1;
    $dst_x =0;
    $dst_y =0;
    $src_x =0;
    $src_y =0;

    //seclecting_offsets
    if($new_width>$width ){//by width
        $dst_x = ($new_width-$width)/2;
    }

    if($new_height>$height){//by height
        $dst_y = ($new_height-$height)/2;
    }

    if( $new_width$height){
            $src_x  = ($width-$new_width)/2;
        }else if ($new_width>$width){
                $src_y  = ($height-$new_height)/2;
        }else{
            if($k_h>$k_w){
                $src_x = round(($width-($new_width/$k_h))/2);
            }else{
                $src_y = round(($height-($new_height/$k_w))/2);
            }
        }
    }   

    //create new empty image with new size
    $tmp_large=imagecreatetruecolor($new_width, $new_height);

    // this function copies old image on to new, resizes and crops according to offsets we set before
    imagecopyresampled($tmp_large,$src,$dst_x, $dst_y, $src_x, $src_y,$new_width-2*$dst_x, $new_height-2*$dst_y, $width-2*$src_x, $height-2*$src_y);

    // here we set where we want to save new image
    $filename = $sys_root.$tmp_filename .".png";

    // SAVE IMAGE
    imagepng($tmp_large,$filename);

通过代码,我试图评论行动,所以你了解发生了什么。 抱歉我的英文。

答案 1 :(得分:1)

if( $new_width$height){
}

//此处缺少连接运算符 //将其修改为

if( $new_width.height){}