php中制作缩略图的方法也很简单,是用imagecopyresampled方法根据源图制作一个小一点的图片,来看代码check_image_addthumbs.php

<?php
//修改图片效果
$db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to database');
mysql_select_db('moviesite',$db) or die(mysql_error($db));
//上传文件的路径
$dir = 'D:\Serious\phpdev\test\images';
//缩略图的路径
$thumbdir = 'D:\Serious\phpdev\test\images\thumbs';
//设置环境变量
putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "arial"; //upload_image.php页面传递过来的参数,如果是上传图片
if($_POST['submit'] == 'Upload')
{
if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
switch($_FILES['uploadfile']['error'])
{
case UPLOAD_ERR_INI_SIZE:
die('The uploaded file exceeds the upload_max_filesize directive');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
break;
case UPLOAD_ERR_PARTIAL:
die('The uploaded file was only partially uploaded');
break;
case UPLOAD_ERR_NO_FILE:
die('No file was uploaded');
break;
case UPLOAD_ERR_NO_TMP_DIR:
die('The server is missing a temporary folder');
break;
case UPLOAD_ERR_CANT_WRITE:
die('The server fail to write the uploaded file to the disk');
break;
case UPLOAD_ERR_EXTENSION:
die('The upload stopped by extension');
break;
}
}
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = date('Y-m-d');
list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);
$error = 'The file you upload is not a supported filetype';
switch($type)
{
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die($error);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die($error);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die($error);
break;
default:
break;
}
$query = 'insert into images(image_caption,image_username,image_date) values("'.$image_caption.'" , "'.$image_username.'","'.$image_date.'")';
$result = mysql_query($query,$db) or die(mysql_error($db));
$last_id = mysql_insert_id(); // $imagename = $last_id.'.jpg';
// imagejpeg($image,$dir.'/'.$imagename);
// imagedestroy($image); $image_id = $last_id;
imagejpeg($image , $dir.'/'.$image_id.'.jpg');
imagedestroy($image);
}
else //如果图片已经上传,则从数据库中取图片名字
{
$query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];
$result = mysql_query($query,$db) or die(mysql_error($db));
extract(mysql_fetch_assoc($result));
list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.'.jpg');
} //如果是保存图片
if($_POST['submit'] == 'Save')
{
if(isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir.'/'.$_POST['id'].'.jpg'))
{
$image = imagecreatefromjpeg($dir.'/'.$_POST['id'].'.jpg');
}
else
{
die('invalid image specified');
}
$effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;
switch($effect)
{
case IMG_FILTER_NEGATE:
imagefilter($image , IMG_FILTER_NEGATE); //将图像中所有颜色反转
break;
case IMG_FILTER_GRAYSCALE:
imagefilter($image , IMG_FILTER_GRAYSCALE); //将图像转换为灰度的
break;
case IMG_FILTER_EMBOSS:
imagefilter($image , IMG_FILTER_EMBOSS); //使图像浮雕化
break;
case IMG_FILTER_GAUSSIAN_BLUR:
imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊图像
break;
} if(isset($_POST['emb_caption']))
{
imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption);
} $thumb_width = $width * 0.10;
$thumb_height = $height * 0.10; //创建一个缩略图
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
//保存原图
imagejpeg($image, $dir . '/' . $_POST['id'] . '.jpg', 100);
//保存缩略图
imagejpeg($thumb, $thumbdir . '/' . $_POST['id'] . '.jpg', 100);
imagedestroy($thumb);
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>Your image has been saved!</h1>
<img src="data:images/<?php echo $_POST['id'];?>.jpg" alt="" />
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1>
<p>Here is the picture you just uploaded to your servers:</p>
<!--<img src="data:images/<?php echo $imagename;?>" alt="" style="float:left;" />-->
</body>
</html>
<?php
if($_POST['submit'] == 'Upload')
{
$imagename = 'images/'.$image_id.'.jpg';
}
else
{
$imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect'];
if(isset($_POST['emb_caption']))
{
$imagename .= '&capt='.urlencode($image_caption);
}
}
?>
<img src="<?php echo $imagename;?>" style="float:left;" alt="" />
<table>
<tr>
<td>Image save as:</td>
<td><?php $image_id?></td>
</tr>
<tr>
<td>Height:</td>
<td><?php echo $height;?></td>
</tr>
<tr>
<td>Widht:</td>
<td><?php echo $width;?></td>
</tr>
<tr>
<td>Upload date:</td>
<td><?php echo $image_date;?></td>
</tr>
</table>
<p>You may apply a special effect to your image from the list of option below.
Note:saving an image with any of the filters applied <em>can be undone</em>
</p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $image_id;?>"/>
Filter:<select name="effect" id="">
<option value="-1">None</option>
<?php
echo '<option value="'.IMG_FILTER_GRAYSCALE.'" ';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GRAYSCALE)
{
echo 'selected="selected"';
}
echo ' >Black and white</option>'; echo '<option value="'.IMG_FILTER_GAUSSIAN_BLUR.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GAUSSIAN_BLUR)
{
echo ' selected="selected"';
}
echo '>Blur</option>'; echo '<option value="'.IMG_FILTER_EMBOSS.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_EMBOSS)
{
echo 'selected="selected"';
}
echo '>Emboss</option>'; echo '<option value="'.IMG_FILTER_NEGATE.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_NEGATE)
{
echo 'selected="selected"';
}
echo '>Negative</option>';
?>
</select><br />
<?php
echo '<input type="checkbox" name="emb_caption"';
if(isset($_POST['emb_caption']))
{
echo ' checked="checked"';
}
echo ' />Embed caption in image?';
?>
<input type="submit" value="Preview" name="submit" /><br /><br />
<input type="submit" value="Save" name="submit" /> </div>
</form>
<?php
}
?>

