【问题标题】:How to resize image after upload上传后如何调整图片大小
【发布时间】:2013-10-11 08:10:08
【问题描述】:

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

谢谢!

    <?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>

【问题讨论】:

    标签: php image upload resize


    【解决方案1】:

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

    if($extension=="jpg" || $extension=="jpeg"){ $uploadedfile = $_FILES['files']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); }否则如果($extension=="png"){ $uploadedfile = $_FILES['files']['tmp_name']; $src = imagecreatefrompng($uploadedfile); }别的{ $src = imagecreatefromgif($uploadedfile); } $width = 图像x($src); $高度=图像($src); $tmp_filename = _file_name_here_; // 大的 $new_width = 你的宽度; $new_height = 你的高度; $k_w = 1; $k_h = 1; $dst_x =0; $dst_y =0; $src_x =0; $src_y =0; //选择偏移量 if($new_width>$width ){//按宽度 $dst_x = ($new_width-$width)/2; } if($new_height>$height){//按高度 $dst_y = ($new_height-$height)/2; } 如果($new_width$height){ $src_x = ($width-$new_width)/2; }否则如果($new_width>$width){ $src_y = ($height-$new_height)/2; }别的{ 如果($k_h>$k_w){ $src_x = round(($width-($new_width/$k_h))/2); }别的{ $src_y = round(($height-($new_height/$k_w))/2); } } } //创建具有新尺寸的新空图像 $tmp_large=imagecreatetruecolor($new_width, $new_height); // 此函数将旧图像复制到新图像上,根据我们之前设置的偏移量调整大小和裁剪 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); // 这里我们设置我们要保存新图像的位置 $filename = $sys_root.$tmp_filename .".png"; // 保存图片 imagepng($tmp_large,$filename);

    通过代码,我试图注释掉动作,所以你明白发生了什么。 对不起我的英语。

    【讨论】:

    • 您需要根据自己的需要修改此代码,而不仅仅是粘贴它,请阅读它,并尝试找到与您的代码的连接。这是你学习的唯一方法。
    【解决方案2】:
    if( $new_width$height){
    }
    

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

    if( $new_width.height){}
    

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      相关资源
      最近更新 更多