很多站长朋友们都不太清楚php制作缩略图,今天小编就来给大家整理php制作缩略图,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php怎么生成缩略图 2、 thinkphp 3.1.2生成缩略图 image类怎么引入 3、 php创建缩略图问题 4、 用PHP怎么生成高质量的缩略图 php怎么生成缩略图给你个函数吧
// *****生成缩略图*****
// 只考虑jpg,png,gif格式
// $srcImgPath 源图象路径
// $targetImgPath 目标图象路径
// $targetW 目标图象宽度
// $targetH 目标图象高度
function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH)
{
$imgSize = GetImageSize($srcImgPath);
$imgType = $imgSize[2];
//@ 使函数不向页面输出错误信息
switch ($imgType)
{
case 1:
$srcImg = @ImageCreateFromGIF($srcImgPath);
break;
case 2:
$srcImg = @ImageCreateFromJpeg($srcImgPath);
break;
case 3:
$srcImg = @ImageCreateFromPNG($srcImgPath);
break;
}
//取源图象的宽高
$srcW = ImageSX($srcImg);
$srcH = ImageSY($srcImg);
if($srcW>$targetW || $srcH>$targetH)
{
$targetX = 0;
$targetY = 0;
if ($srcW > $srcH)
{
$finaW=$targetW;
$finalH=round($srcH*$finaW/$srcW);
$targetY=floor(($targetH-$finalH)/2);
}
else
{
$finalH=$targetH;
$finaW=round($srcW*$finalH/$srcH);
$targetX=floor(($targetW-$finaW)/2);
}
//function_exists 检查函数是否已定义
//ImageCreateTrueColor 本函数需要GD2.0.1或更高版本
if(function_exists("ImageCreateTrueColor"))
{
$targetImg=ImageCreateTrueColor($targetW,$targetH);
}
else
{
$targetImg=ImageCreate($targetW,$targetH);
}
$targetX=($targetX<0)?0:$targetX;
$targetY=($targetX<0)?0:$targetY;
$targetX=($targetX>($targetW/2))?floor($targetW/2):$targetX;
$targetY=($targetY>($targetH/2))?floor($targetH/2):$targetY;
//背景白色
$white = ImageColorAllocate($targetImg, 255,255,255);
ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);
/*
PHP的GD扩展提供了两个函数来缩放图象:
ImageCopyResized 在所有GD版本中有效,其缩放图象的算法比较粗糙,可能会导致图象边缘的锯齿。
ImageCopyResampled 需要GD2.0.1或更高版本,其像素插值算法得到的图象边缘比较平滑,
该函数的速度比ImageCopyResized慢。
*/
if(function_exists("ImageCopyResampled"))
{
ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
else
{
ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
switch ($imgType) {
case 1:
ImageGIF($targetImg,$targetImgPath);
break;
case 2:
ImageJpeg($targetImg,$targetImgPath);
break;
case 3:
ImagePNG($targetImg,$targetImgPath);
break;
}
ImageDestroy($srcImg);
ImageDestroy($targetImg);
}
else //不超出指定宽高则直接复制
{
copy($srcImgPath,$targetImgPath);
ImageDestroy($srcImg);
}
}
代码已经测试,成功运行!
thinkphp 3.1.2生成缩略图 image类怎么引入Thinkphp调用Image类生成缩略图的方法具体分析如下:
Thinkphp的Image类 在ThinkPHP/Extend/Library/ORG/Util/Image.class.php中。
调用方法如下:
?1234567 import("ORG.Util.Image"); $Img = new Image();//实例化图片类对象 $image_path = './图片路径'; //若当前php文件在Thinkphp的中APP_PATH路径中 //'./'就是index.php的上一级文件。 //因为APP_PATH是通过index.php定义和加载的。 $image_info = $Img::getImageInfo($image_path);//获取图片信息
getImageInfo方法会获取图片的width,height,type,size,mime等信息。
缩略图的生成很简单。
参数需要img_path(原图路径),thumb_name(缩略图名,包含路径),thumb_type(图片类型),Max_width(宽),Max_height(高):
?12 //生成缩略图: $Img::thumb2($img_path,$thumb_name,$thumb_type,$Max_width,$Max_height);
需要注意的是,缩略图的宽和高不能比原图的大,不然就会生成失败
php创建缩略图问题其实PHP创建缩略图就是在PHP在原图片的基础上创建一张新的图片的过程,而用PHP创建图像的过程一般分成四部:
第一步:创建一张画布(只要是画图都需要一张画布的)
第二步:在画布画东西(可以画各种图形,如长方形,直线,等等,也可以在画布上写字啥的,或者画其他的图形)
第三步:画完图之后,将图片输出,将图片输出到浏览器,在浏览器显示出来,或者保存为一张新 的图片(缩略图一般是保存为图片文件的)
第四步:因为创建画布时打开了文件流,所以要关闭资源,节省内存。(个人觉得你可以这样理解,打开了一画布,把它铺开了,画完了就把画布卷起来,收起来,不要占着铺的地方)
具体的代码如下:(这段代码来源于ThinkPHP的图像类)
<?php
class Thumb{
/**
* @param string $image 原图
* @param string $thumbname 缩略图文件名
* @param string $type 图像格式
* @param string $maxWidth 宽度
* @param string $maxHeight 高度
*/
static create($img, $thumbname, $type='', $maxWidth=200, $maxHeight=50)
{
$info = getimagesize($img); //获取原图的图像信息(长、宽、格式等)
if ($info !== false) {
$srcWidth = $info['width'];
$srcHeight = $info['height'];
$type = empty($type) ? $info['type'] : $type;
$type = strtolower($type);
$interlace = $interlace ? 1 : 0;
unset($info);
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
if ($scale >= 1) {
// 超过原图大小不再缩略
$width = $srcWidth;
$height = $srcHeight;
} else {
// 缩略图尺寸
$width = (int) ($srcWidth * $scale);
$height = (int) ($srcHeight * $scale);
}
// 载入原图(在原图的基础上创建画布,为第一步)
$createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
if(!function_exists($createFun)) {
return false;
}
$srcImg = $createFun($image);
//第二步开始
//创建缩略图
if ($type != 'gif' function_exists('imagecreatetruecolor'))
$thumbImg = imagecreatetruecolor($width, $height);
else
$thumbImg = imagecreate($width, $height);
//png和gif的透明处理 by luofei614
if('png'==$type){
imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题)
imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)
}elseif('gif'==$type){
$trnprt_indx = imagecolortransparent($srcImg);
if ($trnprt_indx >= 0) {
//its transparent
$trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
$trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($thumbImg, 0, 0, $trnprt_indx);
imagecolortransparent($thumbImg, $trnprt_indx);
}
}
// 复制图片
if (function_exists("ImageCopyResampled"))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
//第三步:输出图像
// 生成图片
$imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
$imageFun($thumbImg, $thumbname);
//第四步:关闭画布
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}
}
?>
你使用的时候直接用:
require Thumb.class.php
$thumb = Thumb::create('s.jpg','thumb_s.jpg',100,50);
希望我的回答你能满意
用PHP怎么生成高质量的缩略图ImageMagick没用过,一般直接用内置的GD库,没有发现你说的这么严重的失真问题。
利用GD库创建缩略图的大致思路如下:
依据设定的尺寸创建真彩色画布$im=createtruecolor(120,90);
读取原始文件尺寸,按照原始尺寸的宽度和高度比例,计算出缩略图的大小(可能与给定的尺寸有一定的偏差)
将原始图像拷贝并缩放到创建的真彩色缩略图画布上。
输出缩略图文件。
可能就是因为利用的是这个真彩色,缩略图效果还凑合,也不是说绝对不失真的
你可以去后盾人平台看看,里面的东西不错
关于php制作缩略图的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php制作缩略图 php缩小图片的详细内容...