imagecrop() เป็นฟังก์ชัน inbuilt ใน PHP ที่ใช้ในการครอบตัดรูปภาพให้เป็นสี่เหลี่ยมที่กำหนด มันครอบตัดรูปภาพจากพื้นที่สี่เหลี่ยมที่กำหนดและส่งคืนรูปภาพที่ส่งออก รูปภาพที่กำหนดจะไม่ถูกแก้ไข
ไวยากรณ์
resource imagecrop ($image, $rect)
พารามิเตอร์
imagecrop() รับสองพารามิเตอร์ $image และ $rect .
-
$image − เป็นพารามิเตอร์ที่ส่งคืนโดยฟังก์ชันการสร้างรูปภาพ เช่น imagecreatetruecolor() . ใช้สำหรับสร้างขนาดของรูปภาพ
-
$rect − สี่เหลี่ยมครอบตัดคืออาร์เรย์ที่มีคีย์ X, Y, ความกว้าง และความสูง
คืนค่า
imagecrop() คืนค่าทรัพยากรรูปภาพที่ครอบตัดเมื่อสำเร็จหรือคืนค่าเป็นเท็จเมื่อล้มเหลว
ตัวอย่าง
<?php
// It will create an image from the given image
$img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
// This will find the size of the image
$size = min(imagesx($img), imagesy($img));
//This will set the size of the cropped image.
$img2 = imagecrop($img, ['x' => 0, 'y' => 0, 'width' => 500, 'height' => 320]);
if($img2 !== FALSE) {
imagepng($img2, 'C:\xampp\htdocs\pic_cropped.png');
imagedestroy($img2);
}
imagedestroy($img);
?> ผลลัพธ์
ป้อนรูปภาพก่อนใช้ฟังก์ชัน imagecrop()

ส่งออกรูปภาพหลังจากใช้ฟังก์ชัน imagecrop()

ตัวอย่างที่ 2
<?php
//load an image from the local drive folder.
$filename = 'C:\xampp\htdocs\Images\img34.png';
$img = imagecreatefrompng($filename );
$ini_x_size = getimagesize($filename)[0];
$ini_y_size = getimagesize($filename )[1];
//the minimum of xlength and ylength to crop.
$crop_measure = min($ini_x_size, $ini_y_size);
// Set the content-type header
//header('Content-Type: image/png');
$crop_array = array('x' =>0 , 'y' => 0, 'width' => $crop_measure, 'height'=>
$crop_measure);
$thumb_img = imagecrop($img, $crop_array);
imagejpeg($thumb_img, 'thumb.png', 100);
?> ผลลัพธ์
