Hướng dẫn how to crop an image using php? - làm thế nào để cắt một hình ảnh bằng php?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcimagecrop[] function is an inbuilt function in PHP which is used to crop an image to the given rectangle. This function crops an image to the given rectangular area and returns the resulting image. The given image is not modified.

    Syntax:

    resource imagecrop [ $image, $rect ]

    Bàn luận This function accepts two parameters as mentioned above and described below:

    • Hàm ImageCrop [] là một hàm sẵn có trong PHP được sử dụng để cắt hình ảnh thành hình chữ nhật đã cho. Hàm này trồng một hình ảnh đến khu vực hình chữ nhật đã cho và trả về hình ảnh kết quả. Hình ảnh đã cho không được sửa đổi. It is returned by one of the image creation functions, such as imagecreatetruecolor[]. It is used to create size of image.
    • Tham số: Hàm này chấp nhận hai tham số như đã đề cập ở trên và được mô tả dưới đây: The cropping rectangle as array with keys x, y, width and height.

    $ Image: Nó được trả lại bởi một trong các chức năng tạo hình ảnh, chẳng hạn như ImageCreatetRueColor []. Nó được sử dụng để tạo kích thước của hình ảnh. This function return cropped image resource on success or False on failure.

    $ Rect: Hình chữ nhật cắt làm mảng với các phím x, y, chiều rộng và chiều cao.imagecrop[] function in PHP:

    Program:

    output:

    Giá trị trả lại: Hàm này trả về tài nguyên hình ảnh đã bị cắt thành công hoặc sai khi thất bại.

    • Dưới đây các chương trình minh họa hàm ImageCrop [] trong PHP:
    • Những bài viết liên quan:

    PHP | hàm gd_info [] //php.net/manual/en/function.imagecrop.php

    [Php 5> = 5.5.0, Php 7, Php 8]

    ImageCrop - Cắt hình ảnh đến hình chữ nhật đã choCrop an image to the given rectangle

    Sự mô tả

    ImageCrop [gdimage

    
    
    0 và đặt nó vào một khu vực hình chữ nhật là
    
    
    1 chiều rộng
    
    
    2 và chiều cao
    
    
    3 ở vị trí
    
    
    4.

    Nếu tọa độ nguồn và đích và chiều rộng và chiều cao khác nhau, việc kéo dài hoặc co lại của đoạn hình ảnh thích hợp sẽ được thực hiện. Các tọa độ đề cập đến góc trên bên trái.

    Hàm này có thể được sử dụng để sao chép các vùng trong cùng một hình ảnh. Nhưng nếu các vùng trùng nhau, kết quả sẽ không thể đoán trước được.

    - Chỉnh sửa -

    Nếu

    $image = imagecreatefromjpeg[$_GET['src']];
    $filename = 'images/cropped_whatever.jpg';
    
    $thumb_width = 200;
    $thumb_height = 150;
    
    $width = imagesx[$image];
    $height = imagesy[$image];
    
    $original_aspect = $width / $height;
    $thumb_aspect = $thumb_width / $thumb_height;
    
    if [ $original_aspect >= $thumb_aspect ]
    {
       // If image is wider than thumbnail [in aspect ratio sense]
       $new_height = $thumb_height;
       $new_width = $width / [$height / $thumb_height];
    }
    else
    {
       // If the thumbnail is wider than the image
       $new_width = $thumb_width;
       $new_height = $height / [$width / $thumb_width];
    }
    
    $thumb = imagecreatetruecolor[ $thumb_width, $thumb_height ];
    
    // Resize and crop
    imagecopyresampled[$thumb,
                       $image,
                       0 - [$new_width - $thumb_width] / 2, // Center the image horizontally
                       0 - [$new_height - $thumb_height] / 2, // Center the image vertically
                       0, 0,
                       $new_width, $new_height,
                       $width, $height];
    imagejpeg[$thumb, $filename, 80];
    
    8 và
    $image = imagecreatefromjpeg[$_GET['src']];
    $filename = 'images/cropped_whatever.jpg';
    
    $thumb_width = 200;
    $thumb_height = 150;
    
    $width = imagesx[$image];
    $height = imagesy[$image];
    
    $original_aspect = $width / $height;
    $thumb_aspect = $thumb_width / $thumb_height;
    
    if [ $original_aspect >= $thumb_aspect ]
    {
       // If image is wider than thumbnail [in aspect ratio sense]
       $new_height = $thumb_height;
       $new_width = $width / [$height / $thumb_height];
    }
    else
    {
       // If the thumbnail is wider than the image
       $new_width = $thumb_width;
       $new_height = $height / [$width / $thumb_width];
    }
    
    $thumb = imagecreatetruecolor[ $thumb_width, $thumb_height ];
    
    // Resize and crop
    imagecopyresampled[$thumb,
                       $image,
                       0 - [$new_width - $thumb_width] / 2, // Center the image horizontally
                       0 - [$new_height - $thumb_height] / 2, // Center the image vertically
                       0, 0,
                       $new_width, $new_height,
                       $width, $height];
    imagejpeg[$thumb, $filename, 80];
    
    9 nhỏ hơn
    
    
    2 và
    
    
    3 tương ứng, hình ảnh ngón tay cái sẽ được phóng to. Nếu không, nó sẽ được phóng to.

    
    

    Đã trả lời ngày 13 tháng 8 năm 2012 lúc 10:38Aug 13, 2012 at 10:38

    ErandaerandaEranda

    8181 Huy hiệu vàng10 Huy hiệu bạc25 Huy hiệu đồng1 gold badge10 silver badges25 bronze badges

    1

    Đã trả lời ngày 26 tháng 3 năm 2016 lúc 14:22Mar 26, 2016 at 14:22

    giorgio79giorgio79giorgio79

    3,4319 huy hiệu vàng49 Huy hiệu bạc79 Huy hiệu đồng9 gold badges49 silver badges79 bronze badges

    $image = imagecreatefromjpeg[$_GET['src']];
    

    Cần được thay thế bằng điều này:

    $image = imagecreatefromjpeg['images/thumbnails/myimage.jpg'];
    

    Bởi vì

    
    
    9 đang mong đợi một chuỗi. Điều này làm việc cho tôi.
    This worked for me.

    Tham khảo: //php.net/manual/en/function.imagecreatefromjpeg.php
    //php.net/manual/en/function.imagecreatefromjpeg.php

    Nippey

    4.62835 huy hiệu bạc44 Huy hiệu đồng35 silver badges44 bronze badges

    Đã trả lời ngày 24 tháng 4 năm 2013 lúc 19:57Apr 24, 2013 at 19:57

    Harry Fairbanksharry FairbanksHarry Fairbanks

    Huy hiệu vàng 4081 Huy hiệu bạc13 Huy hiệu đồng1 gold badge4 silver badges13 bronze badges

    Mã HTML:-

    enter code here
      
      
      
    
      
      Select image to upload:
      
      
     
    
     
     
    

    upload.php

    enter code here
      
    

    Đã trả lời ngày 21 tháng 12 năm 2016 lúc 16:13Dec 21, 2016 at 16:13

    Cải thiện chức năng hình ảnh cây trồng trong PHP khi đang bay.

    //www.example.com/cropimage.php?filename=a.jpg&newxsize=100&newysize=200&constrain=1
    

    Mã trong

    $image = imagecreatefromjpeg[$_GET['src']];
    
    0

    $basefilename = @basename[urldecode[$_REQUEST['filename']]];
    
    $path = 'images/';
    $outPath = 'crop_images/';
    $saveOutput = false; // true/false ["true" if you want to save images in out put folder]
    $defaultImage = 'no_img.png'; // change it with your default image
    
    $basefilename = $basefilename;
    $w = $_REQUEST['newxsize'];
    $h = $_REQUEST['newysize'];
    
    if [$basefilename == ""] {
        $img = $path . $defaultImage;
        $percent = 100;
    } else {
        $img = $path . $basefilename;
    
        $len = strlen[$img];
        $ext = substr[$img, $len - 3, $len];
        $img2 = substr[$img, 0, $len - 3] . strtoupper[$ext];
        if [!file_exists[$img]] $img = $img2;
        if [file_exists[$img]] {
            $percent = @$_GET['percent'];
            $constrain = @$_GET['constrain'];
            $w = $w;
            $h = $h;
        } else if [file_exists[$path . $basefilename]] {
            $img = $path . $basefilename;
            $percent = $_GET['percent'];
            $constrain = $_GET['constrain'];
            $w = $w;
            $h = $h;
        } else {
    
            $img = $path . 'no_img.png';    // change with your default image
            $percent = @$_GET['percent'];
            $constrain = @$_GET['constrain'];
            $w = $w;
            $h = $h;
    
        }
    
    }
    
    // get image size of img
    $x = @getimagesize[$img];
    
    // image width
    $sw = $x[0];
    // image height
    $sh = $x[1];
    
    if [$percent > 0] {
        // calculate resized height and width if percent is defined
        $percent = $percent * 0.01;
        $w = $sw * $percent;
        $h = $sh * $percent;
    } else {
        if [isset [$w] AND !isset [$h]] {
            // autocompute height if only width is set
            $h = [100 / [$sw / $w]] * .01;
            $h = @round[$sh * $h];
        } elseif [isset [$h] AND !isset [$w]] {
            // autocompute width if only height is set
            $w = [100 / [$sh / $h]] * .01;
            $w = @round[$sw * $w];
        } elseif [isset [$h] AND isset [$w] AND isset [$constrain]] {
            // get the smaller resulting image dimension if both height
            // and width are set and $constrain is also set
            $hx = [100 / [$sw / $w]] * .01;
            $hx = @round[$sh * $hx];
    
            $wx = [100 / [$sh / $h]] * .01;
            $wx = @round[$sw * $wx];
    
            if [$hx < $h] {
                $h = [100 / [$sw / $w]] * .01;
                $h = @round[$sh * $h];
            } else {
                $w = [100 / [$sh / $h]] * .01;
                $w = @round[$sw * $w];
            }
        }
    }
    
    $im = @ImageCreateFromJPEG[$img] or // Read JPEG Image
    $im = @ImageCreateFromPNG[$img] or // or PNG Image
    $im = @ImageCreateFromGIF[$img] or // or GIF Image
    $im = false; // If image is not JPEG, PNG, or GIF
    
    if [!$im] {
        // We get errors from PHP's ImageCreate functions...
        // So let's echo back the contents of the actual image.
        readfile[$img];
    } else {
        // Create the resized image destination
        $thumb = @ImageCreateTrueColor[$w, $h];
        // Copy from image source, resize it, and paste to image destination
        @ImageCopyResampled[$thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh];
    
        //Other format imagepng[]
    
        if [$saveOutput] { //Save image
            $save = $outPath . $basefilename;
            @ImageJPEG[$thumb, $save];
        } else { // Output resized image
            header["Content-type: image/jpeg"];
            @ImageJPEG[$thumb];
        }
    }
    

    Đã trả lời ngày 14 tháng 2 năm 2019 lúc 12:24Feb 14, 2019 at 12:24

    Nadeem0035Nadeem0035Nadeem0035

    3,4471 Huy hiệu vàng22 Huy hiệu bạc35 Huy hiệu Đồng1 gold badge22 silver badges35 bronze badges

    Bạn có thể sử dụng hàm

    $image = imagecreatefromjpeg[$_GET['src']];
    
    1 trong [Php 5> = 5.5.0, Php 7]

    Thí dụ:

    Bài Viết Liên Quan

    Chủ Đề