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:

    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?

    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 () http://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

    ';
    
    3, mảng
    ';
    
    4): gdimage | false
    (GdImage
    ';
    
    3
    , array
    ';
    
    4
    ): GdImage|false

    Câ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ả.

    ';
    
    5 đã cho không được sửa đổi.

    Thông số

    ';
    
    5

    Một đối tượng GDimage, được trả về bởi một trong các hàm tạo hình ảnh, chẳng hạn như ImageCreatetRueColor ().GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

    ';
    
    7

    Hình chữ nhật cắt xén dưới dạng mảng với các phím

    ';
    
    8,
    ';
    
    9,
    $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);
    
    0 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);
    
    1.array with keys
    ';
    
    8,
    ';
    
    9,
    $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);
    
    0 and
    $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);
    
    1.

    Trả về giá trị

    Trả về đối tượng hình ảnh bị cắt trên thành công hoặc

    $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);
    
    2 khi thất bại.
    $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);
    
    2
    on failure.

    Thay đổi

    Phiên bảnSự mô tả
    8.0.0 ImageCrop (gdimage
    ';
    
    3, mảng
    ';
    
    4): gdimage | falseGdImage instance now; previously, a resource was expected.
    8.0.0 Câ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ả.
    ';
    
    5 đã cho không được sửa đổi.GDImage instance now; previously, a resource was returned.

    Thông số

    ';
    
    5imagecrop() example

    Một đối tượng GDimage, được trả về bởi một trong các hàm tạo hình ảnh, chẳng hạn như ImageCreatetRueColor ().

    $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);
    
    4

    '; 7

    • Hình chữ nhật cắt xén dưới dạng mảng với các phím
      ';
      
      8,
      ';
      
      9,
      $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);
      
      0 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);
      
      1.

    64

    Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
    Learn more.

    Mã dưới đây trồng tốt hình ảnh, đó là những gì tôi muốn, nhưng đối với hình ảnh lớn hơn, nó cũng hoạt động. Có cách nào 'phóng to ra khỏi hình ảnh'

    Ý tưởng tôi sẽ có thể có mỗi hình ảnh gần như cùng kích thước trước khi cắt để tôi nhận được kết quả tốt mỗi lần

    Mã là

    ';
    

    Đã hỏi ngày 6 tháng 12 năm 2009 lúc 17:39Dec 6, 2009 at 17:39

    4

    Nếu bạn đang cố gắng tạo hình thu nhỏ, trước tiên bạn phải thay đổi kích thước hình ảnh bằng

    $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);
    
    5. Bạn phải thay đổi kích thước hình ảnh sao cho kích thước của phía nhỏ hơn của hình ảnh bằng với phía tương ứng của ngón tay cái.

    Ví dụ: nếu hình ảnh nguồn của bạn là 1280x800px và ngón tay cái của bạn là 200x150px, bạn phải thay đổi kích thước hình ảnh của mình thành 240x150px và sau đó cắt nó thành 200x150px. Điều này là để tỷ lệ khung hình của hình ảnh sẽ không thay đổi.

    Đây là một công thức chung để tạo hình thu nhỏ:

    $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);
    

    Không kiểm tra điều này nhưng nó sẽ hoạt động.

    CHỈNH SỬA

    Bây giờ đã được thử nghiệm và làm việc.

    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?

    Ndequeker

    7.7927 Huy hiệu vàng59 Huy hiệu bạc93 Huy hiệu Đồng7 gold badges59 silver badges93 bronze badges

    Đã trả lời ngày 6 tháng 12 năm 2009 lúc 17:55Dec 6, 2009 at 17:55

    Tatu Ulmanentatu UlmanenTatu Ulmanen

    121K34 Huy hiệu vàng183 Huy hiệu bạc183 Huy hiệu Đồng34 gold badges183 silver badges183 bronze badges

    10

    $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);
    
    6 sẽ lấy một khu vực hình chữ nhật từ
    $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);
    
    7 chiều rộng
    $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à chiều cao
    $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 tại vị trí
    
    
    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

    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?

    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: http://php.net/manual/en/function.imagecreatefromjpeg.php
    http://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
    = $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);
          echo "cropped"; die;
          ?>  
    

    Đã 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.

    http://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ụ:

    ';
    
    0

    Đã trả lời ngày 5 tháng 9 năm 2019 lúc 16:07Sep 5, 2019 at 16:07

    Nabi K.A.Z.Nabi K.A.Z.Nabi K.A.Z.

    8.8466 huy hiệu vàng55 Huy hiệu bạc71 Huy hiệu đồng6 gold badges55 silver badges71 bronze badges

    ';
    
    1

    Phải được thay thế bằng:

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

    Sau đó, nó sẽ hoạt động!

    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?

    Tim Cooper

    154K37 Huy hiệu vàng322 Huy hiệu bạc275 Huy hiệu Đồng37 gold badges322 silver badges275 bronze badges

    Đã trả lời ngày 19 tháng 9 năm 2011 lúc 9:34Sep 19, 2011 at 9:34

    Parisparisparis

    Huy hiệu 151 Đồng1 bronze badge

    Làm thế nào để cắt một hình ảnh trong PHP?

    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ả.imagecrop() 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.

    Làm cách nào để cắt một hình ảnh hình chữ nhật?

    Nếu bạn muốn thay đổi phác thảo của một bức ảnh để biến nó thành một hình dạng (như hình tròn hoặc hình chữ nhật tròn), hãy sử dụng công cụ cắt trên ruy băng với tùy chọn cây trồng để hình dạng.... Cắt một hình ảnh thành một hình dạng, chẳng hạn như một vòng tròn ..

    Làm cách nào để cắt hình ảnh trong Photoshop?

    Cắt và duỗi thẳng một hình ảnh..
    Chọn Công cụ Cây trồng trong bảng công cụ.....
    Kéo bất kỳ cạnh hoặc góc nào để điều chỉnh kích thước và hình dạng của đường viền cây trồng ..
    Kéo bên trong đường viền cây trồng để định vị hình ảnh bên trong đường viền cây trồng ..
    Kéo ra ngoài một góc của đường viền cây trồng để xoay hoặc duỗi thẳng ..