php 制作图片占位 Placeholder

图片占位这个功能真是在网上遍地都是,各有特点,但是考虑到网站项目外链及访问问题,所以还是自己写一个吧。也可以用那个国产的js的。
根据自己的需求分为以下几个字段
/500×500(图片尺寸)/ffffff(背景颜色)/000(文字颜色)/text(自定义文字)
其中的颜色可以使HEX或者RGBA(RGBA中a支持0-1之间的小数或者1-127的数字,1比较特殊,为完全透明。)

示例图片
https://tools.dyniao.com/p/500×500/255,255,0,0.5/000000

干货在下边,用了一般gd生成图片的方法,加上了点处理的函数。

//获取后边的参数
$ele = explode('/', $_SERVER['QUERY_STRING']);
//一些默认值
$size = isset($ele[0]) ? $ele[0] : "500x500";
$bg = isset($ele[1]) ? $ele[1] : "34495e";
$color = isset($ele[2]) ? $ele[2] : "ffffff";
$text = isset($ele[3]) ? $ele[3] : $size;
preg_match('/(\\d{1,4})[xX*?]{1}(\\d{1,4})/', $size, $mat);
if ($mat) {
    $width = $mat[1];
    $height = $mat[2];
} else {
    $width = 500;
    $height = 500;
    $text = $width . 'x' . $height;
}
if ($width < 1 || $height < 1) {
    die("too small!");
}
$area = $width * $height;
if ($area >= 10000000 || $width > 9999 || $height > 9999) {
    die("to big!");
}
$newbg = color2rgba($bg);
$newcolor = color2rgba($color);
//设置显示的字体
$font = '/fonts/wqy-microhei.ttc';
$text_angle = 0;
$fontsize = max(min($width / strlen($text) * 1.15, $height * 0.5), 5);
$text = mb_convert_encoding(urldecode($text), "UTF-8", "auto");
$rect = ImageTTFBBox($fontsize, 0, $font, $text);
$a = deg2rad($text_angle);
$ca = cos($a);
$sa = sin($a);
$ret = array();
for ($i = 0; $i < 7; $i += 2) {
    $ret[$i] = round($rect[$i] * $ca + $rect[$i + 1] * $sa);
    $ret[$i + 1] = round($rect[$i + 1] * $ca - $rect[$i] * $sa);
}
$textWidth = ceil(($ret[4] - $ret[1]) * 1.07);
$textHeight = ceil((abs($ret[7]) + abs($ret[1])) * 1);
$textX = ceil(($width - $textWidth) / 2);
$textY = ceil(($height - $textHeight) / 2 + $textHeight);
header("Content-type:image/png");
$im = imagecreatetruecolor($width, $height);
$bg_color = imagecolorallocatealpha($im, $newbg[0], $newbg[1], $newbg[2], $newbg[3]);
$text_color = imagecolorallocatealpha($im, $newcolor[0], $newcolor[1], $newcolor[2], $newcolor[3]);
imageFilledRectangle($im, 0, 0, $width, $height, $bg_color);
imagettftext($im, $fontsize, 0, $textX, $textY, $text_color, $font, $text);
imagepng($im);
imagedestroy($im);
}
//将hex颜色转换为rgba的颜色
function color2rgba($color)
{
    $rgba = '';
    $color = str_replace('£¬', ',', $color);
    if (strstr($color, ',')) {
        $c = explode(',', $color);
        $r = isset($c[0]) ? $c[0] : '0';
        $g = isset($c[1]) ? $c[1] : '0';
        $b = isset($c[2]) ? $c[2] : '0';
        $a = isset($c[3]) ? $c[3] : '0';
        $rgba = array($r, $g, $b, rgba0127($a));
    } else {
        $c = str_replace("#", "", $color);
        if (strlen($c) == 3) {
            $r = hexdec(substr($c, 0, 1) . substr($c, 0, 1));
            $g = hexdec(substr($c, 1, 1) . substr($c, 1, 1));
            $b = hexdec(substr($c, 2, 1) . substr($c, 2, 1));
        } else {
            $r = hexdec(substr($c, 0, 2));
            $g = hexdec(substr($c, 2, 2));
            $b = hexdec(substr($c, 4, 2));
        }
        $rgba = array($r, $g, $b, 0);
    }
    return $rgba;
}
//根据写的是0到1的小数还是1-127的整数来判断alpha的透明度
function rgba0127($num)
{
    $result = 0;
    if ($num > 1 && $num <= 127) {
        $result = $num;
    } else {
        $result = 127 * $num;
    }
    return $result;
}

因为我放在子目录 p 下边了,所以nginx的伪静态为

rewrite ^/p/(.*)$ /p/index.php?$1;

根据自己的情况修改。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.