php缩略图类
A. 能直接用的php生成缩略图的程序(要求简单)
<?php
/*构造函数-生成缩略图+水印,参数说明:
$srcFile-图片文件名,
$dstFile-另存文件名,
$markwords-水印文字,
$markimage-水印图片,
$dstW-图片保存宽度,
$dstH-图片保存高度,
$rate-图片保存品质*/
makethumb("a.jpg","b.jpg","50","50");
function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$markwords=null,$markimage=null)
{
$data = GetImageSize($srcFile);
switch($data[2])
{
case 1:
$im=@ImageCreateFromGIF($srcFile);
break;
case 2:
$im=@ImageCreateFromJPEG($srcFile);
break;
case 3:
$im=@ImageCreateFromPNG($srcFile);
break;
}
if(!$im) return False;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if ($srcW*$dstH>$srcH*$dstW)
{
$fdstH = round($srcH*$dstW/$srcW);
$dstY = floor(($dstH-$fdstH)/2);
$fdstW = $dstW;
}
else
{
$fdstW = round($srcW*$dstH/$srcH);
$dstX = floor(($dstW-$fdstW)/2);
$fdstH = $dstH;
}
$ni=ImageCreateTrueColor($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$white = ImageColorAllocate($ni,255,255,255);
$black = ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
if($markwords!=null)
{
$markwords=iconv("gb2312","UTF-8",$markwords);
//转换文字编码
ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords); //写入文字水印
//参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
}
elseif($markimage!=null)
{
$wimage_data = GetImageSize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@ImageCreateFromGIF($markimage);
break;
case 2:
$wimage=@ImageCreateFromJPEG($markimage);
break;
case 3:
$wimage=@ImageCreateFromPNG($markimage);
break;
}
image($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31
imagedestroy($wimage);
}
ImageJpeg($ni,$dstFile,$rate);
ImageJpeg($ni,$srcFile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?>
B. php上传图片自动生成缩略图
########################## index.php
<form action="index.php?item=file_upload_ok" method="post" enctype="multipart/form-data">
<input type="file" name="download" >
<INPUT TYPE="text" NAME="describe" >
<INPUT TYPE="submit" value="提交">
</form>
<?
if(!empty($_GET['item'])){
if($_GET['item'] == 'file_upload_ok')
{
// 引入图片类
include("thumb_class.php");
$t = new ThumbHandler();
// 获取上传文件
$file=$_FILES[download];
$yName = $file[name]; // 原文件名字
$tmpName = $file[tmp_name]; // 原文件句柄
// 图片 缩放
$t->setSrcImg($tmpName); //原文件
$t->setDstImg("new_x.jpg");//目标图片
$t->createImg(80,80); //生成图片 宽 300 高 300
// 图片原始大小
$t->setSrcImg($tmpName); //原文件
$t->setDstImg("new_d.jpg");//目标图片
$t->createImg($t->src_w,$t->src_h); //生成图片 $t->src_w 原图片宽 $t->src_h 原图片高
}
}
#############
thumb_class.php 图片类文件 由于代码过多请到我 空间 日 记 查看
h t t p ://user.qzone.qq.com/182887459/
C. php的缩略图 截取 黑色
$white = imagecolorallocate($im,255,255,255)
D. php上传图片生成三张缩略图,大中小。帮忙看一下
public function disposeImgAction($tmp_path, $dst_w, $file_path, $file_name)
{
$arr=getimagesize($tmp_path);
$src_w=$arr[0];
$src_h=$arr[1];
$type=$arr[2];
switch($type){
case 1:$src_im=imagecreatefromgif($tmp_path);break;
case 2:$src_im=imagecreatefromjpeg($tmp_path);break;
case 3:$src_im=imagecreatefrompng($tmp_path);break;
default:UtlsSvc::showMsg('不支持该图片类型','/coinproct/index/');
}
if ($dst_w == 500) {
$dst_h = 385;
} elseif ($dst_w == 300) {
$dst_h = 225;
} elseif ($dst_w == 100) {
$dst_h = 75;
}
$dst_im=imagecreatetruecolor($dst_w,$dst_h);
imageresized($dst_im,$src_im,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($dst_im, $file_path.'/'.$file_name);
}
这是我自己写的一个函数 第一个是上传的文件临时目录 第二个是 缩略后的宽度 里面你可以自己计算高度的比例 第三个是存到什么目录 第四个是文件名
E. PHP 图片上传生成缩略图
//2014年3月5日15:08:02因为需要做缩略图,所以改用thinkphp来做上传,它支持时间戳命名,方便命名,以及更名
//这是以前网络到的,然后使用的缩略图代码,需要cg库支持
/**
*生成缩略图
*@[email protected]
*@paramstring源图绝对完整地址{带文件名及后缀名}
*@paramstring目标图绝对完整地址{带文件名及后缀名}
*@paramint缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
*@paramint缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
*@paramint是否裁切{宽,高必须非0}
*@paramint/float缩放{0:不缩放,0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
*@returnboolean
*/
functionfileext($file)
{
returnstrtolower(pathinfo($file,PATHINFO_EXTENSION));
}
functionimg2thumb($src_img,$dst_img,$width=75,$height=75,$cut=0,$proportion=0)
{
if(!is_file($src_img))
{
returnfalse;
}
$ot=$this->fileext($dst_img);
$otfunc='image'.($ot=='jpg'?'jpeg':$ot);
$srcinfo=getimagesize($src_img);
$src_w=$srcinfo[0];
$src_h=$srcinfo[1];
$type=strtolower(substr(image_type_to_extension($srcinfo[2]),1));
$createfun='imagecreatefrom'.($type=='jpg'?'jpeg':$type);
$dst_h=$height;
$dst_w=$width;
$x=$y=0;
/**
*缩略图不超过源图尺寸(前提是宽或高只有一个)
*/
if(($width>$src_w&&$height>$src_h)||($height>$src_h&&$width==0)||($width>$src_w&&$height==0))
{
$proportion=1;
}
if($width>$src_w)
{
$dst_w=$width=$src_w;
}
if($height>$src_h)
{
$dst_h=$height=$src_h;
}
if(!$width&&!$height&&!$proportion)
{
returnfalse;
}
if(!$proportion)
{
if($cut==0)
{
if($dst_w&&$dst_h)
{
if($dst_w/$src_w>$dst_h/$src_h)
{
$dst_w=$src_w*($dst_h/$src_h);
$x=0-($dst_w-$width)/2;
}
else
{
$dst_h=$src_h*($dst_w/$src_w);
$y=0-($dst_h-$height)/2;
}
}
elseif($dst_wxor$dst_h)
{
if($dst_w&&!$dst_h)//有宽无高
{
$propor=$dst_w/$src_w;
$height=$dst_h=$src_h*$propor;
}
elseif(!$dst_w&&$dst_h)//有高无宽
{
$propor=$dst_h/$src_h;
$width=$dst_w=$src_w*$propor;
}
}
}
else
{
if(!$dst_h)//裁剪时无高
{
$height=$dst_h=$dst_w;
}
if(!$dst_w)//裁剪时无宽
{
$width=$dst_w=$dst_h;
}
$propor=min(max($dst_w/$src_w,$dst_h/$src_h),1);
$dst_w=(int)round($src_w*$propor);
$dst_h=(int)round($src_h*$propor);
$x=($width-$dst_w)/2;
$y=($height-$dst_h)/2;
}
}
else
{
$proportion=min($proportion,1);
$height=$dst_h=$src_h*$proportion;
$width=$dst_w=$src_w*$proportion;
}
$src=$createfun($src_img);
$dst=imagecreatetruecolor($width?$width:$dst_w,$height?$height:$dst_h);
$white=imagecolorallocate($dst,255,255,255);
imagefill($dst,0,0,$white);
if(function_exists('imageresampled'))
{
imageresampled($dst,$src,$x,$y,0,0,$dst_w,$dst_h,$src_w,$src_h);
}
else
{
imageresized($dst,$src,$x,$y,0,0,$dst_w,$dst_h,$src_w,$src_h);
}
$otfunc($dst,$dst_img);
imagedestroy($dst);
imagedestroy($src);
returntrue;
}
F. 关于PHP缩略图程序
Warning: imagedestroy() expects parameter 1 to be resource
看错误提示应可能是 $rs_al["zp"] 空值或输出的图片路径不正确
if(empty ($rs_al["zp"])){
echo '空值!';
}elseif(!file_exists($rs_al["zp"])){
echo '文件不存在或路径不正确';
}else{
$thumb -> Createthumb($rs_al["zp"]);
}
G. 用PHP怎么生成高质量的缩略图
ImageMagick没用过,一般直接用内置的GD库,没有发现你说的这么严重的失真问题。
利用GD库创建缩略图的大致思路如下:
依据设定的尺寸创建真彩色画布$im=createtruecolor(120,90);
读取原始文件尺寸,按照原始尺寸的宽度和高度比例,计算出缩略图的大小(可能与给定的尺寸有一定的偏差)
将原始图像拷贝并缩放到创建的真彩色缩略图画布上。
输出缩略图文件。
可能就是因为利用的是这个真彩色,缩略图效果还凑合,也不是说绝对不失真的
你可以去后盾人平台看看,里面的东西不错
H. php按百分比生成缩略图的代码分享
于是翻了一下手册,弄懂几个函数后自己写了一个简单的php生成缩略图的程序。没有用类,我觉得一个函数就能搞定,而且对于新手来说更容易去理解,从而可以帮助到更多的人。
支持按比分比缩略,支持按指定的长宽缩略,默认按百分比。程序中注释已经很详细了,如有问题可在下面留言,欢迎与我交流。
源码如下:
复制代码
代码如下:
<?php
/*
*
param
ori_img
原图像的名称和路径
*
param
new_img
生成图像的名称
*
param
percent
表示按照原图的百分比进行缩略,此项为空时默认按50%
*
param
width
指定缩略后的宽度
*
param
height
指定缩略后的高度
*
*
注:当
percent
width
height
都传入值的时候,且percent>0时,优先按照百分比进行缩略
*
by:http://www.jb51.net
更多源码与你分享
*
温馨提示:使用此功能要在php.ini中开启
gd2
*
**/
function
makeThumb($ori_img,
$new_img,
$percent=50,
$width=0,
$height=0){
$original
=
getimagesize($ori_img); //得到图片的信息,可以print_r($original)发现它就是一个数组
//$original[2]是图片类型,其中1表示gif、2表示jpg、3表示png
switch($original[2]){
case
1
:
$s_original
=
imagecreatefromgif($ori_img);
break;
case
2
:
$s_original
=
imagecreatefromjpeg($ori_img);
break;
case
3
:
$s_original
=
imagecreatefrompng($ori_img);
break;
}
if($percent
>
0){
$width
=
$original[0]
*
$percent
/
100;
$width
=
($width
>
0)
?
$width
:
1;
$height
=
$original[1]
*
$percent
/
100;
$height
=
($height
>
0)
?
$height
:
1;
}
//创建一个真彩的画布
$canvas
=
imagecreatetruecolor($width,$height);
imageresized($canvas,
$s_original,
0,
0,
0,
0,
$width,
$height,
$original[0],
$original[1]);
//header("Content-type:image/jpeg");
//imagejpeg($canvas); //向浏览器输出图片
$loop
=
imagejpeg($canvas,
$new_img); //生成新的图片
if($loop){
echo
"OK!<br/>";
}
}
makeThumb("bhsj.jpg","suolue1.jpg",15,0,0); //生成原图15%的缩略图
makeThumb("bhsj.jpg","suolue2.jpg",0,200,120); //生成宽为100px,高为60px的缩略图
makeThumb("bhsj.jpg","suolue3.jpg",15,200,120); //生成原图15%的缩略图(参数都填时,百分率优先级大)
?>
I. php制作png格式的缩略图
直接header("Content-Type:image/png");
J. PHP缩略图组件的使用
将图片等比例缩放一下再放到框子里,等比例缩放函数要调用GD库的函数,缩放函数要自己写,网上也能搜到