缩略图放在D:\Serious\phpdev\test\images\thumbs路径下面,这里我们添加了一个gallery.php文件来陈列所有的缩略图,代码如下:

<?php
$db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to database');
mysql_select_db('moviesite',$db) or die(mysql_error($db)); $dir = 'images';
$thumbdir = $dir.'/thumbs';
?>
<html>
<head>
<title>Welcome to our Photo Gallery</title>
<style type="text/css">
th{ background-color:#999; }
.odd_row { background-color:#EEE;}
.even_row {background-color:#FFF;}
</style>
</head>
<body>
<p>Click on any image to see it full sized.</p>
<table style="width:100%">
<tr>
<th>Image</th>
<th>Caption</th>
<th>Uploaded By</th>
<th>Date uploaded</th>
</tr>
<?php
$result = mysql_query('select * from images') or die(mysql_error($db));
$odd = true;
while($row = mysql_fetch_assoc($result))
{
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
extract($row);
echo '<td><a href="' . $dir . '/' . $image_id.'.jpg">';
echo '<img src="' . $thumbdir . '/' . $image_id . '.jpg" alt="" />';
echo '</a></td>';
echo '<td>'.$image_caption.'</td>';
echo '<td>'.$image_username.'</td>';
echo '<td>'.$image_date.'</td>';
echo '</tr>';
} ?>
</table>
</body>
</html>

来看看最后显示缩略图的效果:

注意下面的代码:

    $thumb_width = $width * 0.10;
$thumb_height = $height * 0.10; //创建一个缩略图
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
//保存原图
imagejpeg($image, $dir . '/' . $_POST['id'] . '.jpg', 100);
//保存缩略图
imagejpeg($thumb, $thumbdir . '/' . $_POST['id'] . '.jpg', 100);
imagedestroy($thumb);

我们设置缩略图的长度是原来长度的10%,宽度也是原来宽度的10%,注意保持一致,否则图片会出现失真,当然这个参数如果设置成大于1的整数,图片就会放大。

PHP根据图片制作缩略图的更多相关文章

  1. tp中附件上传文件,表单提交

    public function tianjia(){ $goods=D('Goods'); if(!empty($_POST)){ if($_FILES['f_goods_image']['error ...

  2. PHP-生成缩略图和添加水印图-学习笔记

    1.开始 在网站上传图片过程,经常用到缩略图功能.这里我自己写了一个图片处理的Image类,能生成缩略图,并且可以添加水印图. 2.如何生成缩略图 生成缩略图,关键的是如何计算缩放比率. 这里,我根据 ...

  3. 最新 去掉 Chrome 新标签页的8个缩略图

    chrome的新标签页的8个缩略图实在让人不爽,网上找了一些去掉这个略缩图的方法,其中很多已经失效.不过其中一个插件虽然按照原来的方法已经不能用了,但是稍微变通一下仍然是可以用的(本方法于2017.1 ...

  4. 纯JS打造比QQ空间更强大的图片浏览器-支持拖拽、缩放、过滤、缩略图等

    在线演示地址(打开网页后,点击商家图册): http://www.sport7.cn/cc/jiangnan/football5.html 先看一看效果图: 该图片浏览器实现的功能如下: 1. 鼠标滚 ...

  5. C# webform上传图片并生成缩略图

    其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好! void UpImgs() { if (FileUpload1.HasFile) { str ...

  6. 强大的flash头像上传插件(支持旋转、拖拽、剪裁、生成缩略图等)

    今天介绍的这款flash上传头像功能非常强大,支持php,asp,jsp,asp.net 调用 头像剪裁,预览组件插件. 本组件需要安装Flash Player后才可使用,请从http://dl.pc ...

  7. 获取文件的缩略图Thumbnail和通过 AQS - Advanced Query Syntax 搜索本地文件

    演示如何获取文件的缩略图 FileSystem/ThumbnailAccess.xaml <Page x:Class="XamlDemo.FileSystem.ThumbnailAcc ...

  8. 帝国cms内容页调用缩略图的原始尺寸图片

    在发布文章上传标题图片时,勾选"生成缩略图",将生成原图和对应的缩略图 原图的链接为[!--titlepic--]:/d/file/anlizhanshi/2016-11-25/8 ...

  9. VS2013开启滚动条缩略图和双击选中高亮,效果杠杠滴!

    1.双击代码或选中代码高亮,用以下插件,反应很灵敏,我安装的是第三个 2.代码编辑器的滚动条缩略图是VS自带的,需要打开菜单----工具----选项,如下图设置: 3.VS默认的选中颜色,需要打开菜单 ...

随机推荐

  1. 1-1、create-react-app 配置 mobx

    1.用npx create-react-app my-app安装项目 2.cd my-app 3.执行 npm  run eject  让配置文件可见 4.npm install --saveDev ...

  2. git删除远程主机没有的tag

    可以先删除所有本地tag,然后再拉取远程上的tag git tag -l | xargs git tag -d git fetch --tags 其他方法以及查询tag的命令请见:Remove loc ...

  3. FocusBI: SSIS 开发案例(原创)

    关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...

  4. mongo小记

    进mongo mongo 先添加admin表的账号密码 . use admin . db.createUser( { user: "admin", pwd: "admin ...

  5. 【c++】友元

    c++引入友元的原因 在某些情况下,允许特定的非成员函数访问类的私有成员.在类中以关键字friend开始(只能出现在类定义的内部),声明为友元的可以为类.类的成员函数.普通的非成员函数. 速览 #in ...

  6. C语言经典面试题目(转的,不过写的的确好!)

    第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...

  7. ASP.NET MVC Core的TagHelper(基础篇)

    TagHelper又是一个新的名词,它替代了自之前MVC版本的HtmlHelper,专注于在cshmlt中辅助生成html标记. 通过使用自定义的TagHelper可以提供自定义的Html属性或元素, ...

  8. 用户IP地址的三个属性的区别(HTTP_X_FORWARDED_FOR,HTTP_VIA,REM_addr)

    一.没有使用代理服务器的情况: REMOTE_ADDR = 您的 IP      HTTP_VIA = 没数值或不显示      HTTP_X_FORWARDED_FOR = 没数值或不显示 二.使用 ...

  9. MySQL---7、常用操作

    1.表列的添加.修改和删除 2.视图 3.字符集和校对集 4.触发器相关知识

  10. 单源最短路(Dijkstra算法)

    #返回上一级 @Author: 张海拔 @Update: 2015-03-11 @Link: http://www.cnblogs.com/zhanghaiba/p/3514570.html Dijk ...