如何使用PHP中的imagecrop()函式將影像裁剪到給定的矩形區域?
imagecrop()是PHP中內建的一個函式,用於將影像裁剪到給定的矩形區域。它從給定的矩形區域裁剪影像並返回輸出影像。原始影像不會被修改。
語法
resource imagecrop ($image, $rect)
引數
imagecrop()接受兩個引數,$image和$rect。
$image − 這是由影像建立函式(例如imagecreatetruecolor())返回的引數。它用於建立影像的大小。
$rect − 裁剪矩形是一個數組,包含鍵X、Y、寬度和高度。
返回值
imagecrop()函式成功時返回裁剪後的影像資源,失敗時返回false。
示例
<?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()函式之前的輸入影像
.png)
使用imagecrop()函式之後的輸出影像
.png)
示例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); ?>
輸出
.jpg)
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP