Chúng tôi có thể tạo hình ảnh bằng PHP không?

Ghi chú. Bạn sẽ cần tạo một thư mục mới có tên "tải lên" trong thư mục chứa "tải lên. php" cư trú. Các tệp đã tải lên sẽ được lưu ở đó


Kiểm tra xem tệp đã tồn tại chưa

Bây giờ chúng ta có thể thêm một số hạn chế

Đầu tiên, chúng tôi sẽ kiểm tra xem tệp đã tồn tại trong thư mục "tải lên" chưa. Nếu có, thông báo lỗi sẽ hiển thị và $uploadOk được đặt thành 0

// Kiểm tra xem file đã tồn tại chưa
if (file_exists($target_file)) {
echo "Xin lỗi, tập tin đã tồn tại. “;
$uploadOk = 0;
}


Kích thước tệp giới hạn

Trường nhập tệp trong biểu mẫu HTML của chúng tôi ở trên được đặt tên là "fileToUpload"

Bây giờ, chúng tôi muốn kiểm tra kích thước của tệp. Nếu tệp lớn hơn 500KB, thông báo lỗi sẽ hiển thị và $uploadOk được đặt thành 0

// Kiểm tra dung lượng file
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Xin lỗi, tệp của bạn quá lớn. “;
$uploadOk = 0;
}


Loại tệp giới hạn

Đoạn mã dưới đây chỉ cho phép người dùng tải lên các tệp JPG, JPEG, PNG và GIF. Tất cả các loại tệp khác đưa ra thông báo lỗi trước khi đặt $uploadOk thành 0

Trong hướng dẫn nhanh này, chúng tôi thiết lập một biểu mẫu HTML cơ bản để tải lên hình ảnh bằng PHP, chúng tôi cũng khám phá cách bảo mật tập lệnh PHP của chúng tôi để người dùng độc hại không thể lạm dụng nó

Thiết lập biểu mẫu tải lên hình ảnh

Chúng tôi bắt đầu với một hình thức cơ bản

Biểu mẫu chứa nút gửi và có thuộc tính action. Thuộc tính hành động trỏ đến trang mà biểu mẫu sẽ đăng tất cả nội dung của nó lên khi nhấp vào nút gửi

<form action="upload.php" method="POST">
    <button type="submit">Uploadbutton>
form>

Biểu mẫu đăng lên một tệp PHP có tên là upload.php. Tập tin này sẽ xử lý việc tải lên hình ảnh. Chúng tôi sẽ nhận được điều đó trong một phút

Để cho phép người dùng chọn tệp, chúng tôi thêm trường nhập tệp

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>

Đồng thời, chúng tôi cũng đã thêm thuộc tính biểu mẫu và đặt nó thành

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
1. Điều này là cần thiết nếu chúng tôi có trường nhập tệp trong biểu mẫu của mình

Bởi vì chúng tôi chỉ tải lên hình ảnh, chúng tôi thêm thuộc tính

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
2 vào phần tử đầu vào tệp và đặt nó thành
<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
3 để yêu cầu nó chỉ chấp nhận các tệp có kiểu bắt chước bắt đầu bằng
<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
4, chẳng hạn như
<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
5 hoặc
<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
6

Hãy viết trình xử lý tải lên hình ảnh PHP tiếp theo

Xử lý tải lên hình ảnh PHP

Chúng tôi sẽ tạo một tệp mới có tên là upload.php trong cùng thư mục với trang chứa biểu mẫu của chúng tôi

Tiếp theo, chúng tôi tạo một thư mục có tên là

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
8, cũng trong cùng một thư mục. Đây là nơi tập lệnh của chúng tôi sẽ lưu trữ các tệp hình ảnh đã tải lên bằng cách sử dụng chức năng
<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*" />
    <button type="submit">Uploadbutton>
form>
9



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);

Đã được thực hiện. Đây là tất cả những gì cần thiết để làm cho nó hoạt động

