2011-06-19 27 views
1

請給我提醒:我想整合圖片上傳到我的註冊過程。你知道一些插件或腳本爲此目的(上傳,調整大小,把鏈接到db_table)?圖片上傳整合到註冊頁面

+1

在問這個問題之前你做過什麼研究? –

+1

我沒有找到任何上傳和調整大小的東西。如果你知道一些腳本請給我一個鏈接 –

回答

2

查找下面的代碼使用GD庫上傳和裁剪圖像。

<?php 
function createThumb($upfile, $dstfile, $max_width, $max_height){ 
    $size = getimagesize($upfile); 
    $width = $size[0]; 
    $height = $size[1]; 
    $x_ratio = $max_width/$width; 
    $y_ratio = $max_height/$height; 
    if(($width <= $max_width) && ($height <= $max_height)) { 
      $tn_width = $width; 
      $tn_height = $height; 
    } elseif (($x_ratio * $height) < $max_height) { 
      $tn_height = ceil($x_ratio * $height); 
      $tn_width = $max_width; 
    } else { 
      $tn_width = ceil($y_ratio * $width); 
      $tn_height = $max_height; 
    } 
    if($size['mime'] == "image/jpeg"){ 
      $src = ImageCreateFromJpeg($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      imageinterlace($dst, true); 
      ImageJpeg($dst, $dstfile, 100); 
    } else if ($size['mime'] == "image/png"){ 
      $src = ImageCreateFrompng($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      Imagepng($dst, $dstfile); 

    } else { 

      $src = ImageCreateFromGif($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      imagegif($dst, $dstfile); 
    } 
} 

//usage 

if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') { 
    $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1); 

    $imgNormal = time().$ext; 
    $normalDestination = "Photos/Orignal/" . $imgNormal; 
    $httpRootLarge = "Photos/Large/" . $imgNormal; 
    $httpRootSmall = "Photos/Small/" . $imgNormal; 
    $httpRootThumb = "Photos/Thumb/" . $imgNormal; 
    move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination); 
    createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image 
    createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image 
    createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image 
} 
?> 
<form action="" method="post" enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="upload_Image" id="upload_Image" /> 
<br /> 
<input type="submit" name="submit" value="Submit" /> 
</form> 

在你的mysql表中保存$ imgNormal值。

+0

http://pastebin.com/Ed2YHV6w – mplungjan

1

你可以在mysql上載圖像,並使用imagemagic或gd庫調整它的大小。

+0

進入mysql表嗎?沒有。我想將它們上傳到文件夾,然後獲取圖像鏈接,並將該鏈接放入dn-table –

+0

你有用於此目的的PHP腳本嗎? –

+0

如果你想上傳到一個文件夾。您必須使用enctype =「multipart/form-data」爲文件夾設置正確的權限並使用表單上傳,並使用$ _FILE獲取訪問權限。 – NiLL

0

您可以使用move_uploaded_file to upload,但請注意,你的表單元素也必須有屬性enctype="multipart/form-data"

圖像縮放,你可以從GD庫使用的功能。快速搜索「php縮放圖像」可以揭示這個主題的許多有用鏈接。

更不用說manual itself有許多有用的示例腳本,包括你之後的一個腳本。

看來你對PHP很陌生。我建議你瞭解PHP manual。將其用作您的第一個PHP資源,然後將其作爲互聯網上的文章。在用完所有其他選項後,請到這裏。看起來你甚至沒有嘗試解決這個問題或自己研究它。