因为我在我的小站上放了不少的图片。
因此有用PHP生成缩略图的问题,看了,有几位朋友写的代码。真的写得很精彩。但是我没有必要有那复杂,我只需要很简单的能够生成缩略图就行了。所谓的生成的缩略图,不过是将原图再拷贝粘贴一次而已。
所以。也就只有几个函数可以使用了(以PNG图像为例的):
imagecreate,
imagepng
imagedestroy
imagecolorallocate
header
imagecopyresampled
imagecreatetruecolor
其中最为关键的就是这个imagecratetruecolor的函数。
下面是我写的丑得要死的代码,只是作个参考而已:
CODE
<?php
/*显示缩略图
*/
$imagedir = $_GET["myimage"];
//生成缩略图
$mysize = getimagesize($imagedir);
$mymime = $mysize["mime"];
//得出高和宽
$mywidth = $mysize[0];
$myheight = $mysize[1];
//如果图像超出这个范围即采用缩略图的
//形式
if ($mywidth > 500 || $myheight > 400)
{
header("Content-type:".$mymime);
$img = imagecreatefrompng($imagedir);
if ( ($mywidth > $myheight)) {
//以宽度为主
$imgwidth = 500;
$imgheight = $myheight * (500/$mywidth);
$img2 = imagecreatetruecolor($imgwidth, $imgheight);
#$bac = imagecolorallocate($img2, 255, 255, 255);
imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
imagepng($img2);
imagedestroy($img);
imagedestroy($img2);
} else if ( $mywidth < $myheight ){
//以高度为主
$imgwidth = $mywidth * ( 400 / $myheight);
$imgheight = 400;
$imgwidth = $mywidth * ( 400 / $mywidth);
$imgheight = 400;
$img2 = imagecreatetruecolor($imgwidth, $imgheight);
#$bac = imagecolorallocate($img2, 255, 255, 255);
imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
imagepng($img2);
imagedestroy($img);
imagedestroy($img2);
}
} else {
header("Content-type:".$mymime);
$img = imagecreatefrompng($imagedir);
$bac = imagecolorallocate($img, 255, 255, 255);
imagepng($img);
imagedestroy($img);
}