Thật không may, không phải tất cả mọi người trên internet là một vị thánh. Tập lệnh của chúng tôi hiện cho phép mọi người tải lên bất kỳ thứ gì, đây là rủi ro bảo mật. Chúng tôi cần đảm bảo mọi người tải lên hình ảnh hợp lệ và không cố làm hại máy chủ của chúng tôi

Bảo mật quá trình tải lên hình ảnh

Chúng tôi sẽ đảm bảo thêm ba điều này theo thứ tự

  • Các tập tin cần phải là một hình ảnh
  • Tệp phải nhỏ hơn 5MB
  • Tệp phải có tên hợp lệ

Đảm bảo tệp là một hình ảnh

Hãy bắt đầu bằng cách đảm bảo rằng tệp đã tải lên thực sự là một hình ảnh

Sử dụng



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
0 chúng ta có thể nhận được kiểu mô phỏng hình ảnh, hàm trả về


// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
1 khi tệp không phải là hình ảnh



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_file["name"]
);

Tiếp theo, chúng tôi cần ngăn người dùng tải lên các tệp rất lớn

Giới hạn kích thước tệp

Trong ví dụ bên dưới, chúng tôi giới hạn kích thước tệp hình ảnh ở mức 5MB, tất nhiên bạn có thể chọn giới hạn của riêng mình, nhưng ít nhất nên giới hạn nó ở một điểm nhất định để ngăn người dùng tải lên hàng gigabyte dữ liệu trong nỗ lực tấn công DoS

Trong thư mục của tập lệnh upload.php của chúng tôi, chúng tôi tạo một tệp



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
3 và đặt nội dung của nó như sau

LimitRequestBody 5242880

Ngăn yêu cầu vượt quá 5MB, lưu ý rằng yêu cầu này bao gồm các trường khác trong biểu mẫu

Bây giờ, hãy đảm bảo rằng người dùng không thể tải lên tệp 0 byte. Chúng tôi sử dụng



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
5 để đọc kích thước tệp thực tế thay vì dựa vào


// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
6 được báo cáo bởi


// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
7 vì người dùng có thể sửa đổi



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if image file is zero bytes
if (filesize($image_file["tmp_name"]) <= 0) {
    die('Uploaded file has no contents.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_file["name"]
);

Bây giờ hãy đảm bảo tên tệp của hình ảnh hợp lệ

Bảo vệ chống lại tên tệp độc hại

Các tác nhân độc hại có thể thử tải lên một hình ảnh có tên



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
8, tập lệnh PHP của chúng tôi hiện sẽ lưu trữ “hình ảnh” này trong


// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
9 có thể ghi đè lên tệp


// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_file["name"]
);
0 của chính chúng tôi

Chúng tôi có thể cố gắng làm sạch tên tệp hình ảnh, nhưng cách tiếp cận an toàn hơn là tạo tên tệp của riêng chúng tôi

Chúng tôi sẽ tạo một số



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_file["name"]
);
1 và sử dụng chúng làm tên cho hình ảnh của chúng tôi



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if image file is zero bytes
if (filesize($image_file["tmp_name"]) <= 0) {
    die('Uploaded file has no contents.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Get file extension based on file type, to prepend a dot we pass true as the second parameter
$image_extension = image_type_to_extension($image_type, true);

// Create a unique image name
$image_name = bin2hex(random_bytes(16)) . $image_extension;

// Move the temp image file to the images directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_name
);

Bước cuối cùng của chúng tôi là ngăn không cho mã được thực thi trong thư mục



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_file["name"]
);
2 của chúng tôi

Ngăn chặn thực thi mã trong thư mục hình ảnh

Trong thư mục



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_file["name"]
);
2 của chúng tôi, chúng tôi tạo một tệp


// Get reference to uploaded image
$image_file = $_FILES["image"];

// Image not defined, let's exit
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location, __DIR__ is the location of the current PHP file
    __DIR__ . "/images/" . $image_file["name"]
);
3 và đặt nội dung của nó thành như sau để ngăn mọi tệp PHP đã tải lên chạy

