繁体   English   中英

用PHP创建blob的缩略图

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM