Detailed explanation of PHP custom image center cropping function

墨辰丷
Release: 2023-03-26 16:58:01
Original
1831 people have browsed it

This article mainly introduces the custom image centering and cropping function implemented in PHP, and analyzes PHP's related operating techniques for image acquisition, calculation, cropping, and saving in the form of examples. Friends in need can refer to it

The details are as follows:

The general idea of ​​centering the image:

1. First, scale the image so that the scaled image can exactly cover the cropping area. (imagecopyresampled — resample copy part of the image and resize)

2. Place the scaled image in the middle of the cropped area. (imagecopy — copy part of an image)

3. Crop the image and save it. (imagejpeg | imagepng | imagegif - Output image to browser or file)

Specific code:

//==================缩放裁剪函数====================
/**
 * 居中裁剪图片
 * @param string $source [原图路径]
 * @param int $width [设置宽度]
 * @param int $height [设置高度]
 * @param string $target [目标路径]
 * @return bool [裁剪结果]
 */
function image_center_crop($source, $width, $height, $target)
{
  if (!file_exists($source)) return false;
  /* 根据类型载入图像 */
  switch (exif_imagetype($source)) {
    case IMAGETYPE_JPEG:
      $image = imagecreatefromjpeg($source);
      break;
    case IMAGETYPE_PNG:
      $image = imagecreatefrompng($source);
      break;
    case IMAGETYPE_GIF:
      $image = imagecreatefromgif($source);
      break;
  }
  if (!isset($image)) return false;
  /* 获取图像尺寸信息 */
  $target_w = $width;
  $target_h = $height;
  $source_w = imagesx($image);
  $source_h = imagesy($image);
  /* 计算裁剪宽度和高度 */
  $judge = (($source_w / $source_h) > ($target_w / $target_h));
  $resize_w = $judge ? ($source_w * $target_h) / $source_h : $target_w;
  $resize_h = !$judge ? ($source_h * $target_w) / $source_w : $target_h;
  $start_x = $judge ? ($resize_w - $target_w) / 2 : 0;
  $start_y = !$judge ? ($resize_h - $target_h) / 2 : 0;
  /* 绘制居中缩放图像 */
  $resize_img = imagecreatetruecolor($resize_w, $resize_h);
  imagecopyresampled($resize_img, $image, 0, 0, 0, 0, $resize_w, $resize_h, $source_w, $source_h);
  $target_img = imagecreatetruecolor($target_w, $target_h);
  imagecopy($target_img, $resize_img, 0, 0, $start_x, $start_y, $resize_w, $resize_h);
  /* 将图片保存至文件 */
  if (!file_exists(dirname($target))) mkdir(dirname($target), 0777, true);
  switch (exif_imagetype($source)) {
    case IMAGETYPE_JPEG:
      imagejpeg($target_img, $target);
      break;
    case IMAGETYPE_PNG:
      imagepng($target_img, $target);
      break;
    case IMAGETYPE_GIF:
      imagegif($target_img, $target);
      break;
  }
//  return boolval(file_exists($target));//PHP5.5以上可用boolval()函数获取返回的布尔值
  return file_exists($target)?true:false;//兼容低版本PHP写法
}
Copy after login

//==================函数使用方式====================
// 原始图片的路径
$source = '../source/img/middle.jpg';
$width = 480; // 裁剪后的宽度
$height = 480;// 裁剪后的高度
// 裁剪后的图片存放目录
$target = '../source/temp/resize.jpg';
// 裁剪后保存到目标文件夹
if (image_center_crop($source, $width, $height, $target)) {
  echo "原图1440*900为:<img src=&#39;$source&#39;>";
  echo "<hr>";
    echo "修改后图片480*480为:<img src=&#39;$target&#39;>";
}
Copy after login

Running effect:

The original picture 1440*900 is:


The modified picture 480*480 is:

Similarly, for pictures of 480*320, 800*600 and other sizes, you only need to modify the corresponding parameters.

Attachment: Problems encountered during code testing

Error report: call an undefined function exif_imagetype()

Solution:

Open the extensionextension=php_exif.dll

And put extension=php_mbstring.dll in front of extension=php_exif.dll

Another: boolval()Function is function that can be used only after version PHP5.5 or above. The test code in this article is compatible with lower versions. Use the following statement instead:

return file_exists($target)?true:false;
Copy after login


Related recommendations:

PHPCustom functionMethods to determine whether it is Get, Post and Ajax submission

jQueryCustom functionApplication and parsing

##About phpCustom functionand internal Function explanation

The above is the detailed content of Detailed explanation of PHP custom image center cropping function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!