17-文件上传+生成缩略图

17-文件上传+生成缩略图(版本II)
 
上一篇博文中,咱们曾经提到其局限性,今天咱们就来把其修改一下吧!

第一:根据不一样的文件类型来生成缩略图

咱们曾经讲过,咱们应该根据不一样的文件类型来生成与之相对应格式的缩略图,那么就也就是说,咱们必须知道源文件的文件类型才能够!那么getp_w_picpathsize函数能够完成这个功能!那么这么函数在使用时也就变成了:
 
$filename = "01.jpg";
 
$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>&nbsp;</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(" 文件上传失败! " );
 
}
 
?>
 
 

  固然,我没有判断文件的类型,请你们参照之前的博文就能够了!

明天,咱们再将今天的功能进行升级-----添加水印效果!
晚安,各位!!!!
相关文章
相关标签/搜索