2012-08-11 103 views
2

我從網上獲得了這個腳本,並改變了一些東西來滿足我的需求,但無論我做什麼,我都無法將鏈接放到我的數據庫中。上傳並調整您的圖片鏈接到mysql

<html> 
    <body> 
    <h2>Upload and Resize Your Image</h2> 
    <?php 
    // index.php 
    function generate_resized_image(){ 
     $max_dimension = 800; // Max new width or height, can not exceed this value. 
     $dir = "../../upload123/"; // Directory to save resized image. (Include a trailing slash - /) 
     // Collect the post variables. 
     $postvars = array(
      "image" => trim($_FILES["image"]["name"]), 
      "image_tmp" => $_FILES["image"]["tmp_name"], 
      "image_size" => (int)$_FILES["image"]["size"], 
      "image_max_width" => (int)$_POST["image_max_width"], 
      "image_max_height" => (int)$_POST["image_max_height"] 
     ); 
     // Array of valid extensions. 
     $valid_exts = array("jpg","jpeg","gif","png"); 
     // Select the extension from the file. 
     $ext = end(explode(".",strtolower(trim($_FILES["image"]["name"])))); 
     // Check not larger than 175kb. 
     if($postvars["image_size"] <= 1048576){ 
      // Check is valid extension. 
      if(in_array($ext,$valid_exts)){ 
       if($ext == "jpg" || $ext == "jpeg"){ 
        $image = imagecreatefromjpeg($postvars["image_tmp"]); 
       } 
       else if($ext == "gif"){ 
        $image = imagecreatefromgif($postvars["image_tmp"]); 
       } 
       else if($ext == "png"){ 
        $image = imagecreatefrompng($postvars["image_tmp"]); 
       } 
       // Grab the width and height of the image. 
       list($width,$height) = getimagesize($postvars["image_tmp"]); 
       // If the max width input is greater than max height we base the new image off of that, otherwise we 
       // use the max height input. 
       // We get the other dimension by multiplying the quotient of the new width or height divided by 
       // the old width or height. 
       if($postvars["image_max_width"] > $postvars["image_max_height"]){ 
        if($postvars["image_max_width"] > $max_dimension){ 
         $newwidth = $max_dimension; 
        } else { 
         $newwidth = $postvars["image_max_width"]; 
        } 
        $newheight = ($newwidth/$width) * $height; 
       } else { 
        if($postvars["image_max_height"] > $max_dimension){ 
         $newheight = $max_dimension; 
        } else { 
         $newheight = $postvars["image_max_height"]; 
        } 
        $newwidth = ($newheight/$height) * $width; 
       } 
       // Create temporary image file. 
       $tmp = imagecreatetruecolor($newwidth,$newheight); 
       // Copy the image to one with the new width and height. 
       imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height); 
       // Create random 4 digit number for filename. 
       $rand = rand(1000,9999); 
       $filename = $dir.$rand.$postvars["image"]; 
       // Create image file with 100% quality. 
       imagejpeg($tmp,$filename,100); 
       return "<strong>Image Preview:</strong><br/> 
        <img src=\"".$filename."\" border=\"0\" title=\"Resized Image Preview\" style=\"padding: 4px 0px 4px 0px;background-color:#e0e0e0\" /><br/> 
        Resized image successfully generated. <a href=\"".$filename."\" target=\"_blank\" name=\"Download your resized image now!\">Click here to download your image.</a>"; 
       imagedestroy($image); 
       imagedestroy($tmp); 
      } else { 
       return "File size too large. Max allowed file size is 175kb."; 
      } 
     } else { 
      return "Invalid file type. You must upload an image file. (jpg, jpeg, gif, png)."; 
     } 
    } 
    if(isset($_GET["do"])){ 
     if($_GET["do"] == "upload"){ 
      $upload_and_resize = generate_resized_image(); 
     } else { 
     $upload_and_resize = ""; 
    } 
    } else { 
     $upload_and_resize = ""; 
    } 
    ?> 

       <form action="./upload.php?do=upload" method="post" enctype="multipart/form-data"> 
        <table width="100%" align="center" border="0" cellpadding="2" cellspacing="0"> 
        <tr> 
         <td align="left" width="100">Max Width:</td> 
         <input name="image_max_width" type="hidden" id="hiddenField" value="400" /> 
        </tr> 
        <tr> 
         <td align="left">Image:</td> 
         <td align="left"><input type="file" name="image" size="40" /></td> 
        </tr> 
        <tr> 
         <td align="left" colspan="2"> 
          <ol style="margin:0;padding:3px 0px 3px 15px"> 
           <li>Max file size: 175 KB.</li> 
           <li>Allowed extensions: jpg, jpeg, gif, png.</li> 
           <li>Max Dimension: <em>800</em>px.</li> 
          </ol> 
        </tr> 
        <tr> 
         <td align="left" colspan="2"> 
          <input type="submit" name="submit" value="Submit!" class="submit" /> 
         </td> 
        </tr> 
       </table> 
      </form> 

      <div id="upload"> 
       <?php echo $upload_and_resize; ?> 
      </div> 
     </body> 
    </html> 

於是,我嘗試過各種職務,但沒有幫助,這是我需要把我的數據庫是簡單的表1 ID 2 - 煤泥(煤泥是圖像鏈接進入)

