2013-03-10 57 views
1

創建縮略圖作爲一個文件是非常容易的,但我無法弄清楚如何使用PHP圖像函數來創建圖像並將數據存儲在變量女巫我可以使用將數據存儲在數據庫表中。使用PHP創建縮略圖blob使用php

我是新來的圖像功能,我不明白他們的工作。他們是否以目錄爲導向?所以我必須爲縮略圖製作者創建一個類似tmp的地圖,然後使用file_get_content然後刪除圖像。

或者我可以在過程中如何存儲圖像的數據。我不知道!

這是我如何創建縮略圖,如果我需要它們保存爲一個文件:

//found at stackoverflow 
function tumbmaker($updir, $img,$MaxWe=100,$MaxHe=150){ 
    $arr_image_details = getimagesize($img); 
    $width = $arr_image_details[0]; 
    $height = $arr_image_details[1]; 

    $percent = 100; 
    if($width > $MaxWe) $percent = floor(($MaxWe * 100)/$width); 

    if(floor(($height * $percent)/100)>$MaxHe) 
    $percent = (($MaxHe * 100)/$height); 

    if($width > $height) { 
     $newWidth=$MaxWe; 
     $newHeight=round(($height*$percent)/100); 
    }else{ 
     $newWidth=round(($width*$percent)/100); 
     $newHeight=$MaxHe; 
    } 

    if ($arr_image_details[2] == 1) { 
     $imgt = "ImageGIF"; 
     $imgcreatefrom = "ImageCreateFromGIF"; 
    } 
    if ($arr_image_details[2] == 2) { 
     $imgt = "ImageJPEG"; 
     $imgcreatefrom = "ImageCreateFromJPEG"; 
    } 
    if ($arr_image_details[2] == 3) { 
     $imgt = "ImagePNG"; 
     $imgcreatefrom = "ImageCreateFromPNG"; 
    } 


    if ($imgt) { 
     $old_image = $imgcreatefrom($img); 
     $new_image = imagecreatetruecolor($newWidth, $newHeight); 
     imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 

     $imgt($new_image, $updir."_t.jpg"); 
     return;  
    } 
} 

我用這個代碼做了它:

function tumbmaker($img_source,$MaxWe=100,$MaxHe=150){ 
     $arr_image_details = getimagesize($img_source); 
     $width = $arr_image_details[0]; 
     $height = $arr_image_details[1]; 

     $percent = 100; 
     if($width > $MaxWe) $percent = floor(($MaxWe * 100)/$width); 

     if(floor(($height * $percent)/100)>$MaxHe) 
     $percent = (($MaxHe * 100)/$height); 

     if($width > $height) { 
      $newWidth=$MaxWe; 
      $newHeight=round(($height*$percent)/100); 
     }else{ 
      $newWidth=round(($width*$percent)/100); 
      $newHeight=$MaxHe; 
     } 

     if ($arr_image_details[2] == 1) { 
      $imgt = "ImageGIF"; 
      $imgcreatefrom = "ImageCreateFromGIF"; 
     } 
     if ($arr_image_details[2] == 2) { 
      $imgt = "ImageJPEG"; 
      $imgcreatefrom = "ImageCreateFromJPEG"; 
     } 
     if ($arr_image_details[2] == 3) { 
      $imgt = "ImagePNG"; 
      $imgcreatefrom = "ImageCreateFromPNG"; 
     } 


     if ($imgt) { 
      $old_image = $imgcreatefrom($img_source); 
      $new_image = imagecreatetruecolor($newWidth, $newHeight); 
      imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
      $imgt($new_image, $updir."_t.jpg"); 
      ob_start(); 
      $imgt($new_image); 
      $imgData = ob_get_clean(); 
      return $imgData; 
     } 
} 

閱讀答案更多信息

回答

1

如果您不提供圖像創建功能的文件名,他們只會將圖像數據寫入PHP的輸出緩衝區。

這意味着你可以攔截這樣的圖像數據:

ob_start(); 
$imgt($new_image); 
$imgData = ob_get_clean(); 

看一看的PHP GD Manual

+0

這工作得很好 – botenvouwer 2013-03-12 12:38:17