【php】图像裁剪
来源:互联网 发布:java书籍借阅 编辑:程序博客网 时间:2024/05/21 17:39
001
<?php
002
/**
003
* 说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形
004
* 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高
005
*/
006
// 获得任意大小图像,不足地方拉伸,不产生变形,不留下空白
007
function
my_image_resize(
$src_file
,
$dst_file
,
$new_width
,
$new_height
)
008
{
009
if
(
$new_width
< 1 ||
$new_height
< 1) {
010
echo
'params width or height error !'
;
011
die
;
012
}
013
if
(!
file_exists
(
$src_file
)) {
014
echo
$src_file
.
' is not exists !'
;
015
die
;
016
}
017
// 图像类型
018
$type
= exif_imagetype(
$src_file
);
019
$support_type
=
array
(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
020
if
(!in_array(
$type
,
$support_type
, true)) {
021
echo
'this type of image does not support! only support jpg , gif or png'
;
022
die
;
023
}
024
//Load image
025
switch
(
$type
) {
026
case
IMAGETYPE_JPEG:
027
$src_img
= imagecreatefromjpeg(
$src_file
);
028
break
;
029
case
IMAGETYPE_PNG:
030
$src_img
= imagecreatefrompng(
$src_file
);
031
break
;
032
case
IMAGETYPE_GIF:
033
$src_img
= imagecreatefromgif(
$src_file
);
034
break
;
035
default
:
036
echo
'Load image error!'
;
037
die
;
038
}
039
$w
= imagesx(
$src_img
);
040
$h
= imagesy(
$src_img
);
041
$ratio_w
= (1.0 *
$new_width
) /
$w
;
042
$ratio_h
= (1.0 *
$new_height
) /
$h
;
043
$ratio
= 1.0;
044
// 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了)
045
if
(
$ratio_w
< 1 &&
$ratio_h
< 1 ||
$ratio_w
> 1 &&
$ratio_h
> 1) {
046
if
(
$ratio_w
<
$ratio_h
) {
047
$ratio
=
$ratio_h
;
048
}
else
{
049
$ratio
=
$ratio_w
;
050
}
051
// 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
052
$inter_w
= (int) (
$new_width
/
$ratio
);
053
$inter_h
= (int) (
$new_height
/
$ratio
);
054
$inter_img
= imagecreatetruecolor(
$inter_w
,
$inter_h
);
055
imagecopy(
$inter_img
,
$src_img
, 0, 0, 0, 0,
$inter_w
,
$inter_h
);
056
// 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
057
// 定义一个新的图像
058
$new_img
= imagecreatetruecolor(
$new_width
,
$new_height
);
059
imagecopyresampled(
$new_img
,
$inter_img
, 0, 0, 0, 0,
$new_width
,
$new_height
,
$inter_w
,
$inter_h
);
060
switch
(
$type
) {
061
case
IMAGETYPE_JPEG:
062
imagejpeg(
$new_img
,
$dst_file
, 100);
063
// 存储图像
064
break
;
065
case
IMAGETYPE_PNG:
066
imagepng(
$new_img
,
$dst_file
, 100);
067
break
;
068
case
IMAGETYPE_GIF:
069
imagegif(
$new_img
,
$dst_file
, 100);
070
break
;
071
default
:
072
break
;
073
}
074
}
else
{
075
$ratio
=
$ratio_h
>
$ratio_w
?
$ratio_h
:
$ratio_w
;
076
//取比例大的那个值
077
// 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大
078
$inter_w
= (int) (
$w
*
$ratio
);
079
$inter_h
= (int) (
$h
*
$ratio
);
080
$inter_img
= imagecreatetruecolor(
$inter_w
,
$inter_h
);
081
//将原图缩放比例后裁剪
082
imagecopyresampled(
$inter_img
,
$src_img
, 0, 0, 0, 0,
$inter_w
,
$inter_h
,
$w
,
$h
);
083
// 定义一个新的图像
084
$new_img
= imagecreatetruecolor(
$new_width
,
$new_height
);
085
imagecopy(
$new_img
,
$inter_img
, 0, 0, 0, 0,
$new_width
,
$new_height
);
086
switch
(
$type
) {
087
case
IMAGETYPE_JPEG:
088
imagejpeg(
$new_img
,
$dst_file
, 100);
089
// 存储图像
090
break
;
091
case
IMAGETYPE_PNG:
092
imagepng(
$new_img
,
$dst_file
, 100);
093
break
;
094
case
IMAGETYPE_GIF:
095
imagegif(
$new_img
,
$dst_file
, 100);
096
break
;
097
default
:
098
break
;
099
}
100
}
101
}
今天用到一个“图片裁切工具类”,报错“Call to undefined function exif_imagetype”,
解决方案如下:
“Call to undefined function exif_imagetype”PHP会出现这样的错误,一般是因为配置的问题把php.ini里面的extension=php_exif.dll取消掉注释,然而还是可能同样出错,
一定要注意的是
extension=php_mbstring.dll 必须放在它的前面且去掉释义符”;”,这样就能解决问题
- 【php】图像裁剪
- php 裁剪图像
- php 图像裁剪(自定义裁剪图片大小)
- php任意图像裁剪成固定大小
- php 裁剪为任意大小的图像,图像不变形
- 图像裁剪
- 图像裁剪
- 图像裁剪
- 【PHP】PHP图像裁剪缩略裁切类源代码及使用方法
- php任意图像裁剪成固定大小[zz]
- PHP图像裁剪为任意大小的图像,图像不变形,不留下空白
- html5使用图像-图像裁剪
- 插件15:图像裁剪
- Android 裁剪图像
- WPF Image图像裁剪
- 图像压缩(裁剪)
- opencv图像裁剪
- 图像裁剪的实现
- NeL Network Layers
- MyEclipse iReport JasperReport Struts2
- wsdl文件详解和在myeclipse如何打开wsdl文件的
- NeL Network Layer 1
- ORA-13504: No SYSAUX datafile clause specified
- 【php】图像裁剪
- Cocos2d开发系列(一)
- 我回来了!
- Cocos2d开发系列(二)
- 论文读书笔记-Multi-Label Classification on Tree- and DAG-Structured Hierarchies
- Android权限之三共享UID和签名
- NET Framework 中的反射
- Ubuntu machine - no interfaces listed
- Cocos2d开发系列(三)