2011-06-06 70 views
0

這是您每天上傳並裁剪「簡介」圖片腳本/函數。有人瀏覽他們的電腦並上傳圖片。這個圖像然後被上傳到一個單獨的服務器。在此之後,同樣的圖像被提出來讓他們「裁剪」,然後保存該新版本並移除舊的大圖像。我有這一切運作良好,除了一個小細節...ftp_put():在將圖像添加到單獨的服務器時,它也將其添加到本地服務器

一旦他們保存裁剪版本,它也保存到主目錄中的本地服務器。我無法弄清楚爲什麼它會這樣做,因爲它專門設置爲僅上傳到另一個。

這裏是信息:

$upload_dir = "profile_images/". $username;   // The directory for the images to be saved in 
$upload_path = $upload_dir."/";    // The path to where the image will be saved 
$large_image_name = "resized_pic.jpg";  // New name of the large image 
$thumb_image_name = $userid ."00". $i .".jpg"; // New name of the thumbnail image 
$max_file = "200000";      // Approx 200kb 
$max_width = "500";       // Max width allowed for the large image 
$thumb_width = "80";      // Width of thumbnail image 
$thumb_height = "80";      // Height of thumbnail image 

//Image Locations 
$large_image_location = $upload_path.$large_image_name; 
$thumb_image_location = $upload_path.$thumb_image_name; 

這裏是調整裁剪圖像的功能:

function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){ 
global $ftp_server; 
global $ftp_user_name; 
global $ftp_user_pass; 
global $thumb_image_location; 
global $large_image_location; 
global $user; 
global $userid; 
global $username; 
global $success; 
global $error; 

$conn_id = ftp_connect($ftp_server); 

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
ftp_pasv($conn_id, true); 

if ($login_result) { 
    $newImageWidth = ceil($width * $scale); 
    $newImageHeight = ceil($height * $scale); 
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
    $source = imagecreatefromjpeg($image); 
    imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height); 
    imagejpeg($newImage,$thumb_image_name,90); 
    // chmod($thumb_image_name, 0777); 

    $upload = ftp_put($conn_id, $thumb_image_location, $thumb_image_name, FTP_BINARY);// upload the file 
    ftp_chmod($conn_id, 0777, $thumb_image_location); 

    if ($upload) { 
     // User DB Queries 
     $time = strtotime("now"); 
     $time = $time + 3600; 
     $date = date(c, $time); 
     $path = 'http://www.otherserver.net/'. $thumb_image_location; 

     $fileQuery = "INSERT INTO `avatars`(userid, username, path, date) VALUES('$userid', '$username', '$path', '$date')"; 
     $fileResult = mysql_query($fileQuery); 
     $userQuery = "UPDATE `users` SET avatar = '$path' WHERE username = '$user'"; 
     $userResult = mysql_query($userQuery); 

     if ($fileResult && $userResult) { 
      $success = 'Your avatar has been uploaded successfully.'; 
     } else { 
      $error = 'Something went wrong with your upload.'; 
     } 
    } 
} 
ftp_close($conn_id); // close the FTP stream 
} 

最後,這裏是當函數被調用:

if (isset($_POST["upload_thumbnail"]) && strlen($large_photo_exists) > 0) { 
    //Get the new coordinates to crop the image. 
    $x1 = $_POST["x1"]; 
    $y1 = $_POST["y1"]; 
    $x2 = $_POST["x2"]; 
    $y2 = $_POST["y2"]; 
    $w = $_POST["w"]; 
    $h = $_POST["h"]; 
    //Scale the image to the thumb_width set above 
    $scale = $thumb_width/$w; 
    resizeThumbnailImage($thumb_image_name, "http://otherserver.net/". $large_image_location,$w,$h,$x1,$y1,$scale); 

} 

再次,一切工作正常,除了它保存最終(裁剪)的圖像到本地服務器以及(是的,我t也將它保存到另一臺服務器上,這樣一部分就可以了)。

+0

全部固定。我只是不得不「解除」上傳到本地服務器的圖像。 – Nate 2011-06-07 05:20:07

回答

0

嗯,你做兩件事情:

  • imagejpeg磁盤創建JPG,叫$thumb_image_name
  • 上傳JPG到FTP服務器ftp_put

你永遠不刪除本地文件,所以它仍然存在。

+0

這將是有道理的哈哈...有無論如何使imagejpeg()在FTP服務器上工作?所以它不會上傳到本地?我希望本地服務器沒有任何事情發生,我需要它在FTP服務器上完成所有操作:/我試圖使它$ thumb_image_name = imagejpeg($ newImage,$ thumb_image_name,90);但它失敗了。 – Nate 2011-06-06 23:17:23