GD库是PHP进行图象操作一个很强大的库。
先在php.ini里增加一行引用:extension=php_gd2.dll
重启apache。做一个测试页 var_dump(gd_info());输出数据表明GD库引用成功。
图片上传页面 upload.html
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>图片上传</title>
</head>
<body>
<h1>文件上传(只允许上传jpg类型图片)</h1>
<form enctype="multipart/form-data" action="upload_img.php" method="post">
<input name="upfile" type="file"><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>图片上传</title>
</head>
<body>
<h1>文件上传(只允许上传jpg类型图片)</h1>
<form enctype="multipart/form-data" action="upload_img.php" method="post">
<input name="upfile" type="file"><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
处理页面upload_img.php
<?php
//上传图片保存地址
$uploadfile = "upfiles/".$_FILES['upfile']['name'];
//缩略图保存地址
$smallfile = "upfiles/small_".$_FILES['upfile']['name'];
if($_FILES['upfile']['type'] != "image/jpeg")
{
echo '文件类型错误';
}
else
{
move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile); //上传文件
$dstW=200;//缩略图宽
$dstH=200;//缩略图高
$src_image=ImageCreateFromJPEG($uploadfile);
$srcW=ImageSX($src_image); //获得图片宽
$srcH=ImageSY($src_image); //获得图片高
$dst_image=ImageCreateTrueColor($dstW,$dstH);
ImageCopyResized($dst_image,$src_image,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
ImageJpeg($dst_image,$smallfile);
echo '文件上传成功<br>';
echo "<img src='$smallfile' />";
}
?>
//上传图片保存地址
$uploadfile = "upfiles/".$_FILES['upfile']['name'];
//缩略图保存地址
$smallfile = "upfiles/small_".$_FILES['upfile']['name'];
if($_FILES['upfile']['type'] != "image/jpeg")
{
echo '文件类型错误';
}
else
{
move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile); //上传文件
$dstW=200;//缩略图宽
$dstH=200;//缩略图高
$src_image=ImageCreateFromJPEG($uploadfile);
$srcW=ImageSX($src_image); //获得图片宽
$srcH=ImageSY($src_image); //获得图片高
$dst_image=ImageCreateTrueColor($dstW,$dstH);
ImageCopyResized($dst_image,$src_image,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
ImageJpeg($dst_image,$smallfile);
echo '文件上传成功<br>';
echo "<img src='$smallfile' />";
}
?>