很多站长朋友们都不太清楚php生成略缩图,今天小编就来给大家整理php生成略缩图,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php创建缩略图问题 2、 ThinkPHP3.2.3 上传图片到ftp,同时生成缩略图。 3、 如何使php使用gd无法生成缩略图 4、 phpcms v9 thumb函数不能生成缩略图,还是现实的原图 5、 php怎么生成缩略图 6、 php怎么给psd图片生成缩略图??? 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);
希望我的回答你能满意
ThinkPHP3.2.3 上传图片到ftp,同时生成缩略图。ThinkPHP上传文件类:
$upload = new \Think\Upload($config);// 实例化上传类
使用这个。
如要处理图片大小。需要另外调用图像处理:
裁剪图片
$image = new \Think\Image();
$image->open('./1.jpg');
//将图片裁剪为400x400并保存为corp.jpg
$image->crop(400, 400)->save('./crop.jpg');
居中裁剪
$image = new \Think\Image();
$image->open('./1.jpg');
// 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
$image->thumb(150, 150,\Think\Image::IMAGE_THUMB_CENTER)->save('./thumb.jpg');
如何使php使用gd无法生成缩略图php利用GD库生成缩略图。
[php] view plain copy print?在CODE上查看代码片派生到我的代码片
<form method="post" action="suo_do.php" enctype="multipart/form-data">
<input type="file" name="pic" />
<input type="submit" value="上传1" />
</form>
<?php
header("content-type:text/html;charset=gbk");
ini_set("date.timezone","Asia/chong");
//判断文件是否为空
if(empty($_FILES)){
echo"上传文件过大";
exit;
}
//判断文件上传是否有错误
if($_FILES['pic']['error']){
echo "上传文件";
exit;
}
//判断文件类型是否非法获取文件后缀
$allowtype=array("jpg","png","jpeg","gif");
$a=explode('.',$_FILES['pic']['name']);
$index=count($a)-1;
$ex=strtolower($a[$index]);
if(!in_array($ex,$allowtype)){
echo "上传文件非法";
exit;
}
$file=date('YmdHis').rand().".".$ex;
$src=$_FILES['pic']['tmp_name'];
$des="upload/".$file;
$rs=move_uploaded_file($src,$des);
//缩略图
//读取已经上传图片
$image=imagecreatefromjpeg($des);
$a=getimagesize($des);
$w=$a[0];
$h=$a[1];
if($w>$h){
$width=300;
$height=$width/$w*$h;
}else if($w<$h){
$height=300;
$width=$height/$h*$w;
}else{
$width=300;
$height=300;
}
//创建空白新图片
$newimage=imagecreatetruecolor($width, $height);
//copy源图片内容 copy新图片
imagecopyresized($newimage, $image, 0,0, 0,0, $width, $height, $w, $h);
$filename="upload/s_".$file;
imagejpeg($newimage,$filename);
phpcms v9 thumb函数不能生成缩略图,还是现实的原图如果想得到指定尺寸的缩略图
建议修改模型的缩略图字段,设置其缩略图尺寸为自己需要的尺寸
PHPCMS自带的缩略图函数不是非常好用
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);
}
}
代码已经测试,成功运行!
php怎么给psd图片生成缩略图???psd是不会通过缩略图显示的,用其他能打开它的软件可以外,其他都是没法的。你这个php生成缩略图是在什么地方生成嘛,在网页上面的话浏览器只支持jpg/gif/png这几种图片格式,其他的都是个别支持不通用的。
求采纳
关于php生成略缩图的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php生成略缩图 php 生成图片的详细内容...