Hướng dẫn how to save crop image in php? - làm cách nào để lưu hình ảnh cắt trong php?

Đã tải xuống mã cho hình ảnh cắt từ liên kết sau:

http://deepliquid.com/content/Jcrop.html

Đây là mã trong trang crop.php:

<?php

/**
 * Jcrop image cropping plugin for jQuery
 * Example cropping script
 * @copyright 2008-2009 Kelly Hallman
 * More info: http://deepliquid.com/content/Jcrop_Implementation_Theory.html
 */

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;

    $src = 'demo_files/pool.jpg';
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);

    header('Content-type: image/jpeg');
    imagejpeg($dst_r,null,$jpeg_quality);

    exit;
}

// If not a POST request, display page below:

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.Jcrop.js"></script>
        <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
        <script language="Javascript">

            $(function(){

                $('#cropbox').Jcrop({
                    aspectRatio: 1,
                    onSelect: updateCoords
                });

            });

            function updateCoords(c)
            {
                $('#x').val(c.x);
                $('#y').val(c.y);
                $('#w').val(c.w);
                $('#h').val(c.h);
            };

            function checkCoords()
            {
                if (parseInt($('#w').val())) return true;
                alert('Please select a crop region then press submit.');
                return false;
            };

        </script>

</head>

<body>
<!-- This is the image we're attaching Jcrop to -->
        <img src="demo_files/pool.jpg" id="cropbox" />

        <!-- This is the form that our event handler fills -->
        <form action="crop.php" method="post" onsubmit="return checkCoords();">
            <input type="hidden" id="x" name="x" />
            <input type="hidden" id="y" name="y" />
            <input type="hidden" id="w" name="w" />
            <input type="hidden" id="h" name="h" />
            <input type="submit" value="Crop Image" />
        </form>
</body>
</html>

Việc cắt xén được thực hiện hoàn hảo và hiển thị hình ảnh bị cắt thành công.

Nhưng tôi muốn lưu hình ảnh bị cắt vào một thư mục.

Tôi có thể làm cái này như thế nào?

Hướng dẫn how to save crop image in php? - làm cách nào để lưu hình ảnh cắt trong php?

Thử nghiệm

Bạn có thể cắt hình ảnh trước khi tải lên và lưu nó trên máy chủ của bạn bằng PHP. Cắt một hình ảnh sẽ yêu cầu một thư viện jQuery gọi là & nbsp; cropper. Bạn có thể tải xuống Thư viện Cropper từ liên kết bên dưới:cropper. You can download the cropper library from the link below:

Tải xuống Thư viện Cropper

Hãy tiếp tục và tải xuống thư viện này. Bạn cũng sẽ cần jQuery cho việc này. Tất cả các tập tin cũng đã được bao gồm trong các tệp nguồn bên dưới.

Bao gồm các tập tin thư viện

Sau khi tải xuống và đặt các tệp trong dự án của bạn, bạn sẽ cần bao gồm 3 tệp. jQuery, cropper.css và cropper.js. Bạn có thể tìm thấy các tệp đó trong các tệp nguồn bên dưới:

index.php

<script src="jquery-3.3.1.min.js"></script>
<script src="cropper.js"></script>
<link rel="stylesheet" type="text/css" href="cropper.css">

<!-- Image needs to be cropped, should be displayed in an <img> tag -->
<img src="adnan-afzal.jpg" style="width: 200px;" id="image">

<!-- Initiating the library and disabling the zoom from mouse wheel and from laptop mousepad -->
<script>
    $(function () {
        $("#image").cropper({
            zoomable: false
        });
    });
</script>

<!-- If you want to remove the background and make it transparent, you can apply the following styles -->
<style>
    .cropper-crop {
        display: none;
    }
    .cropper-bg {
        background: none;
    }
</style>

Tại thời điểm này, bạn sẽ có thể cắt hình ảnh đã chọn trong thẻ. Bước tiếp theo là lưu khu vực cắt khỏi hình ảnh.

Tiết kiệm khu vực cắt hình ảnh

Trước tiên, bạn phải tạo một nút đơn giản, khi nhấp vào sẽ gửi yêu cầu AJAX với khu vực bị cắt:

<!-- A button when clicked will save the cropped area -->
<button type="button" onclick="crop();">
    Crop
</button>

<script>
function crop() {
    // Blob is a textual form of an image which will be obtained from <img> tag
    $("#image").cropper("getCroppedCanvas").toBlob(function (blob) {

        // FormData is a built-in javascript object
        var formData = new FormData();
        formData.append("croppedImage", blob);

        $.ajax({
            url: "upload.php", // name of the file which we will be creating soon
            method: "POST",
            data: formData,
            processData: false, // necessary for sending image data
            contentType: false, // necessary for sending image data
            success: function (response) {
                alert(response);
            }, error: function (xhr, status, error) {
                console.log(status, error);
            }
        });
    });
}
</script>

Bây giờ, chúng tôi chỉ cần tạo một tệp mới có tên & nbsp; upload.php & nbsp; và dán mã sau trong đó. Mã này khá đơn giản, chúng ta có thể lưu trữ hình ảnh được gửi từ AJAX bằng cách sử dụng hàm PHP tích hợp được gọi là & nbsp; Move_uploaded_file ():move_uploaded_file():

upload.php

<?php

move_uploaded_file($_FILES["croppedImage"]["tmp_name"], "Cropped image.jpg");
echo "Image has been uploaded";

Rằng nó, tiếp tục và thử cắt một hình ảnh và nhấn nút crop crop. Khu vực cắt xén của bạn sẽ được lưu dưới dạng hình ảnh cắt xén.jpg.

bài chuyển hướng

Quan điểm đăng: 3.720 3,720

Hướng dẫn how to save crop image in php? - làm cách nào để lưu hình ảnh cắt trong php?

Lập trình máy tính. Lập trình viên full stack. Chuyên về phát triển web (PHP), phát triển bản địa Android (Java và Kotlin), phát triển bản địa iOS (Swift 4 và Swift UI), Vue JS và Node JS và Mongo DB Development. Xem tất cả các bài viết của Adnanafzal565