2017-02-13 20 views
-1

イメージは次のコードでサイズ変更されます。PHPイメージのサイズ変更 - 高さと幅の自動設定

すべてのアップロードで、高さを300px、幅をautoに設定したいとします。

現在、テスト目的で100 x 100に設定されています。これはアップロード時には何もしません。

// Escape user inputs for security 
 

 
$id = mysqli_real_escape_string($link, $_POST['id']); 
 

 
$target_dir = "images/breweryimages/"; 
 
$size = getimagesize($_FILES["fileToUpload"]["name"]); 
 
$base_file = rand(100000000000,10000000000000) . "-" . basename($_FILES["fileToUpload"]["name"]); 
 
$target_file = $target_dir . $base_file; 
 
$image = getimagesize($_FILES['fileToUpload']); 
 
    $file_width=$image[0]; 
 
    $file_height=$image[1]; 
 
$file_width = 100; 
 
$file_height = 100; 
 
$uploadOk = 1; 
 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
 
// Check if image file is a actual image or fake image 
 
if(isset($_POST["submit"])) { 
 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
 
    if($check !== false) { 
 
     echo "File is an image - " . $check["mime"] . "."; 
 
     $uploadOk = 1; 
 
    } else { 
 
     echo "File is not an image."; 
 
     $uploadOk = 0; 
 
    } 
 
} 
 
// Check if file already exists 
 
if (file_exists($target_file)) { 
 
    echo "Sorry, file already exists."; 
 
    $uploadOk = 0; 
 
} 
 
// Check file size 
 
if ($_FILES["fileToUpload"]["size"] > 50000000000) { 
 
    echo "Sorry, your file is too large."; 
 
    $uploadOk = 0; 
 
} 
 

 

 
// Check if $uploadOk is set to 0 by an error 
 
if ($uploadOk == 0) { 
 
    echo "Sorry, your file was not uploaded."; 
 
// if everything is ok, try to upload file 
 
} else { 
 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]) . " " . $file_width . "has been uploaded."; 
 
    } else { 
 
     echo "Sorry, there was an error uploading your file."; 
 
    } 
 
}

+0

あなたのニーズに合うのだろうか?あなたはあなたのアップロードメッセージ以外ではそれらを使用していません。 – jeroen

答えて

1

うまくいけば、これは、なぜ、これらの値は、ファイルには何をするだろう

static public function square_crop($src_image, $dest_image, $dst_w = 100, $dst_h = 100, $jpg_quality = 90) { 

    // Get dimensions of existing image 
    $image = getimagesize($src_image); 

    // Check for valid dimensions 
    if($image[0] <= 0 || $image[1] <= 0) return false; 

    // Determine format from MIME-Type 
    $image['format'] = strtolower(preg_replace('/^.*?\//', '', $image['mime'])); 

    // Import image 
    switch($image['format']) { 
     case 'jpg': 
     case 'jpeg': 
      $image_data = imagecreatefromjpeg($src_image); 
     break; 
     case 'png': 
      $image_data = imagecreatefrompng($src_image); 
     break; 
     case 'gif': 
      $image_data = imagecreatefromgif($src_image); 
     break; 
     default: 
      // Unsupported format 
      return false; 
     break; 
    } 

    // Verify import 
    if($image_data == false) return false; 

    // Calculate measurements 
    if($image[0] & $image[1]) { 
     // For landscape images 
     $x_offset = ($image[0] - $image[1])/2; 
     $y_offset = 0; 
     $square_size = $image[0] - ($x_offset * 2); 
    } else { 
     // For portrait and square images 
     $x_offset = 0; 
     $y_offset = ($image[1] - $image[0])/2; 
     $square_size = $image[1] - ($y_offset * 2); 
    } 

    // Resize and crop 
    $canvas = imagecreatetruecolor($dst_w, $dst_h); 
    if(imagecopyresampled(
     $canvas, 
     $image_data, 
     0, 
     0, 
     $x_offset, 
     $y_offset, 
     $dst_w, 
     $dst_h, 
     $square_size, 
     $square_size 
    )) { 

     // Create thumbnail 
     switch(strtolower(preg_replace('/^.*\./', '', $dest_image))) { 
      case 'jpg': 
      case 'jpeg': 
       return imagejpeg($canvas, $dest_image, $jpg_quality); 
      break; 
      case 'png': 
       return imagepng($canvas, $dest_image); 
      break; 
      case 'gif': 
       return imagegif($canvas, $dest_image); 
      break; 
      default: 
       // Unsupported format 
       return false; 
      break; 
     } 

    } else { 
     return false; 
    } 

}