$filenamex=$filename; 
    $con = mysql_connect("localhost","USER","PASS"); 
    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("myDB", $con); 

    $sql="INSERT INTO acc1 (slime) 
    VALUES 
    ('$filenamex')"; 

    if (!mysql_query($sql,$con)) 
     { 
     die('Error: ' . mysql_error()); 
     } 

    mysql_close($con) 
+2

請停止使用不推薦使用的'mysql_'函數,而改用'mysqli'或'PDO'。我個人比較喜歡'PDO',因爲它的工作原理不僅僅是MySQL – 2012-08-11 20:44:42

+0

謝謝你的提示,請問你是否也有解決我的問題的方法。伍德很吝嗇! – maki001 2012-08-12 10:24:33

+0

任何人?請幫忙! – maki001 2012-08-12 10:36:35

回答

1

您需要更改generate_resized_image功能只返回文件路徑:

<?php 
    // index.php 
    function generate_resized_image(){ 
     $max_dimension = 800; // Max new width or height, can not exceed this value. 
     $dir = "../../upload123/"; // Directory to save resized image. (Include a trailing slash - /) 
     // Collect the post variables. 
     $postvars = array(
      "image" => trim($_FILES["image"]["name"]), 
      "image_tmp" => $_FILES["image"]["tmp_name"], 
      "image_size" => (int)$_FILES["image"]["size"], 
      "image_max_width" => (int)$_POST["image_max_width"], 
      "image_max_height" => (int)$_POST["image_max_height"] 
     ); 
     // Array of valid extensions. 
     $valid_exts = array("jpg","jpeg","gif","png"); 
     // Select the extension from the file. 
     $ext = end(explode(".",strtolower(trim($_FILES["image"]["name"])))); 
     // Check not larger than 175kb. 
     if($postvars["image_size"] <= 1048576){ 
      // Check is valid extension. 
      if(in_array($ext,$valid_exts)){ 
       if($ext == "jpg" || $ext == "jpeg"){ 
        $image = imagecreatefromjpeg($postvars["image_tmp"]); 
       } 
       else if($ext == "gif"){ 
        $image = imagecreatefromgif($postvars["image_tmp"]); 
       } 
       else if($ext == "png"){ 
        $image = imagecreatefrompng($postvars["image_tmp"]); 
       } 
       // Grab the width and height of the image. 
       list($width,$height) = getimagesize($postvars["image_tmp"]); 
       // If the max width input is greater than max height we base the new image off of that, otherwise we 
       // use the max height input. 
       // We get the other dimension by multiplying the quotient of the new width or height divided by 
       // the old width or height. 
       if($postvars["image_max_width"] > $postvars["image_max_height"]){ 
        if($postvars["image_max_width"] > $max_dimension){ 
         $newwidth = $max_dimension; 
        } else { 
         $newwidth = $postvars["image_max_width"]; 
        } 
        $newheight = ($newwidth/$width) * $height; 
       } else { 
        if($postvars["image_max_height"] > $max_dimension){ 
         $newheight = $max_dimension; 
        } else { 
         $newheight = $postvars["image_max_height"]; 
        } 
        $newwidth = ($newheight/$height) * $width; 
       } 
       // Create temporary image file. 
       $tmp = imagecreatetruecolor($newwidth,$newheight); 
       // Copy the image to one with the new width and height. 
       imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height); 
       // Create random 4 digit number for filename. 
       $rand = rand(1000,9999); 
       $filename = $dir.$rand.$postvars["image"]; 
       // Create image file with 100% quality. 
       imagejpeg($tmp,$filename,100); 
    //------------------------------------------------------------------------ 
    //here is the change 
       return $filename; //return the new image path 
    //------------------------------------------------------------------------ 
       imagedestroy($image); 
       imagedestroy($tmp); 
      } else { 
       return "File size too large. Max allowed file size is 175kb."; 
      } 
     } else { 
      return "Invalid file type. You must upload an image file. (jpg, jpeg, gif, png)."; 
     } 
    } 
    ?> 

然後把你的SQL代碼到一個函數是這樣的:

// you can modify this to handle errors better, this is just for example 
function saveToDb($filename) 
{ 
    $con = mysql_connect("localhost", "USER", "PASS"); 
    if (!$con) { 
     die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("myDB", $con); 

    $sql = "INSERT INTO acc1 (slime) 
    VALUES 
    ('$filename')"; 

    if (!mysql_query($sql, $con)) { 
     die('Error: ' . mysql_error()); 
    } else { 
     return true; 
    } 
    mysql_close($con); 
} 

那麼這部分代碼是,大部分的動作發生:

if(isset($_GET["do"])){ 
     if($_GET["do"] == "upload"){ 
      $uploaded = generate_resized_image();//now just returns path to new image 
      $mysql = saveToDb($uploaded); 
      if (!$mysql) { 
      //handle error 
      } 
     } else { 
     $upload_and_resize = ""; 
    } 
    } else { 
     $upload_and_resize = ""; 
    } 

現在節省完成,你必須解決這個div作爲函數不再返回的HTML代碼。

<div id="upload"> 
    <?php echo $upload_and_resize; ?> 
</div> 

祝你好運。

P.S.它可能是開始使用面向對象的PHP的時候了。

+0

好吧,實際上你應該在imagedestroy調用之後放置你的return語句,否則圖像所使用的內存將不會被釋放(至少在劇本結束之前)。 – wimvds 2012-08-12 12:02:09

+0

我傾聽你的消化,它的工作原理! – maki001 2012-08-12 14:59:03

+0

謝謝大家! – maki001 2012-08-12 14:59:32