国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

php - How to output image in base64
伊謝爾倫
伊謝爾倫 2017-05-24 11:32:19
0
1
1002

That’s it. Today I wrote a PHP template for generating verification codes based on the information on the Internet:

<?php
// 獲取驗(yàn)證碼(參數(shù):驗(yàn)證碼個(gè)數(shù),驗(yàn)證碼寬度,驗(yàn)證碼高度)
function getCode($num = 4, $width = 100, $height = 30){
    session_start();
    $authcode='';
    // 生成驗(yàn)證碼
    for($i=0;$i<$num;$i++){
        switch(rand(0,1))
        {
            case  0:$authcode[$i]=chr(rand(48,57));break; // 數(shù)字
            case  1:$authcode[$i]=chr(rand(65,90));break; // 大寫字母
            //case  2:$authcode[$i]=chr(rand(97,122));break; // 小寫字母
        }
    }
    $_SESSION["AuthCode"]=$authcode;
    $image=imagecreate($width,$height); // 賦值寬度,高度
    imagecolorallocate($image,255,255,255); // 設(shè)定圖片背景顏色
    // 生成干擾像素
    for($i=0;$i<80;$i++){
        $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
        imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);
    }
    // 打印字符到圖像
    for($i=0;$i<$num;$i++){
        $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
        imagechar($image,60,($width/$num)*$i,rand(0,5),$authcode[$i],$char_color);
    }
    
    // 將圖片直接輸出
    header("Content-type:image/png");
    imagepng($image);//輸出圖像到瀏覽器
    imagedestroy($image);//釋放資源
}
getCode();

But I don’t want him to output the image directly, but output the image in base64. How should I do it? I have tried many methods without success, unless it is necessary: ??

$img_file = 'https://www.xxxxxxxxxx.com/authcode.php';
$img_info = getimagesize($img_file);
$img_src = "data:{$img_info['mime']};base64," . base64_encode(file_get_contents($img_file));
echo "<img src='{$img_src}' />";

How can I directly output base64 in getCode?

伊謝爾倫
伊謝爾倫

小伙看你根骨奇佳,潛力無(wú)限,來(lái)學(xué)PHP伐。

reply all(1)
淡淡煙草味

The key to whether to output pictures directly is

  1. Close header

  2. imagepng function, the second parameter sets the image saving location.

Then read the storage location to read the image and convert it to base64.

php documentation

The comments section below the document may have the answers you need

ob_start();
imagepng($image);
$image_data = ob_get_contents();
ob_end_clean();

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template