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

时间:2014-04-05 10:10:31

标签: php image upload resize-image


我有一个问题我无法解决:(,我想让代码在上传后重新调整图像并将其保存在文件夹中

我的代码:

<form method="post" action="" enctype="multipart/form-data">
<?php
$filename=$_FILES['f1']['name'];
$file=$_FILES['f1']['tmp_name'];
$ext=strtolower(end(explode(".",$filename)));
if ($ext=="jpg" or $ext=="jpeg" or $ext=="png" or $ext=="gif" ){
$newfilename= $t2.".".$ext;
copy ($file,"images/profiles/".$newfilename);
?>
<table dir="ltr" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right" width="84%">
<input name="f1" type="file" class="btn" id="f1"  value=""/></td>
<td width="16%">image:</td>
</tr><tr>
</tr>
</table>
</form>

3 个答案:

答案 0 :(得分:2)

我写了一个函数来调整Wordpress插件中的图像大小。 只需将此函数复制到脚本并使用所需的参数调用它。

<?php
/* 
 * Author : Azi Baloch
 * Date Created : Saturday March 8 2014
 * 
 */

function CreateThumbs($src, $dst, $width, $height, $crop=0){

  if(!list($w, $h) = getimagesize($src)) return "Invalid Picture Type";

  $type = strtolower(substr(strrchr($src,"."),1));
  if($type == 'jpeg') $type = 'jpg';
  switch($type){
    case 'bmp': $img = imagecreatefromwbmp($src); break;
    case 'gif': $img = imagecreatefromgif($src); break;
    case 'jpg': $img = imagecreatefromjpeg($src); break;
    case 'png': $img = imagecreatefrompng($src); break;
    default : return "Invalid Picture Type!";
  }

  // resize
  if($crop){
    if($w < $width or $h < $height) return "Picture is too small!";
    $ratio = max($width/$w, $height/$h);
    $h = $height / $ratio;
    $x = ($w - $width / $ratio) / 2;
    $w = $width / $ratio;
  }
  else{
    if($w < $width and $h < $height) return "Picture is too small!";
    $ratio = min($width/$w, $height/$h);
    $width = $w * $ratio;
    $height = $h * $ratio;
    $x = 0;
  }

   $new = imagecreatetruecolor($width, $height);
  if($type == "gif" or $type == "png"){
    imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
    imagealphablending($new, false);
    imagesavealpha($new, true);
  }

  imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);

  switch($type){
    case 'bmp': imagewbmp($new, $dst); break;
    case 'gif': imagegif($new, $dst); break;
    case 'jpg': imagejpeg($new, $dst); break;
    case 'png': imagepng($new, $dst); break;
  }

  return true;
} ?>

答案 1 :(得分:0)

相反您可以使用jquery调整图像大小,然后上传到文件夹[文件系统]。

$("#img_id").css({"width":"your value","height":"your value"});

然后使用move_uploaded_file函数

存储它

答案 2 :(得分:0)

$rsr_org = imagecreatefromjpeg('path/to/image');
$rsr_scl = imagescale($rsr_org, new_width, new_height);
$newimg = imagejpeg($rsr_scl, 'path/to/new/image/image_name');
imagedestroy($rsr_org);
imagedestroy($rsr_scl);
相关问题