如何利用php函数做一个简单的自动裁剪图像功能
php语法如何利用函数做一个简单的自动裁剪图像功能,下面编程教程网小编给大家简单介绍一下具体实现代码!
实现代码如下:
function crop_image($source_path, $target_path, $width, $height) {
list($original_width, $original_height, $type) = getimagesize($source_path);
$image = imagecreatefromstring(file_get_contents($source_path));
$crop_width = min($original_width, $original_height * $width / $height);
$crop_height = min($original_height, $original_width * $height / $width);
$crop_x = ($original_width - $crop_width) / 2;
$crop_y = ($original_height - $crop_height) / 2;
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, $crop_x, $crop_y, $width, $height, $crop_width, $crop_height);
imagejpeg($new_image, $target_path, 90);
imagedestroy($image);
imagedestroy($new_image);
}
以上是编程学习网小编为您介绍的“如何利用php函数做一个简单的自动裁剪图像功能”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。