2006年7月 的存档
classCreateHtml{functionmkdir($prefix='article'){$y=date('Y');$m=date('m');$d=date('d');$p=DIRECTORY_SEPARATOR;$filePath='article'.$p.$y.$p.$m.$p.$d;$a=explode($p,$filePath);foreach($aas$dir){$path.=$dir.$p;if(!is_dir($path)){//echo'没有这个目录'.$path;mkdir($path,0755);}}return$filePath.$p;}functionstart(){ob_start();}functionend(){$info=ob_get_contents();$fileId='12345';$postfix='.html';$path=$this->mkdir($prefix='article');$fileName=time().'_'.$fileId.$postfix;$file=fopen($path.$fileName,'w ');fwrite($file,$info);fclose($file);ob_end_flush();}}?><?php$s=newCreateHtml();$s->start();?><html><body>asdfasdfasdfasdfasdfasdfasdfasdfasdf<br>adfasdfasdf<br></body>></html><?php$s->end();?>]]>
在生成缩略图的过程当中我们需要用到GD库当中的几个函数: getimagesize(stringfilename[,arrayvar])),取得图像的信息,返回值是一人array,包括几项信息$var[0]—-返回图像的width,$var[1]—-返回height,[2]返回图像文件的type,[4]返回的是与<imgsrc="">当中的wdith,height有关的width="",height=""信息。 imageX(resourceimage) imageY(resourceimage)返回图像的宽和高 imagecopyresized(desimg,srcimg,intdes_x,intdes_y,intsrc_x,intsrc_y,intdes_w,intdes_h,intsrc_w,intsrc_y)复制并截取区域图像 imagecreatetruecolor(intwidth,intheight)创建一个真彩图 imagejpeg(resourceimage) 下面就是Code: <?php#Constantsdefine(IMAGE_BASE,'/var/www/html/mbailey/images');define(MAX_WIDTH,150);define(MAX_HEIGHT,150); #Getimagelocation$image_file=str_replace('..','',$_SERVER['QUERY_STRING']);$image_path=IMAGE_BASE."/$image_file"; #Loadimage$img=null;$ext=strtolower(end(explode('.',$image_path)));if($ext=='jpg'||$ext=='jpeg'){$img=@imagecreatefromjpeg($image_path);}elseif($ext=='png'){$img=@imagecreatefrompng($image_path);#OnlyifyourversionofGDincludesGIFsupport}elseif($ext=='gif'){$img=@imagecreatefrompng($image_path);} #Ifanimagewassuccessfullyloaded,testtheimageforsizeif($img){ #Getimagesizeandscaleratio$width=imagesx($img);$height=imagesy($img);$scale=min(MAX_WIDTH/$width,MAX_HEIGHT/$height); #Iftheimageislargerthanthemaxshrinkitif($scale<1){$new_width=floor($scale*$width);$new_height=floor($scale*$height); #Createanewtemporaryimage$tmp_img=imagecreatetruecolor($new_width,$new_height); #Copyandresizeoldimageintonewimageimagecopyresized($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);imagedestroy($img);$img=$tmp_img;}} #Createerrorimageifnecessaryif(!$img){$img=imagecreate(MAX_WIDTH,MAX_HEIGHT);imagecolorallocate($img,0,0,0);$c=imagecolorallocate($img,70,70,70);imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);} #Displaytheimageheader("Content-type:image/jpeg");imagejpeg($img);?> 我们把上面的Code存储为test.php,然后通过test.php?imagename的形式来访问,结果会让你惊喜的,因为在这里你看到了PHP的优点,它可以让ASP相形见绌。 上面的这段代码当中我们通过end(explode(".",$image_path)来取得文件的扩展名,但是我感觉还是不理想。这样是能够取得文件的类型的,因为end()函数会跳到本array的最后一个单元,但是如果我们采用getimagesize()会取得更为强大的专门针对于图像文件的类型。 本程序显示的缩略图是限制宽高都在150内,然后用min()函数来取得它们比值的最小值来计算缩略图的宽和高,并且通过一系列的GD库函数来取得相应的信息,并且呈现给浏览器,当然你也可以写到你所使用的硬盘当中。 好了,这就是PHP的缩略图功能,大家觉得有什么好的意见可以多多拍砖!]]>
