简体   繁体   English

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

[英]Image file size smaller form base64 encode

There is tow image in base64 format ( $theme_image_enc and $theme_image_enc_little ). 有base64格式的两个图像( $theme_image_enc$theme_image_enc_little )。 Now I want to make the second image ( $theme_image_enc_little ) size smaller like 15kb. 现在,我想将第二个图像( $theme_image_enc_little )的大小减小到15kb。 How can do that? 那怎么办

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

try to show low image size in the browser you need to reduce the quality using with imagejpeg function 尝试在浏览器中显示低图像尺寸,需要使用imagejpeg函数降低质量

// Take value between $quality = (0 < 100)

imagejpeg($theme_image_enc_little, NULL, $quality);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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