上传前调整图像大小

时间:2015-04-15 00:33:15

标签: php jquery image file-upload

我一直在寻找上传图片的简单脚本。我找到了interesting one,但我不知道如何修改它以在上传之前调整图片大小。

这是PHP代码:

if (isset($_POST['submit'])) {
    $validextensions = array("jpeg", "jpg", "png");
    $temporary = explode(".", $_FILES["file"]["name"]);
    $file_extension = end($temporary);
    if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] ==      "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")) && ($_FILES["file"]["size"] < 100000)//Approx. 100kb files can be   uploaded.
        && in_array($file_extension, $validextensions)) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
        } else {
            echo "<span>Your File Uploaded Succesfully...!!</span><br/>";
            echo "<br/><b>File Name:</b> " . $_FILES["file"]["name"] . "<br>";
            echo "<b>Type:</b> " . $_FILES["file"]["type"] . "<br>";
            echo "<b>Size:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "<b>Temp file:</b> " . $_FILES["file"]["tmp_name"] . "<br>";
            if (file_exists("upload/" . $_FILES["file"]["name"])) {
                echo $_FILES["file"]["name"] . " <b>already exists.</b> ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]     ["name"]);
                echo "<b>Stored in:</b> " . "upload/" . $_FILES["file"]["name"];
            }
        }
    } else {
        echo "<span>***Invalid file Size or Type***<span>";
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个非常强大的例程。

调整大小,创建,预览尺寸并创建缩略图

还选择最佳的最佳照片类型并将大和缩略图保存为base64 <img>

$thumbWidth = 60;
$thumbHeight = 60;
$previewWidth = 80;
$previewHeight = 60;
$bigWidth = 320;
$bigHeight = 240;

if( is_uploaded_file($_FILES['image1']['tmp_name']) || !($_FILES['image1']['error'] !== UPLOAD_ERR_OK)){
  $save = false;
  $jspan = '';
  $gspan = '';
  $pspan = '';
  switch(strtolower($_FILES['image1']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = @getimagesize($_FILES['image1']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['image1']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
}
 if ($save === true){
  $originalWidth  = imagesx($image);
  $originalHeight = imagesy($image);
  $scale      = min($previewWidth/$originalWidth, $previewHeight/$originalHeight);
  $newWidth  = ceil($scale*$originalWidth);
  $newHeight = ceil($scale*$originalHeight);
  $newPic = imagecreatetruecolor($newWidth, $newHeight);
  imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
  ob_start();
  imagejpeg($newPic, NULL, 70);
  $jpg = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $jLen = strlen($jpg);
  ob_start();
  imagepng($newPic, NULL, 9);
  $png = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $pLen = strlen($png);
  ob_start();
  imagegif($newPic);
  $gif = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $gLen = strlen($gif);
  if ($gLen < $jLen){
    if ($pLen < $gLen){
      $raw = $png;
      $png = null;
      $type = 'png';
      $pspan = '<span class="size">';
    }
    else{
      $raw = $gif;
      $gif = null;
      $type = 'gif';
      $gspan = '<span class="size">';
    }
  }
  elseif($pLen < $jLen){
    $raw = $png;
    $png = null;
    $type = 'png';
    $pspan = '<span class="size">';
  }
  else{
    $raw = $jpg;
    $jpg = null;
    $type = 'jpg';
    $jspan = '<span class="size">';
  }
  $base64 = base64_encode($raw);
  $thumb1 = "#thumb1{background-image:url('data:image/$type;base64,$base64')}";
  $size = 'Thumb';
  echo  "<p>Image $num $size <br>$jspan jpg: $jLen bytes</span>, <br>$gspan gif: $gLen bytes</span>, <br>$pspan png: $pLen bytes</span></p> $thumb1"; 
  $filename = 'thumb1.' . $type;
  $fp = fopen($filename  ,"w");
  fwrite($fp, $raw);
  fclose($fp);
  imagedestroy($newPic);
  $filename = "thumb1_$type.html";
  $fp = fopen($filename  ,"w");
  fwrite($fp, $thumb1);
  fclose($fp);
  $thumb1 = mysql_escape_string($thumb1);
  @mysql_unbuffered_query("UPDATE `photos` SET `thumb1`='$thumb1' WHERE `id`=$id");
  $scale      = min($bigWidth/$originalWidth, $bigHeight/$originalHeight);
  $newWidth  = ceil($scale*$originalWidth);
  $newHeight = ceil($scale*$originalHeight);
  $newPic = imagecreatetruecolor($newWidth, $newHeight);
  imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
  ob_start();
  imagejpeg($newPic, NULL, 70);
  $jpg = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $jLen = strlen($jpg);
  ob_start();
  imagepng($newPic, NULL, 9);
  $png = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $pLen = strlen($png);
  ob_start();
  imagegif($newPic);
  $gif = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $gLen = strlen($gif);
  if ($gLen < $jLen){
    if ($pLen < $gLen){
      $raw = $png;
      $png = null;
      $type = 'png';
      $pspan = '<span class="size">';
    }
    else{
      $raw = $gif;
      $gif = null;
      $type = 'gif';
      $gspan = '<span class="size">';
    }
  }
  elseif($pLen < $jLen){
    $raw = $png;
    $png = null;
    $type = 'png';
      $pspan = '<span class="size">';
  }
  else{
    $raw = $jpg;
    $jpg = null;
    $type = 'jpg';
    $jspan = '<span class="size">';
  }
  $base64 = base64_encode($raw);
  $size = 'Full Size';
  $filename = 'big1.' . $type;
  $big1 = "<img  width=\"$newWidth\" height=\"$newHeight\" src=\"data:image/$type;base64,$base64\" alt =\"profile photo one\"/>";
  echo  "<p>Image $num $size <br>$jspan jpg: $jLen bytes</span>, <br>$gspan gif: $gLen bytes</span>, <br>$pspan png: $pLen bytes</span></p> $big1"; 
  $fp = fopen($filename  ,"w");
  fwrite($fp, $raw);
  fclose($fp);
  imagedestroy($newPic);
  $filename = "big1_$type.html";
  $fp = fopen($filename  ,"w");
  fwrite($fp, $big1);
  fclose($fp);
  $big1 = mysql_escape_string($big1);
  @mysql_unbuffered_query("UPDATE `photos` SET `big1`='$big1' WHERE `id`=$id");
  $scale      = min($thumbWidth/$originalWidth, $thumbHeight/$originalHeight);
  $newWidth  = ceil($scale*$originalWidth);
  $newHeight = ceil($scale*$originalHeight);
  $newPic = imagecreatetruecolor($newWidth, $newHeight);
  imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
  ob_start();
  imagejpeg($newPic, NULL, 70);
  $jpg = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $jLen = strlen($jpg);
  ob_start();
  imagepng($newPic, NULL, 9);
  $png = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $pLen = strlen($png);
  ob_start();
  imagegif($newPic);
  $gif = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $gLen = strlen($gif);
  if ($gLen < $jLen){
    if ($pLen < $gLen){
      $raw = $png;
      $png = null;
      $type = 'png';
    }
    else{
      $raw = $gif;
      $gif = null;
      $type = 'gif';
    }
  }
  elseif($pLen < $jLen){
    $raw = $png;
    $png = null;
    $type = 'png';
  }
  else{
    $raw = $jpg;
    $jpg = null;
    $type = 'jpg';
  }
  $base64 = base64_encode($raw);
  $size = 'Small Thumb';
  $filename = 'smallThumb.' . $type;
  $smallThumb = "#t$id{margin:0;display:inline-block;width:60px;height:60px;border:0;background-image: background-image:url('data:image/gif;base64,$base64');}";
  echo '<div style="height:60px;width:60px;background-repeat: no-repeat;background-image:background-image:url(' . "'data:image/gif;base64,$base64');\" ></div>\n";
  echo  "<br/><p>Image $num $size <br/>jpg: $jLen bytes<br>gif: $gLen bytes<br/>png: $pLen bytes</p> "; 
  $fp = fopen($filename  ,"w");
  fwrite($fp, $raw);
  fclose($fp);
  imagedestroy($image);
  imagedestroy($newPic);
  $filename = "smallThumb$type.html";
  $fp = fopen($filename  ,"w");
  fwrite($fp,'<!DOCTYPE html><html lang="en"><head><style>' . $smallThumb . '</style></head><body><div id="t9542058256"></div></body></html>');
  fclose($fp);
  $smallThumb = mysql_escape_string($smallThumb);
  @mysql_unbuffered_query("UPDATE `photos` SET `SmallThumb`='$smallThumb' WHERE `id`=$id");

}