微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用php创建png图表图像?

如何解决如何使用php创建png图表图像?

我有一个 api,它返回有关加密货币的历史汇率。

我想像这样生成和保存迷你图:

enter image description here

请看:https://coinmarketcap.com/,这种类型的图表是否已经保存在服务器中?或者他们会在每次通话中每次都生成??

无论如何... 我不知道我应该做什么以及如何做。

我尝试了一些方法,但我无法使那个透明 (png)

我尝试过的方法

    $size = '200x60';
    $back = '#fbefef';
    $line = '#f73536';
    $fill = '#fbefef';
    $data = isset($test) ? $test : array();

    list($w,$h) = explode('x',$size);
    $w = floor(max(50,min(800,$w)));
    $h = !strstr($size,'x') ? $w : floor(max(20,$h)));
    $t = 2.3;
    $s = 5;

    $w *= $s;
    $h *= $s;
    $t *= $s;

    $salt = 'v1.0.1';
    $hash = md5($salt);

    $data = (count($data) < 2) ? array_fill(0,2,$data[0]) : $data;
    $count = count($data);
    $step = $w / ($count - 1);

    $min = min($data);
    $max = max($data);
    if ($max != $min) {
        foreach ($data as $k => $v) {
            $data[$k] -= $min;
        }
        $max = max($data);
    }


    $im = imagecreatetruecolor($w,$h);
    $bg = imagecolorallocate($im,255,255);

    list($r,$g,$b) = $this->hexToRgb($line);
    $fg = imagecolorallocate($im,$r,$b);
    list($r,$b) = $this->hexToRgb($fill);
    $lg = imagecolorallocate($im,$b);

    imagecolortransparent($im,$bg);
    imagesavealpha($im,true);


    imagefill($im,$bg);
    imagesetthickness($im,$t);

    foreach ($data as $k => $v)
    {
        $v = $v > 0 ? round($v / $max * $h) : 0;
        $data[$k] = max($s,min($v,$h - $s));
    }

    $x1 = 0;
    $y1 = $h - $data[0];
    $line = array();
    $poly = array(0,$h + 50,$x1,$y1);
    for ($i = 1; $i < $count; $i++)
    {
        $x2 = $x1 + $step;
        $y2 = $h - $data[$i];
        array_push($line,array($x1,$y1,$x2,$y2));
        array_push($poly,$y2);
        $x1 = $x2;
        $y1 = $y2;
    }
    array_push($poly,$h + 50);

    imagefilledpolygon($im,$poly,$count + 2,$lg);

    foreach ($line as $k => $v)
    {
        list($x1,$y2) = $v;
        imageline($im,$y2,$fg);
    }

    $om = imagecreatetruecolor($w / $s,$h / $s);
    imagecopyresampled($om,$im,$w / $s,$h / $s,$w,$h);
    imagedestroy($im);

    header('Content-Type: image/png');
    header('Content-disposition: inline; filename="sparkline_' . time() . substr(microtime(),3) . '.png"');
    header('ETag: ' . $hash);
    header('Accept-Ranges: none');
    header('Cache-Control: max-age=604800,must-revalidate');
    header('Expires: ' . gmdate('D,d M Y H:i:s T',strtotime('+7 days')));
    imagepng($om);
    imagedestroy($om);

帮帮我,谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。