2019-02-22/每天 5 个函数

imageaffinematrixget / imagealphablending / imageantialias / imagearc / imagechar

Posted by CHEN on February 22, 2019

一些建议,如下:

1. 记 PHP 函数

2. 学习优秀代码的思想

3. 不停写代码,十万行代码什么时候达到什么时候就 NB 了

4. 多尝试脱离任何固定思维去写东西、按自己的想法去做、然后你会遇到问题,总结起来

5. 反思自己写代码的套路与流程,敢于怀疑

从每天 5 个函数做起

一:imageaffinematrixget()

imageaffinematrixget — 获取矩阵

实例

array imageaffinematrixget ( int $type [, mixed $options ] )

返回结果

成功则返回数组(其中键为 0 至 5 的数字)和浮点值,或者在失败时返回 FALSE

二:imagealphablending()

imagealphablending — 设定图像的混色模式

实例

<?php
    //  创建图像
    $im = imagecreatetruecolor(100, 100);
    
    // 启用混色模式
    imagealphablending($im, true);
    
    // 画一个正方形
    imagefilledrectangle($im, 30, 30, 70, 70, imagecolorallocate($im, 255, 0, 0));
    
    // 输出
    header('Content-type: image/png');
    
    imagepng($im);
    imagedestroy($im);
?>

三:imageantialias()

imageantialias — 是否使用抗锯齿(antialias)功能

实例

<?php
    //  使用抗锯齿图片和一个普通图片
    $aa = imagecreatetruecolor(400, 100);
    $normal = imagecreatetruecolor(200, 100);
    
    // 使用抗锯齿功能
    imageantialias($aa, true);
    
    // 设置颜色
    $red = imagecolorallocate($normal, 255, 0, 0);
    $red_aa = imagecolorallocate($aa, 255, 0, 0);
    
    // 画两条线
    imageline($normal, 0, 0, 200, 100, $red);
    imageline($aa, 0, 0, 200, 100, $red_aa);
    
    // 合并图像
    imagecopymerge($aa, $normal, 200, 0, 0, 0, 200, 100, 100);
    
    // 输出图像
    header('Content-type: image/png');
    
    imagepng($aa);
    imagedestroy($aa);
    imagedestroy($normal);
?>

四:imagearc()

imagearc — 用于画椭圆弧

语法

<?php
    $img = imagecreatetruecolor(200, 200);// 创建一个 200*200 图片
    $white  = imagecolorallocate($img,   255,   255, 255); // 颜色
    
    // 画椭圆弧
    imagearc($img, 140,  75,  50,  50,  0, 360, $white);
    
    // 浏览器输出图片
    header("Content-type: image/png");
    imagepng($img);
    
    imagedestroy($img);

五:imagechar()

imagechar — 写出横向的字符

语法

<?php
    $im = imagecreate(100,100);
    
    $string = 'PHP';
    
    $bg = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);
    
    // 左上角输出字符 P
    imagechar($im, 5, 0, 0, $string, $black);
    
    header('Content-type: image/png');
    imagepng($im);