代码1:
<html>
<head>
<title>Upload your pic to our site!</title>
<style type="text/css">
<!--
td {vertical-align:top;}
-->
</style>
</head>
<body>
<form action="check_image.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Your Username</td>
<td><input type="text" name="username"/></td>
</tr><tr>
<td>Upload images*</td>
<td><input type="file" name='uploadfile'/></td>
</tr><tr>
<td colspan="2">
<small><em>*Acceptable image formats include:GIF,JPG/JPEG and PNG.</em></small>
</td>
</tr><tr>
<td>Image caption</br>
</td>
<td><input type="text" name="caption"/></td>
</tr><tr>
<td colspan="2" style="text-align:center">
<input type="submit" name="submit" value="upload"/>
</td>
</tr>
</table>
</form>
</body>
</html>代码2:
<?php
$db=mysql_connect('localhost','hihi','huluhulu') or die ('unable to connect.Check your connection parameters.');
mysql_select_db('moviesite',$db) or die(mysql_error($db)); $dir='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
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 in php.ini.');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The uploaded 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 failed to write the uploaded file to disk.');
break;
case UPLOAD_ERR_EXTENSION:
die('File 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 uploaded was 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:
die($error);
}
$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)); $image_id=mysql_insert_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;
}
imagejpeg($image,$dir.'/'.$_POST['id'].'.jpg',100);
imagedestroy($image);
?>
<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 to be famous?</h1>
<p>Here is the picture you just uploaded to our servers:</p>
<?php
if ($_POST['submit']=='upload')
{ $imagename='images/'. $image_id .'.jpg';}
else {$imagename='image_effect.php?id='. $image_id .'&e='.$_POST['effect'];}
?> <img src="<?php echo $imagename; ?>" style="float:left;">
<table>
<tr><td>Image Saved as:</td><td><?php echo $image_id . '.jpg'; ?></td></tr>
<tr><td>Height:</td><td><?php echo $height; ?></td></tr>
<tr><td>Width:</td><td><?php echo $width; ?></td></tr>
<tr><td>Upload Date:</td><td><?php echo $image_date; ?></td></tr>
</table>
<p>You may apply special options to your image below.Note:saving an image with any of the options applied.
<em>Can't 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">
<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_GRUSSIAN_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>
<input type="submit" value="Preview" name="submit" />
<br/><br/>
<input type="submit" value="Save" name="submit" />
</div>
</form>
</body>
</html>
<?php
}
?>代码3:
<?php
$dir='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
if (isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir . '/' . $_GET['id'] . '.jpg'))
{$image=imagecreatefromjpeg($dir . '/' .$GET['id'] . '.jpg');}
else { die('invalid image specified');} $effect =(isset($_GET['e'])) ? $_GET['e'] : -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;
} header('Content-Type:image/jpeg');
imagejpeg($image,'',100);
?>功能基本都正常,除了preview的时候图片不能打开,如何解决?请高手指教!
<html>
<head>
<title>Upload your pic to our site!</title>
<style type="text/css">
<!--
td {vertical-align:top;}
-->
</style>
</head>
<body>
<form action="check_image.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Your Username</td>
<td><input type="text" name="username"/></td>
</tr><tr>
<td>Upload images*</td>
<td><input type="file" name='uploadfile'/></td>
</tr><tr>
<td colspan="2">
<small><em>*Acceptable image formats include:GIF,JPG/JPEG and PNG.</em></small>
</td>
</tr><tr>
<td>Image caption</br>
</td>
<td><input type="text" name="caption"/></td>
</tr><tr>
<td colspan="2" style="text-align:center">
<input type="submit" name="submit" value="upload"/>
</td>
</tr>
</table>
</form>
</body>
</html>代码2:
<?php
$db=mysql_connect('localhost','hihi','huluhulu') or die ('unable to connect.Check your connection parameters.');
mysql_select_db('moviesite',$db) or die(mysql_error($db)); $dir='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
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 in php.ini.');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The uploaded 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 failed to write the uploaded file to disk.');
break;
case UPLOAD_ERR_EXTENSION:
die('File 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 uploaded was 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:
die($error);
}
$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)); $image_id=mysql_insert_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;
}
imagejpeg($image,$dir.'/'.$_POST['id'].'.jpg',100);
imagedestroy($image);
?>
<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 to be famous?</h1>
<p>Here is the picture you just uploaded to our servers:</p>
<?php
if ($_POST['submit']=='upload')
{ $imagename='images/'. $image_id .'.jpg';}
else {$imagename='image_effect.php?id='. $image_id .'&e='.$_POST['effect'];}
?> <img src="<?php echo $imagename; ?>" style="float:left;">
<table>
<tr><td>Image Saved as:</td><td><?php echo $image_id . '.jpg'; ?></td></tr>
<tr><td>Height:</td><td><?php echo $height; ?></td></tr>
<tr><td>Width:</td><td><?php echo $width; ?></td></tr>
<tr><td>Upload Date:</td><td><?php echo $image_date; ?></td></tr>
</table>
<p>You may apply special options to your image below.Note:saving an image with any of the options applied.
<em>Can't 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">
<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_GRUSSIAN_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>
<input type="submit" value="Preview" name="submit" />
<br/><br/>
<input type="submit" value="Save" name="submit" />
</div>
</form>
</body>
</html>
<?php
}
?>代码3:
<?php
$dir='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
if (isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir . '/' . $_GET['id'] . '.jpg'))
{$image=imagecreatefromjpeg($dir . '/' .$GET['id'] . '.jpg');}
else { die('invalid image specified');} $effect =(isset($_GET['e'])) ? $_GET['e'] : -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;
} header('Content-Type:image/jpeg');
imagejpeg($image,'',100);
?>功能基本都正常,除了preview的时候图片不能打开,如何解决?请高手指教!
建议看一下错误提示,另外多看手册中的GD函数说明,这样有利于帮助你完成这个功能:Image Processing and GD
简介
安装/配置
需求
安装
运行时配置
资源类型
预定义常量
范例
PNG creation with PHP
Adding waters to images using alpha channels
Using imagecopymerge to create a translucent water
GD and Image 函数
gd_info — 取得当前安装的 GD 库的信息
getimagesize — 取得图像大小
image_type_to_extension — 取得图像类型的文件后缀
image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型
image2wbmp — 以 WBMP 格式将图像输出到浏览器或文件
imagealphablending — 设定图像的混色模式
imageantialias — 是否使用抗锯齿(antialias)功能
imagearc — 画椭圆弧
imagechar — 水平地画一个字符
imagecharup — 垂直地画一个字符
imagecolorallocate — 为一幅图像分配颜色
imagecolorallocatealpha — 为一幅图像分配颜色 + alpha
imagecolorat — 取得某像素的颜色索引值
imagecolorclosest — 取得与指定的颜色最接近的颜色的索引值
imagecolorclosestalpha — 取得与指定的颜色加透明度最接近的颜色
imagecolorclosesthwb — 取得与给定颜色最接近的色度的黑白色的索引
imagecolordeallocate — 取消图像颜色的分配
imagecolorexact — 取得指定颜色的索引值
imagecolorexactalpha — 取得指定的颜色加透明度的索引值
imagecolormatch — 使一个图像中调色板版本的颜色与真彩色版本更能匹配
imagecolorresolve — 取得指定颜色的索引值或有可能得到的最接近的替代值
imagecolorresolvealpha — 取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值
imagecolorset — 给指定调色板索引设定颜色
imagecolorsforindex — 取得某索引的颜色
imagecolorstotal — 取得一幅图像的调色板中颜色的数目
imagecolortransparent — 将某个颜色定义为透明色
imageconvolution — 用系数 div 和 offset 申请一个 3x3 的卷积矩阵
imagecopy — 拷贝图像的一部分
imagecopymerge — 拷贝并合并图像的一部分
imagecopymergegray — 用灰度拷贝并合并图像的一部分
imagecopyresampled — 重采样拷贝部分图像并调整大小
imagecopyresized — 拷贝部分图像并调整大小
imagecreate — 新建一个基于调色板的图像
imagecreatefromgd2 — 从 GD2 文件或 URL 新建一图像
imagecreatefromgd2part — 从给定的 GD2 文件或 URL 中的部分新建一图像
imagecreatefromgd — 从 GD 文件或 URL 新建一图像
imagecreatefromgif — 从 GIF 文件或 URL 新建一图像
imagecreatefromjpeg — 从 JPEG 文件或 URL 新建一图像
imagecreatefrompng — 从 PNG 文件或 URL 新建一图像
imagecreatefromstring — 从字符串中的图像流新建一图像
imagecreatefromwbmp — 从 WBMP 文件或 URL 新建一图像
imagecreatefromxbm — 从 XBM 文件或 URL 新建一图像
imagecreatefromxpm — 从 XPM 文件或 URL 新建一图像
imagecreatetruecolor — 新建一个真彩色图像
imagedashedline — 画一虚线
imagedestroy — 销毁一图像
imageellipse — 画一个椭圆
imagefill — 区域填充
imagefilledarc — 画一椭圆弧且填充
imagefilledellipse — 画一椭圆并填充
imagefilledpolygon — 画一多边形并填充
imagefilledrectangle — 画一矩形并填充
imagefilltoborder — 区域填充到指定颜色的边界为止
imagefilter — 对图像使用过滤器
imagefontheight — 取得字体高度
imagefontwidth — 取得字体宽度
imageftbbox — 给出一个使用 FreeType 2 字体的文本框
imagefttext — 使用 FreeType 2 字体将文本写入图像
imagegammacorrect — 对 GD 图像应用 gamma 修正
imagegd2 — 将 GD2 图像输出到浏览器或文件
imagegd — 将 GD 图像输出到浏览器或文件
imagegif — 以 GIF 格式将图像输出到浏览器或文件
imagegrabscreen — Captures the whole screen
imagegrabwindow — Captures a window
imageinterlace — 激活或禁止隔行扫描
imageistruecolor — 检查图像是否为真彩色图像
imagejpeg — 以 JPEG 格式将图像输出到浏览器或文件
imagelayereffect — 设定 alpha 混色标志以使用绑定的 libgd 分层效果
imageline — 画一条线段
imageloadfont — 载入一新字体
imagepalettecopy — 将调色板从一幅图像拷贝到另一幅
imagepng — 以 PNG 格式将图像输出到浏览器或文件
imagepolygon — 画一个多边形
imagepsbbox — 给出一个使用 PostScript Type1 字体的文本方框
imagepsencodefont — 改变字体中的字符编码矢量
imagepsextendfont — 扩充或精简字体
imagepsfreefont — 释放一个 PostScript Type 1 字体所占用的内存
imagepsloadfont — 从文件中加载一个 PostScript Type 1 字体
imagepsslantfont — 倾斜某字体
imagepstext — 用 PostScript Type1 字体把文本字符串画在图像上
imagerectangle — 画一个矩形
imagerotate — 用给定角度旋转图像
imagesavealpha — 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)
imagesetbrush — 设定画线用的画笔图像
imagesetpixel — 画一个单一像素
imagesetstyle — 设定画线的风格
imagesetthickness — 设定画线的宽度
imagesettile — 设定用于填充的贴图
imagestring — 水平地画一行字符串
imagestringup — 垂直地画一行字符串
imagesx — 取得图像宽度
imagesy — 取得图像高度
imagetruecolortopalette — 将真彩色图像转换为调色板图像
imagettfbbox — 取得使用 TrueType 字体的文本的范围
imagettftext — 用 TrueType 字体向图像写入文本
imagetypes — 返回当前 PHP 版本所支持的图像类型
imagewbmp — 以 WBMP 格式将图像输出到浏览器或文件
imagexbm — 将 XBM 图像输出到浏览器或文件
iptcembed — 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中
iptcparse — 将二进制 IPTC http://www.iptc.org/ 块解析为单个标记
jpeg2wbmp — 将 JPEG 图像文件转换为 WBMP 图像文件
png2wbmp — 将 PNG 图像文件转换为 WBMP 图像文件
如果能确认图片生成成功,检查一下头输出信息,header("Content-type: image/png");或者是header("Content-type: image/jpeg");header("Content-type: image/gif");