如何利用php函数做一个简单的自动裁剪图像功能

2024年09月03日 建站教程

php语法如何利用函数做一个简单的自动裁剪图像功能,下面web建站小编给大家简单介绍一下具体实现代码!

实现代码如下:

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

本文链接:http://so.lmcjl.com/news/12125/

展开阅读全文
相关内容