本文实例讲述了php实现的自定义图像居中裁剪函数。分享给大家供大家参考,具体如下:
图像居中裁减的大致思路:
1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域。(imagecopyresampled — 重采样拷贝部分图像并调整大小)
2.将缩放后的图像放置在裁减区域中间。(imagecopy — 拷贝图像的一部分)
3.裁减图像并保存。(imagejpeg | imagepng | imagegif — 输出图象到浏览器或文件)
具体代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
//==================缩放裁剪函数====================
/**
* 居中裁剪图片
* @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写法
}
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//==================函数使用方式====================
// 原始图片的路径
$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='$source'>";
echo "<hr>";
echo "修改后图片480*480为:<img src='$target'>";
}
|
运行效果:
同理,480*320,、800*600等尺寸的图片只需修改相应参数即可。
附:代码测试中遇到的问题
报错:call an undefined function exif_imagetype()
解决方法:
打开扩展 extension=php_exif.dll
并将extension=php_mbstring.dll ,放到extension=php_exif.dll前边
另:boolval()函数为php5.5版本以上才能使用的函数,本文测试代码中为兼容低版本,使用如下语句代替:
?
|
1
|
return file_exists($target)?true:false;
|
希望本文所述对大家php程序设计有所帮助。
原文链接:http://www.cnblogs.com/woider/p/6380491.html
相关文章
猜你喜欢
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 97
-
2025-05-29 66
-
2025-05-29 95
-
2025-06-04 99
-
Debian 8或Debian 9(64 位)安装 .NET Core
2025-05-29 109
热门评论



