首先来一段简单的php上传图片生成缩略图的详细代码,分享给大家供大家参考,具体内容如下
<?php
function createthumbnail($imagedirectory, $imagename, $thumbdirectory, $thumbwidth, $quality){
$details = getimagesize("$imagedirectory/$imagename") or die('please only upload images.');
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$srcimg = imagecreatefrom'.$type.'("$imagedirectory/$imagename");');
$thumbheight = $details[1] * ($thumbwidth / $details[0]);
$thumbimg = imagecreatetruecolor($thumbwidth, $thumbheight);
imagecopyresampled($thumbimg, $srcimg, 0, 0, 0, 0, $thumbwidth, $thumbheight,
$details[0], $details[1]);
eval('image'.$type.'($thumbimg, "$thumbdirectory/$imagename"'.
(($type=='jpeg')?', $quality':'').');');
imagedestroy($srcimg);
imagedestroy($thumbimg);
}
foreach ($_files["pictures"]["error"] as $key => $error) {
if ($error == upload_err_ok) {
$tmp_name = $_files["pictures"]["tmp_name"][$key];
$name = $_files["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
createthumbnail("/location/of/main/image", $name, "/location/to/store/thumb", 120, 80);
//120 = thumb width :: 80 = thumb quality (1-100)
}
}
?>
接下来再为大家推荐一个实例php使用gd库上传图片以及创建缩略图,直接看代码:
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>
处理页面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' />";
}
?>
希望对大家学习php程序设计有所帮助,谢谢大家的支持。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!