PHP缩略图,作为PHP处理图片的最基本应用,而缩略图也有多种生成方式,其中,网络最觉的缩略图是严格大小生成的缩略图。本文使用PHP生成的缩略图是按原图的比例缩放,再使用补白扩充至要求的大小...
![PHP无损生成缩略图实例源码下载](http://pjiaxu.com/uploads/image/20120616/20120616212654_54808.png)
渲染原图
要生成图片的缩略图,首先要获取图片的大小信息和类型等,在PHP中,使用getimagesize函数就可以获取到这些信息。代码如下:
01.
function
thumb(
$image
,
$thumb
,
$type
=
''
,
$thumb_width
= 120,
$thumb_height
= 120){
02.
if
(!
file_exists
(
$image
)){
03.
return
flase;
04.
}
05.
$srcinfo
=
getimagesize
(
$image
);
06.
if
(false ===
$srcinfo
){
07.
return
false;
08.
}
09.
if
(
empty
(
$type
)){
10.
$type
= image_type_to_extension(
$srcinfo
[2],false);
11.
}
12.
$type
=
strtolower
(
$type
);
其中要注意的是image_type_extension这个函数,用于获取图片的后缀名,当然也可以用其它的方法去获取,最后的一行代码让所有的后缀后用小写的方式表示。
计算缩略图的比率
如果一张1000x500的图片,要生成100x100的缩略图,应该先缩小到100x50,再补白到100x100,这样才不会失真,否则,图片直接缩为100x100,会被拉伸了,请看以下的两张图,是狼魂博客【http://pjiaxu.com/】首页的缩略图,一张是没有补白的,一张是经过补白的,看看效果如何:
相对来说,左面通过补白的缩略看起来比较自然,右边的缩略,感觉被拉伸得有些难看了。以下是计算比率和补白大小的代码(紧接之前代码):
01.
$scale
= min(
$thumb_width
/
$srcinfo
[0],
$thumb_height
/
$srcinfo
[1]);
02.
/*缩略图的实际大小*/
03.
if
(
$scale
>= 1) {
04.
$width
=
$srcinfo
[0];
05.
$height
=
$srcinfo
[1];
06.
}
else
{
07.
$width
= (int) (
$srcinfo
[0] *
$scale
);
08.
$height
= (int) (
$srcinfo
[1] *
$scale
);
09.
}
10.
/*计算补白的部分大小*/
11.
$x
=
$y
= 0;
12.
if
(
$width
>
$height
)
13.
{
14.
$y
= (
$thumb_height
-
$height
)/2;
15.
}
16.
else
17.
{
18.
$x
= (
$thumb_width
-
$width
)/2;
19.
}
PHP生成缩略图
按照上面的讲述,这时应该生成两张缩略图,一张是按比较生成的的缩略力,一张是按要求大小生成的,比较,1000x500的图片要生成100x100的缩略图,这时就会有以下的两步:
- 创建一张100x50的缩略图,将原图缩小并渲染到这张小图上
- 创建一张100x100的缩略图,上下各进行25高度的补白,再将第一步创建的小图移到这张图的中央位置
这样,一张比较自然无损的缩略图就生成了。但有一种情况要考虑,如果一张10x10的图片,要生成100x100的缩略图,这时,只要第二步就可以了。具体代码如下(紧接上面的代码):
01.
/*渲染原图*/
02.
$function
=
'ImageCreateFrom'
. (
$type
==
'jpg'
?
'jpeg'
:
$type
);
03.
$srcimage
=
$function
(
$image
);
04.
/*创建两张缩略图
05.
$thumbimage是以比较缩放图,$realimage是补白后$thumb_width、$thumb_height大小的缩略图*/
06.
if
(
$type
!=
'gif'
&& function_exists(
'imagecreatetruecolor'
)){
07.
$thumbimage
= imagecreatetruecolor(
$width
,
$height
);
08.
$realimage
= imagecreatetruecolor(
$thumb_width
,
$thumb_height
);
09.
}
else
{
10.
$thumbimage
= imagecreate(
$width
,
$height
);
11.
$realimage
= imagecreate(
$thumb_width
,
$thumb_height
);
12.
}
13.
/*透明背景*/
14.
$background
= imagecolorallocate(
$realimage
,244,244,244);
15.
imagefill(
$realimage
,0,0,
$background
);
16.
imagecolortransparent(
$realimage
,
$background
);
17.
/*缩略图片*/
18.
if
(function_exists(
"ImageCopyResampled"
))
19.
{
20.
imagecopyresampled(
$thumbimage
,
$srcimage
, 0, 0, 0, 0,
$width
,
$height
,
$srcinfo
[0],
$srcinfo
[1]);
21.
}
22.
else
23.
{
24.
imagecopyresized(
$thumbimage
,
$srcimage
, 0, 0, 0, 0,
$width
,
$height
,
$srcinfo
[0],
$srcinfo
[1]);
25.
}
26.
/*补白图片*/
27.
imagecopymerge(
$realimage
,
$thumbimage
,
$x
,
$y
, 0, 0,
$width
,
$height
, 100);
28.
$create_func
=
'image'
. (
$type
==
'jpg'
?
'jpeg'
:
$type
);
29.
$create_func
(
$realimage
,
$thumb
);
30.
imagedestroy(
$thumbimage
);
31.
imagedestroy(
$srcimage
);
32.
imagedestroy(
$realimage
);
33.
return
true;