2016-08-11 31 views
0

有沒有什麼方法可以將收到的圖像保存在兩個地方,一個原始大小的文件夾和另一個縮略圖的大小(90,120)在另一個文件夾中使用PHP。原始圖像的長寬比是(3,4),我只需要改變大小...
我的代碼已經很好,可以保存原始大小的圖像。
順便說一下,圖像從Android應用程序發送。
這裏是PHP代碼保存圖像文件夾中,然後插入數據的數據庫表...
如何將收到的圖像保存在兩個地方,一個原始大小,另一個縮略圖大小使用PHP

<?php 

    if ($_SERVER["REQUEST_METHOD"]=="POST") { 
     require 'connection.php'; 
     insertData(); 
    } 

    function insertData(){ 
     global $connect; 

     mysqli_set_charset($connect,"utf8"); 

     $name = $_POST["name"]; 
     $description = $_POST["description"]; 
     $image = $_POST["image"]; 

     $sql ="SELECT id FROM my_table ORDER BY name ASC"; 

     $res = mysqli_query($connect,$sql); 

     $id = 0; 

     while($row = mysqli_fetch_array($res)){ 
       $id = $row['id']; 
     } 

     $path = "img/$id.png"; 

     $decoded_string = base64_decode($image); 

     $file = fopen($path, 'wb'); 

     $is_written = fwrite($file, $decoded_string); 
     fclose($file); 

     $imgpath = "http://my-site.com/folder/$path"; 

     if($is_written > 0) { 

      $query = "INSERT INTO my_table (path,name,description) VALUES ('$imgpath','$name','$description ')"; 

      $result = mysqli_query($connect, $query) ; 

      if($result){ 
       echo "success"; 
      }else{ 
       echo "failed"; 
      } 

      mysqli_close($connect); 
     } 
    } 
?> 
+0

請問這段代碼實際上保存一次文件? – RiggsFolly

+0

你可以使用一個函數來創建縮略圖:http://stackoverflow.com/questions/11376315/creating-a-thumbnail-from-an-uploaded-image – grausof

+0

@RiggsFolly是的。沒關係 。我在這段代碼中做錯了什麼?我不是php的專家。 – unknown

回答

0

所以我找到了答案,只是從stweb答案
這裏有一點不同的是代碼

<?php 

if ($_SERVER["REQUEST_METHOD"]=="POST") { 
    require 'connection.php'; 
    insertData(); 
} 

function insertData(){ 
    global $connect; 

    mysqli_set_charset($connect,"utf8"); 

    $name = $_POST["name"]; 
    $description = $_POST["description"]; 
    $image = $_POST["image"]; 

    $sql ="SELECT id FROM my_table ORDER BY name ASC"; 

    $res = mysqli_query($connect,$sql); 

    $id = 0; 

    while($row = mysqli_fetch_array($res)){ 
      $id = $row['id']; 
    } 

    $path = "img/$id.png"; 
    $path_thumb = "thumb/$id.png"; 


    $decoded_string = base64_decode($image); 

    $file = fopen($path, 'wb'); 

    $is_written = fwrite($file, $decoded_string); 
    fclose($file); 

    $imgpath = "http://my-site.com/folder/$path"; 


    //---------------- create thumbnail ---------------- 

    $jpg_image = imagecreatefromjpeg($path); 

    $orig_w = imagesx($jpg_image); 
    $orig_h = imagesy($jpg_image); 
    $new_w = 120; 
    $new_h = 90; 

    $new_image = imagecreatetruecolor($new_w, $new_h); 
    imagecopyresampled($new_image, $jpg_image, 0, 0, 0, 0, $new_w, $new_h,$orig_w,$orig_h); 
    imagejpeg($new_image,$path_thumb); 

    $thumbpath = "http://my-site.com/folder/$path_thumb"; 

    //---------------- create thumbnail ---------------- 


    if($is_written > 0) { 

     $query = "INSERT INTO my_table (path,thumbnail,name,description) VALUES ('$imgpath','$thumbpath','$name','$description ')"; 

     $result = mysqli_query($connect, $query) ; 

     if($result){ 
      echo "success"; 
     }else{ 
      echo "failed"; 
     } 

     mysqli_close($connect); 
    } 
} 
?> 
0

您需要在服務器上安裝GD庫。只需echo phpinfo();並檢查其模塊是否啓用。

然後:(我現在不能測試,對不起

list($width, $height) = getimagesize($imgpath); 
$source = imagecreatefromjpeg($imgpath); 
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);/ 

imagecopyresized($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); 

imagejpeg($thumb, $path_to_thumb); 

我們得到原始圖像和其內容的大小,然後創建一個空的真彩色圖像。我們將源內容複製到空白圖像,同時傳遞拇指和原始大小。所以,我們保存縮略圖,傳遞$ thumb圖像並保存。

+0

謝謝Vanderlei。我會測試它。 – unknown

1

試試這個:

<?php 

if ($_SERVER["REQUEST_METHOD"]=="POST") { 
    require 'connection.php'; 
    insertData(); 
} 

function insertData(){ 
    global $connect; 

    mysqli_set_charset($connect,"utf8"); 

    $name = $_POST["name"]; 
    $description = $_POST["description"]; 
    $image = $_POST["image"]; 

    $sql ="SELECT id FROM my_table ORDER BY name ASC"; 

    $res = mysqli_query($connect,$sql); 

    $id = 0; 

    while($row = mysqli_fetch_array($res)){ 
      $id = $row['id']; 
    } 

    $path = "img/$id.png"; 
    $path_thumb = "thumb/$id.png"; 


    $decoded_string = base64_decode($image); 

    $file = fopen($path, 'wb'); 

    $is_written = fwrite($file, $decoded_string); 
    fclose($file); 
    $jpg_image = imagecreatefromjpeg($path); 

    $orig_w = imagesx($jpg_image); 
    $orig_h = imagesy($jpg_image); 
    $new_w = 90; 
    $new_h = round ((90 * $orig_h)/$orig_w); 

    $new_image = imagecreatetruecolor($new_w, $new_h); 
    imagecopyresampled($new_image, $jpg_image, 0, 0, 0, 0, $new_w, $new_h,$orig_w,$orig_h); 
    imagejpeg($new_image,$path_thumb,80); 


    $imgpath = "http://my-site.com/folder/$path"; 

    if($is_written > 0) { 

     $query = "INSERT INTO my_table (path,name,description) VALUES ('$imgpath','$name','$description ')"; 

     $result = mysqli_query($connect, $query) ; 

     if($result){ 
      echo "success"; 
     }else{ 
      echo "failed"; 
     } 

     mysqli_close($connect); 
    } 
} 
?> 

不要忘記創建文件夾 '拇指' 附近的 'IMG' 文件夾。這個例子是爲JPEG圖像。如果你需要發佈PNG文件,你需要調用imagecreatefrompng而不是imagecreatefromjpeg;

相關問題