2013-03-10 92 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