17-文件上传+生成缩略图(版本II)
上一篇博文中,我们曾经提到其局限性,今天我们就来把其修改一下吧!
第一:根据不同的文件类型来生成缩略图
我们曾经讲过,我们应该根据不同的文件类型来生成与之相对应格式的缩略图,那么就也就是说,我们必须知道源文件的文件类型才可以!那么getp_w_picpathsize函数可以完成这个功能!那么这么函数在使用时也就变成了:
$filename = "01.jpg";
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
$scalePercent
= 0.5;
list
(
$srcWidth
,
$srcHeight
,
$fileType
)
= getp_w_picpathsize($filename);
另外还有两个问题,
一是:将读取源文件资源时应该使用p_w_picpathcreategif、p_w_picpathcreatejpeg、p_w_picpathpng确定了
二是:输出图像时使用p_w_picpathgif、p_w_picpathjpeg、p_w_picpathpng也确定了
好了,可以干活吧!
<?php
$filename
= "01.jpg";
$scalePercent
= 0.5;
list
(
$srcWidth
,
$srcHeight
,
$fileType
)
= getp_w_picpathsize($filename);
$destWidth
= ceil($srcWidth * $scalePercent);
$destHeight
= ceil($srcHeight * $scalePercent);
$destImage
= p_w_picpathcreatetruecolor($destWidth,$destHeight);
if
(
$fileType
== 1)
{
$createImage = "p_w_picpathcreatefromgif";
$outImage = "p_w_picpathgif";
}
elseif
($fileType == 2)
{
$createImage = "p_w_picpathcreatefromjpeg";
$outImage = "p_w_picpathjpeg";
}
elseif
($fileType == 3)
{
$createImage = "p_w_picpathcreatefrompng";
$outImage = "p_w_picpathpng";
}
$srcImage
= $createImage($fil
ename);
p_w_picpathcopyresampled(
$destImage
,
$srcImage
,
0
,
0
,
0
,
0
,
$destWidth
,
$destHeight
,
$srcWidth
,
$srcHeight
);
$outImage
(
$destImage
,
"./small_
{
$filename
}
"
);
p_w_picpathdestroy(
$destImage
);
p_w_picpathdestroy(
$srcImage
);
具体的代码在附件一
第二:根据文件上传的文件生成缩略图
在我们实现的不同类型的图像文件生成缩略图后,我们再来把整个过程升级!---将上传文件生成缩略图!
我们先来实现一个简单的!
首先,我们来制作一个简单的GUI,如下图
其源代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
<link href="style/common.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div id="container">
<form action="uploadFile.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="120">选择要上传的文件:</td>
<td><input name="uploadFile" type="file" id="uploadFile" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="submit" id="submit" value="上传图片" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
uploadFile.php
<?php
$filename
= $_FILES["uploadFile"]["name"];
$tmpName
= $_FILES["uploadFile"]["tmp_name"];
$errorCode
= $_FILES["uploadFile"]["error"];
if
(
$errorCode
== 0)
{
$scalePercent = 0.5;
list($srcWidth,$srcHeight,$fileType) = getp_w_picpathsize($tmpName);
$destWidth = ceil($srcWidth * $scalePercent);
$destHeight = ceil($srcHeight * $scalePercent);
$destImage = p_w_picpathcreatetruecolor($destWidth,$destHeight);
if($fileType == 1)
{
$createImage = "p_w_picpathcreatefromgif";
$outImage = "p_w_picpathgif";
}
elseif ($fileType == 2)
{
$createImage = "p_w_picpathcreatefromjpeg";
$outImage = "p_w_picpathjpeg";
}
elseif ($fileType == 3)
{
$createImage = "p_w_picpathcreatefrompng";
$outImage = "p_w_picpathpng";
}
$srcImage = $createImage($tmpName);
p_w_picpathcopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight);
$outImage($destImage,"./small_{$filename}");
p_w_picpathdestroy($destImage);
p_w_picpathdestroy($srcImage);
}
else
{
echo("
文件上传失败!
"
);
}
?>
当然,我没有判断文件的类型,请大家参照以前的博文就可以了!
明天,我们再将今天的功能进行升级-----添加水印效果!
晚安,各位!!!!
转载于:https://blog.51cto.com/pangdou/240442