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(" 文件上傳失敗! " );
 
}
 
?>
 
 

  固然,我沒有判斷文件的類型,請你們參照之前的博文就能夠了!

明天,咱們再將今天的功能進行升級-----添加水印效果!
晚安,各位!!!!
相關文章
相關標籤/搜索