PHP根据图片制作缩略图

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="images/<?php echo $_POST[‘id‘];?>.jpg" />
</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="images/<?php echo $imagename;?>" 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;" />
<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" />‘;
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的整数,图片就会放大。

时间: 2024-10-28 09:28:59

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

JS获取图片的缩略图,并且动态的加载多张图片

找了好多资料也没有找到该死的ie的解决办法,最后放弃了ie <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js获取缩略图</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <st

nginx实时生成缩略图到硬盘上

原文:http://www.ttlsa.com/nginx/nginx-create-image-on-disk/ 现在随着各终端的出现(手机,ipad等平板),以及各种终端的手机分辨率和尺寸都不同,现在手机用户流量都是宝,网上出现了各种各样的生成缩略图功能的架构,有使用php实时生成缩略图的,也有用nginx + lua实现的,上节我也讲到了使用nginx生成缩略图,但是用户每次访问都需要生成一次,会给cpu和硬盘带来比较大的压力,今天带来了另外一种方式,这次使用nginx将原图生成缩略图到硬

VB.NET-QQ新闻弹窗样式图片制作工具

〇.下载地址 本程序的下载地址(百度网盘):http://pan.baidu.com/s/1qWBGGGG 一.关于本程序 Gnaea是一个QQ新闻弹窗的填字工具,可以在输入新闻标题和新闻内容后生成一张类似QQ新闻弹窗的图片.生成的图片可以被保存为BMP和PNG两种格式,或是直接复制到剪贴板. 效果如下图(注:内容纯属虚构) 二.程序控件 三.程序资源 被用作素材的资源:My.Resources.PopUp,取材于一张PNG格式的图片 四.程序代码 Imports System.Text Pub

php验证码+缩略图+饼状图+五环图

@1.验证码 1 captcher.php 2 header('Content-type:image/png'); 3 session_start(); 4 $img = imagecreate(100, 30); 5 $captcha = array( 6 'a', 'b', 'c', 'd', 'e', 'f', 7 'g', 'h', 'i', 'j', 'k', 'l', 8 'm', 'n', 'o', 'p', 'q', 'r', 9 's', 't', 'u', 'v', 'w',

最蛋疼的bug:读取图片缩略图(一定要在相册查看下形成缓存)

最近的一个连接服务端的应用,需要读取图片,一般供用户发布商品选择上传图片,初始的图片列表应该是缩略图,只有确定了,才上传原图,OK不多说上代码 package edu.buaa.erhuo; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import

生成缩略图时报GDI+中发生一般性错误

最近由于业务需要要写一个生成缩略图并能设置图片质量的功能,本来这是一件so easy的事,以前也干过,可是却遇到了问题.话不多说先看代码 /// <summary> /// 等比生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理

dedecms有缩略图则显示缩略图,没有则显示随机缩略图

随着html5以及扁平化等新的设计概念的深入人心,缩略图功能则成了一般网页模版制作不可或缺的一个功能,dedecms默认的的缩略图调用标签[field:imglink/] 或者 [field:litpic/],在文章有缩略图的时候会调用缩略图,没有的时候就调用默认图片defaultpic.gif,但是会有各种意外的情况导致文章内没有配图,只是显示默认的图片的话,页面美观上就得不到保证,前面还有人分享过一篇通过js来实现随机缩略图的,具体使用起来的话,还是觉得有些鸡肋,今天的话给大家分享一个新的解

dede 添加 栏目缩略图

一. 涉及到文件如下(注意备份) dede/catalog_add.php dede/catalog_edit.php dede/templets/catalog_add.htm dede/templets/catalog_edit.htm 二.打开文件夹/templets目录(模板),在里面新建一个文件夹typeimg,用于独立存放栏目缩略图 三. 新加字段 typeimg 后台执行SQL(前缀默认为dede_ 具体前缀根据自己网站修改): alter table `dede_arctype`

nginx利用image_filter动态生成缩略图

原文:http://www.open-open.com/lib/view/open1416193847945.html "我现在是有些图片需要生成缩略图,这个现在加了image_filter这个已经实现了,但我不知道怎么样才能访问我上传的原图" 刚开始觉得也不太好弄,让他用程序区处理,实际上稍微动脑筋分析一下也可以不修改程序实现动态生成缩略图且能够访问原图. 前提是需要定好图片的访问规则. 先来看一下什么是nginx的image filter模块. HttpImageFilterMod