上传调整大小图片功能php

时间:2013-12-19 12:43:12

标签: php

我想让这段代码按照描述运行,只需要指定路径,我不知道在哪里添加它。

此代码正常工作,我想添加路径将图像上传到特定文件夹吗?

<?php

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

  if(!list($w, $h) = getimagesize($src)) return "Unsupported 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 "Unsupported 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);

  // preserve transparency
  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;
}

if(isset($_POST['submitButton'])){


   $picture=$_FILES['image1'];

   $pic_type = strtolower(strrchr($picture['name'],"."));
   $pic_name = "original$pic_type";
   move_uploaded_file($picture['tmp_name'], $pic_name);
   if (true !== ($pic_error = @image_resize($pic_name, "100x100$pic_type", 100, 100, 1))) {
    echo $pic_error;
     unlink($pic_name);
   }
   else echo "OK!";
  }
?>





<form method="post" action="" enctype="multipart/form-data" >

    <p>
        <input type="file" name="image1" />
    </p>

    <input type="submit" name="submitButton" value="save"/>

</form>

我通过添加$ pic_name&amp;的文件夹路径解决了这个问题。 “100×100 $ PIC_TYPE”:)

2 个答案:

答案 0 :(得分:0)

你的第一和第二个论点 image_resize($src, $dst, $width, $height, $crop=0)函数分别获取图片的源路径和目标路径。

因此,在您的代码中,您应该编辑以下部分:

$pic_name = "original$pic_type";
move_uploaded_file($picture['tmp_name'], $pic_name);
if (true !== ($pic_error = @image_resize($pic_name, "100x100$pic_type", 100, 100, 1))) {

目前,您的源路径指定为"original$pic_type",目标路径指定为"100x100$pic_type",您可以以某种方式更改它。

答案 1 :(得分:0)

if(isset($_POST['submitButton'])){
    //function image_resize($src, $dst, $width, $height, $crop=0)
    $tmpName = $_FILES['image1']['tmp_name'];
    image_resize($tmpName, '/where/you/want/your/file', 300, 300)
      // width and height just set a minimal width and height for the picture)
}

你想要的是$ dst,altrought,你可以使这段代码更好。

编辑:您必须在$ dst中指定文件夹和文件名,如果您不知道文件ext,可以将$ dst添加为文件夹,并在函数内添加文件名,喜欢:     $ dst =“/ myfolder / toupload /”     $ file = myfilename。“。”。$ type     $ dst。= $ file;