Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Thay đổi kích thước hình ảnh PHP

Theo hiểu biết của tôi, bạn không thể thay đổi kích thước hình ảnh trước đó đang tải nó lên. (Tôi có thể nhầm!) Tuy nhiên, khi bạn tải lên hình ảnh, nó sẽ chuyển thành một tệp tạm thời. Bạn có thể thay đổi kích thước hình ảnh tạm thời và sao chép hình ảnh đã thay đổi kích thước đến đích cuối cùng của nó.

Mã này được điều chỉnh từ một đoạn mã tại FliquidStudios: Thay đổi kích thước hình ảnh trong PHP với GD và Imagick .

Vì (có vẻ như) bạn muốn giữ cho chiều rộng không đổi, bạn không thực sự cần thực hiện nhiều thử nghiệm tỷ lệ.

Cập nhật:

Bạn có thể chỉ cần sử dụng mã này thay cho mã gốc của mình. Hầu hết nó không thay đổi.

<?php

// resizes an image to fit a given width in pixels.
// works with BMP, PNG, JPEG, and GIF
// $file is overwritten
function fit_image_file_to_width($file, $w, $mime = 'image/jpeg') {
    list($width, $height) = getimagesize($file);
    $newwidth = $w;
    $newheight = $w * $height / $width;

    switch ($mime) {
        case 'image/jpeg':
            $src = imagecreatefromjpeg($file);
            break;
        case 'image/png';
            $src = imagecreatefrompng($file);
            break;
        case 'image/bmp';
            $src = imagecreatefromwbmp($file);
            break;
        case 'image/gif';
            $src = imagecreatefromgif($file);
            break;
    }

    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    switch ($mime) {
        case 'image/jpeg':
            imagejpeg($dst, $file);
            break;
        case 'image/png';
            imagealphablending($dst, false);
            imagesavealpha($dst, true);
            imagepng($dst, $file);
            break;
        case 'image/bmp';
            imagewbmp($dst, $file);
            break;
        case 'image/gif';
            imagegif($dst, $file);
            break;
    }

    imagedestroy($dst);
}

// init file vars
$pic  = $_FILES['photo']['name'];
$target = 'uploads/' . basename( $_FILES['photo']['name']);
$temp_name = $_FILES['photo']['tmp_name'];
$type = $_FILES["photo"]["type"];

// Connects to your Database 
mysql_connect("hostname", "username", "password") or die(mysql_error()) ; 
mysql_select_db("database") or die(mysql_error()) ; 

// get form data
$name = mysql_real_escape_string(isset($_POST['name']) ? $_POST['name'] : 'No name');

//Writes the information to the database 
mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$pic')") ; 

// resize the image in the tmp directorys
fit_image_file_to_width($temp_name, 200, $type);

//Writes the photo to the server
if(move_uploaded_file($temp_name, $target)) {

    //Tells you if its all ok 
    echo "The file ". basename( $_FILES['photo']['name'] ). " has been uploaded"; 

} else {

    //Gives and error if its not 
    echo "Sorry, there was a problem uploading your file."; 

}

?>


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Một cột cụ thể không được chèn khi lưu bản ghi

  2. Lỗi cú pháp SQL khi tạo một thủ tục được lưu trữ trong MySQL

  3. Cách chuyển đổi con trỏ SQL Server sang MySQL tương đương

  4. Mysql chọn hàng cuối cùng cho mỗi nhóm

  5. Vấn đề backtick Bash + MySQL -d