本文目录一览:
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);
}
if (function_exists("ImageCreateTrueColor"))
{
$targetImg = ImageCreateTrueColor($targetW, $targetH);
}
else
{
$targetImg = ImageCreate($targetW, $targetH);
}
$targetX = ($targetX < 0) ? 0 : $targetX;
$targetY = ($targetY < 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);
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);
}
}
代码已经测试,成功运行!
用PHP怎么生成高质量的缩略图
ImageMagick
没用过,一般直接用内置的GD库,没有发现你说的这么严重的失真问题。
利用GD库创建缩略图的大致思路如下:
- 依据设定的尺寸创建真彩色画布
$im = create_true_color(120, 90);
- 读取原始文件尺寸,按照原始尺寸的宽度和高度比例,计算出缩略图的大小(可能与给定的尺寸有一定的偏差)
- 将原始图像拷贝并缩放到创建的真彩色缩略图画布上。
- 输出缩略图文件。 可能就是因为利用的是这个真彩色,缩略图效果还凑合,也不是说绝对不失真的 你可以去后盾人平台看看,里面的东西不错。
php简单缩略图类|image.class.php
使用方法:
$img = new image;
$img->resize('dstimg.jpg', 'srcimg.jpg', 100, 100); // 说明:这个是按照比例缩放,'dstimg.jpg'是目标文件,'srcimg.jpg'是源文件,后面的是目标文件的宽和高
$img->thumb('dstimg.jpg', 'scrimg.jpg', 100, 100); // 说明:这个是按照比例缩略图,比如常用在头像缩略图的时候,'dstimg.jpg'是目标文件,'scrimg.jpg'是源文件,后面的是目标文件的宽和高
这个是针对GD库才这样麻烦的,如果采用Imagick的话,就只需要两个函数就实现上面的功能,去查下文档就找到了。
<?php
class image
{
public function resize($dstImg, $srcImg, $dstW, $dstH)
{
list($srcW, $srcH) = getimagesize($srcImg);
$scale = min($dstW / $srcW, $dstH / $srcH);
$newW = round($srcW * $scale);
$newH = round($srcH * $scale);
$newImg = imagecreatetruecolor($newW, $newH);
$srcImg = imagecreatefromjpeg($srcImg);
imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newW, $newH, $srcW, $srcH);
imagejpeg($newImg, $dstImg);
}
public function thumb($dstImg, $srcImg, $dstW, $dstH)
{
list($srcW, $srcH) = getimagesize($srcImg);
$scale = max($dstW / $srcW, $dstH / $srcH);
$newW = round($dstW / $scale);
$newH = round($dstH / $scale);
$x = ($srcW - $newW) / 2;
$y = ($srcH - $newH) / 2;
$newImg = imagecreatetruecolor($dstW, $dstH);
$srcImg = imagecreatefromjpeg($srcImg);
imagecopyresampled($newImg, $srcImg, 0, 0, $x, $y, $dstW, $dstH, $newW, $newH);
imagejpeg($newImg, $dstImg);
}
}
lishixinzhi/Article/program/PHP/201311/21312
php创建缩略图问题
其实PHP创建缩略图就是在PHP在原图片的基础上创建一张新的图片的过程,而用PHP创建图像的过程一般分成四步:
- 创建一张画布(只要是画图都需要一张画布的)
- 在画布上画东西(可以画各种图形,如长方形、直线等,也可以在画布上写字或画其他图形)
- 画完图之后,将图片输出,将图片输出到浏览器显示出来,或者保存为一张新的图片(缩略图一般是保存为图片文件的)
- 因为创建画布时打开了文件流,所以要关闭资源,节省内存(个人觉得你可以这样理解,打开了一张画布,把它铺开了,画完了就把画布卷起来,收起来,不要占着铺的地方) 具体的代码如下(这段代码来源于ThinkPHP的图像类):
<?php
class Thumb
{
/**
* @param string $image 原图
* @param string $thumbname 缩略图文件名
* @param string $type 图像格式
* @param string $maxWidth 宽度
* @param string $maxHeight 高度
*/
static function 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);
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($img);
// 第二步开始
// 创建缩略图
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);
希望我的回答你能满意。