您的位置:首页 > 编程语言 > PHP开发

PHP图像裁剪为任意大小的图像,图像不变形,不留下空白

2014-07-14 11:22 323 查看
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
widthorheighterror!'
;
011
die
;
012
}
013
if
(!
file_exists
(
$src_file
))
{
014
echo
$src_file
.
'
isnotexists!'
;
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
typeofimagedoesnotsupport!onlysupportjpg,giforpng'
;
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
imageerror!'
;
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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: