本文实例讲述了php使用gd库创建图片缩略图的方法。分享给大家供大家参考。具体分析如下:
上传页面的静态html代码:
<html> <head> <title>文件上传</title> <meta http-equiv="content-type" content="text/html; charset=gb2312"> </head> <h1>文件上传</h1> <form enctype="multipart/form-data" action="upload.php" method="post"> <input name="upfile" type="file"><br> <input type="submit" value="submit"> </form> <body> </body> </html>
相应的upload.php文件代码:
<?php
$uploadfile = "upfiles/".$_files['upfile']['name'];
//上传后文件所在的文件名和路径
$smallfile = "upfiles/small_".$_files['upfile']['name'];
//上传后缩略图文件所在的文件名和路径
if($_files['upfile']['type'] != "image/pjpeg")
{
echo "文件类型错误"; //输出错误信息
}
else
{
move_uploaded_file($_files['upfile']['tmp_name'], $uploadfile);
//上传文件
$dstw = 200; //设定缩略图的宽度
$dsth = 200; //设定缩略图的高度
$src_image = imagecreatefromjpeg($uploadfile);
//读取jpeg文件并创建图像对象
$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" mce_src="$smallfile"></img>";
//在页面上显示缩略图
}
?>
希望本文所述对大家的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!