dsjgk330337 2014-08-23 15:37
浏览 19

如何重新调整高质量图像使用此代码?

How to re-size image with high quality When using this code ?

.....................................................................................................................................................................

<?PHP
if(isset($_POST["Submit"]))
    {
        $image_type_check = $_FILES['file']['type']; //file type
        $image_name = $_FILES['file']['name']; //file name
        $image_size = $_FILES['file']['size']; //file size
        $image_temp = $_FILES['file']['tmp_name']; //file temp
        $image_size_info = getimagesize($image_temp); //get image size

        //Get file extension and name to construct new file name 
        $image_info = pathinfo($image_name);
        $image_extension = strtolower($image_info["extension"]); //image extension
        $image_name_only = strtolower($image_info["filename"]);//file name only, no extension

        //create a random name for new image (Eg: fileName_293749.jpg);
        $new_file_name = '123456789'.$image_extension;

        list($width,$height)=getimagesize($image_temp);

        $newwidth=985;
        $newheight=($height/$width)*$newwidth;
        $tmp=imagecreatetruecolor($newwidth,$newheight);

        $filename = "uploads/".$new_file_name;

        imagejpeg($tmp,$filename,100);
    }
?>
  • 写回答

1条回答 默认 最新

  • douqiao6015 2014-08-23 17:05
    关注
    • you should use move_uploaded_file()
      to make sure that the paths are OK and the file is valid .

    • To change the size you need imagecopyresampled() .

    • your file name is invalid with

    $image_info = pathinfo($image_name);
    $image_extension = strtolower($image_info["extension"]);
    $new_file_name = '123456789'.$image_extension;

    you get

    $new_file_name == '123456789jpg'
    without .


    [...]
    $image_extension = "." . strtolower($image_info["extension"]);
    
    $target = "uploads/123456789".$image_extension;
    
    if(move_uploaded_file($image_temp,$target)) {
      if ($image_extension == '.jpg') {
      // Get new dimensions
      list($width_orig, $height_orig) = getimagesize($image_temp);
      $ratio_orig = $width_orig/$height_orig;
    
      $width = 985;
    
      if ($width_orig > $height_orig) {
        $height = $width / $ratio_orig;
      } else {
        $height = $width * $ratio_orig;
      }   
    
      // Resample
    
      $image_p = imagecreatetruecolor($width, $height);
      $image = imagecreatefromjpeg($target);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    
      imagejpeg($image_p,$target,100);
      imagedestroy($image_p);
      } //is jpg file
    } else { echo "File could not be moved to" .$target; }
    
    评论

报告相同问题?