本版版主招募中

 
标题: 简易的生成较好质量的缩略图的方法
黄叶
技术专家
Rank: 14Rank: 14Rank: 14Rank: 14



UID 1004
精华 5
积分 57
帖子 93
活跃指数 0
LU金币 5406 个
LU金条 0 个
阅读权限 200
注册 2003-10-26
 
发表于 2003-10-29 16:34  资料  个人空间  短消息  加为好友 
因为我在我的小站上放了不少的图片。
因此有用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);

                                                                                                                           

}

     

顶部
 



当前时区 GMT+8, 现在时间是 2008-11-23 17:48
乐悠LoveUnix论坛-京ICP备05005823号

Thanks to Discuz!  © 2001-2007    Power by LoveUnix.net
Processed in 0.051066 second(s), 6 queries , Gzip enabled

清除 Cookies - 联系我们 - 乐悠LoveUnix - Archiver