图像文件大小较小,形式为base64编码

时间:2017-08-03 09:54:38

标签: php image base64 encode image-resizing

有base64格式的拖曳图像($theme_image_enc$theme_image_enc_little)。现在我想让第二张图像($theme_image_enc_little)尺寸变小,如15kb。怎么办?

    $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); 

    $WIDTH      = 400; // The size of your new image
    $HEIGHT     = 300;  // The size of your new image
    $QUALITY    = 100; //The quality of your new image
    $org_w      = 850;
    $org_h      = 660;

    $theme_image_little     = imagecreatefromstring(base64_decode($theme_image_enc));
    $image_little           = imagecreatetruecolor($WIDTH, $HEIGHT);
    imagecopyresampled($image_little, $theme_image_little, 0, 0, 0, 0, $WIDTH, $HEIGHT, $org_w, $org_h);
    ob_start();
    imagepng($image_little);
    $contents =  ob_get_contents();
    ob_end_clean();

    echo $theme_image_enc_little = base64_encode($contents);

1 个答案:

答案 0 :(得分:0)

尝试使用imagejpeg功能

在浏览器中显示低图像尺寸以降低质量
// Take value between $quality = (0 < 100)

imagejpeg($theme_image_enc_little, NULL, $quality);