[英]create thumbnail for blob with php
將縮略圖創建為文件非常容易,但是我不知道如何使用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;
}
}
閱讀答案以獲取更多信息
如果您沒有為圖像創建函數提供文件名,它們只會將圖像數據寫入PHP的輸出緩沖區。
這意味着您可以像這樣截取圖像數據:
ob_start();
$imgt($new_image);
$imgData = ob_get_clean();
看看PHP GD手冊 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.