Điều này đảm bảo rằng mã PHP trong thư mục này sẽ không thực thi. Vì vậy, nếu ai đó vẫn quản lý để tải lên tệp PHP, chẳng hạn như tệp PHP được ngụy trang dưới dạng hình ảnh, tệp đó vẫn không chạy

php_flag engine off

Quy trình tải tệp lên của chúng tôi hiện đã được bảo mật. 🎉

Phần kết luận

Chúng tôi đã viết một biểu mẫu cơ bản với phần tử nhập tệp, để xử lý việc tải lên, chúng tôi đã tạo một tệp PHP để di chuyển hình ảnh đã tải lên vào một thư mục trên máy chủ. Để đảm bảo quá trình tải lên, chúng tôi đảm bảo rằng tên tệp hợp lệ, kích thước tệp không quá cao và tệp được tải lên thực sự là một hình ảnh

Nếu bạn cũng đang tìm cách thêm chức năng chỉnh sửa hình ảnh vào trường tải tệp lên, bạn có thể sử dụng phần tử



// Get reference to uploaded image
$image_file = $_FILES["image"];

// Exit if no file uploaded
if (!isset($image_file)) {
    die('No file uploaded.');
}

// Exit if is not a valid image file
$image_type = exif_imagetype($image_file["tmp_name"]);
if (!$image_type) {
    die('Uploaded file is not an image.');
}

// Move the temp image file to the images/ directory
move_uploaded_file(
    // Temp image location
    $image_file["tmp_name"],

    // New image location
    __DIR__ . "/images/" . $image_file["name"]
);
5. Thành phần web do Pintura hỗ trợ này sẽ tự động mở trình chỉnh sửa hình ảnh mạnh mẽ khi hình ảnh được thêm vào trường và cho phép người dùng của bạn chỉnh sửa hình ảnh trước khi tải lên

Chúng ta có thể vẽ hình ảnh bằng PHP không?

21 tháng 2 năm 2022. PHP có thể được sử dụng để tạo và thao tác các phần tử đồ họa một cách độc lập . Do đó, phạm vi và khả năng xử lý đồ họa của PHP là rất mạnh mẽ. Nó thực hiện điều này với sự trợ giúp của thư viện GD, hiện hỗ trợ các định dạng hình ảnh ở định dạng jpeg , png , gif và wbmp.

Làm cách nào để tạo một hình ảnh trong PHP?

Hàm imagecreate() là một hàm có sẵn trong PHP dùng để tạo một hình ảnh mới. Hàm này trả về hình ảnh trống có kích thước đã cho. Nói chung, hàm imagecreatetruecolor() được sử dụng thay cho hàm imagecreate() vì hàm imagecreatetruecolor() tạo ra hình ảnh chất lượng cao.

Làm cách nào để hiển thị hình ảnh bằng PHP?

do đó, chọn tên hình ảnh cụ thể và sao chép tên hình ảnh . ví dụ: bạn có thể thấy phải đề cập đến một ví dụ về nguồn hình ảnh src='tên hình ảnh. png' đây là một đường dẫn hình ảnh trực tiếp. Vì vậy, nếu hình ảnh trong thư mục hoặc thư mục và thư mục con thì hãy đặt mã này src='folderName/SubfolderName/image-name.

Làm cách nào để nhúng hình ảnh vào PHP?

Để nhúng hình ảnh do PHP tạo vào trang HTML, giả sử rằng tập lệnh PHP tạo hình ảnh thực sự là hình ảnh . Vì vậy, nếu chúng ta có image1. php và hình ảnh2. php tạo hình ảnh, chúng ta có thể sửa đổi HTML trước đó.

Bước đầu tiên để tạo một hình ảnh trong PHP là gì?

Tạo hình ảnh . Hàm này trả về mã định danh tài nguyên cho hình ảnh mà chúng tôi lưu trong $my_img. Số nhận dạng là cần thiết cho tất cả các hoạt động của chúng tôi trên hình ảnh. call the imagecreate() function with the dimensions of the image, namely its width and height in that order. This function returns a resource identifier for the image which we save in $my_img . The identifier is needed for all our operations on the image.