图像居中裁减的大致思路:

1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域.(imagecopyresampled ― 重采样拷贝部分图像并调整大小)

2.将缩放后的图像放置在裁减区域中间.(imagecopy ― 拷贝图像的一部分)

3.裁减图像并保存.(imagejpeg | imagepng | imagegif ― 输出图象到浏览器或文件)

具体代码:

<?php

//==================缩放裁剪函数====================
/**
* 居中裁剪图片
* @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写法
} //==================函数使用方式====================
// 原始图片的路径
$source = 'photoClip.jpg';
$width = 480; // 裁剪后的宽度
$height = 480;// 裁剪后的高度
// 裁剪后的图片存放目录
$target = 'dd111.jpg';
// 裁剪后保存到目标文件夹
if (image_center_crop($source, $width, $height, $target)) {
echo "原图1440*900为:<img src='$source'>";
echo "<hr>";
echo "修改后图片480*480为:<img src='$target'>";
}

附:代码测试中遇到的问题

报错:call an undefined function exif_imagetype()

解决方法:

打开扩展 extension=php_exif.dll

并将extension=php_mbstring.dll ,放到extension=php_exif.dll前边

另:boolval()函数为PHP5.5版本以上才能使用的函数,本文测试代码中为兼容低版本,使用如下语句代替

return file_exists($target)?true:false;

最新文章

  1. 浅谈浏览器http的缓存机制
  2. JSTREE 实现AJAX重载入时刷新所有节点树
  3. oracle去重等基础问题
  4. Altium Designer 多个输出相连等问题报错解决方法
  5. 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。
  6. My集合框架第五弹 最小堆
  7. Linux 基本权限(一)
  8. linux_iptables 详解
  9. struct--file_operations
  10. Java编程思想-泛型-泛型方法
  11. ISNULL-sqlserver语句
  12. js 数组,字符串,JSON,bind, Name
  13. USACO Section 5.3 Big Barn(dp)
  14. Bmob Androidstudio配置
  15. Ubuntu下搜狗拼音突然无法输入中文的解决办法
  16. Nginx 在 Linux 上的安装和配置
  17. The JSP specification requires that an attribute name is
  18. FICO模块
  19. 常见的移动端Web页面问题
  20. C和C指针小记(十)-函数

热门文章

  1. [LeetCode] 35. Search Insert Position_Easy tag: Binary Search
  2. [Python] Frequently used method or solutions for issues
  3. Win10 JDK 配置
  4. 机器人meta标签和X-Robots-Tag HTTP标头规格
  5. ICSharpCode.TextEditor使用及扩展
  6. 查看Andorid应用是32位还是64位
  7. vue中computed和watch的写法,以及区别
  8. php 下载完成后删除文件
  9. netcore swagger xml发布丢失问题
  10. windows下golang环境搭建