【问题标题】:Resize image from base64_encode从 base64_encode 调整图像大小
【发布时间】:2018-01-10 17:47:06
【问题描述】:

我的图像文件大小是 800x600,我想将此 800x600 调整为 400x300,然后以数据库 base64_encode 格式保存两个图像(800x600 和 400x300)。我可以将第一张图像 (800x600) 保存在数据库中,但是如何将第二张图像 (400x300) 转换为 base64_encode 格式并保存在数据库中?我不想使用两个输入字段。我认为一个输入字段就足够了。

$image              = ($_FILES["my_image"]["name"]);
$theme_image        = ($_FILES["my_image"]["tmp_name"]);
$bin_string         = file_get_contents("$theme_image"); 
$theme_image_enc    = base64_encode($bin_string); 

【问题讨论】:

    标签: php image base64 encode image-resizing


    【解决方案1】:

    您必须编写一个小脚本来从第一个图像创建新图像并在其上进行 base64_encode

    $WIDTH                  = 400; // The size of your new image
    $HEIGHT                 = 300;  // The size of your new image
    $QUALITY                = 100; //The quality of your new image
    $DESTINATION_FOLDER = DependOfYourRepository; // The folder of your new image
    
    // The directory where is your image
    $filePath = DependOfYourRepository; 
    
    // This little part under depend if you wanna keep the ratio of the image or not
    list($width_orig, $height_orig) = getimagesize($filePath);
    $ratio_orig = $width_orig/$height_orig;
    if ($WIDTH/$HEIGHT > $ratio_orig) {
        $WIDTH = $HEIGHT*$ratio_orig;
    } else {
        $HEIGHT = $WIDTH/$ratio_orig;
    }
    
    // The function using are different for png, so it's better to check
    if ($file_ext == "png") {
        $image = imagecreatefrompng($filePath);
    } else {
        $image = imagecreatefromjpeg($filePath);
    }
    
    // I create the new image with the new dimension and maybe the new quality
    $bg = imagecreatetruecolor($WIDTH, $HEIGHT);
    imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
    imagealphablending($bg, TRUE);
    imagecopyresampled($bg, $image, 0, 0, 0, 0, $WIDTH, $HEIGHT, $width_orig, $height_orig);
    imagedestroy($image);
    imagejpeg($bg, $DESTINATION_FOLDER.$filename, $QUALITY);
    $bin_string_little = file_get_contents($DESTINATION_FOLDER.$filename); 
    // I remove the image created because you just wanna save the base64 version
    unlike($DESTINATION_FOLDER.$filename);
    imagedestroy($bg);
    $theme_image_enc_little =  base64_encode($bin_string_little); 
    // And now do what you want with the result 
    

    编辑 1

    不使用第二张图片的目录也可以做到这一点,但这很棘手。

    $theme_image_little = imagecreatefromstring(base64_decode($theme_image_enc));
    $image_little = imagecreatetruecolor($WIDTH, $HEIGHT);
    // $org_w and org_h depends of your image, in your case, i guess 800 and 600
    imagecopyresampled($image_little, $theme_image_little, 0, 0, 0, 0, $WIDTH, $HEIGHT, $org_w, $org_h);
    
    // Thanks to Michael Robinson
    // start buffering
    ob_start();
    imagepng($image_little);
    $contents =  ob_get_contents();
    ob_end_clean();
    
    $theme_image_enc_little = base64_encode($contents):
    

    【讨论】:

    • 我的文件以base64_encode格式直接保存在数据库中。我不想使用目标文件夹。如果不使用目标路径第二张图像保存在数据库中,这可能吗?
    • 谢谢,可以将第二张图片缩小到 15kb。因为第二张图片将用于缩略图。
    猜你喜欢
    • 2012-02-24
    • 2023-03-28
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多