|
帝国cms自带文章生成标题图片插件AutoTitlePic.zip
发布时间:2024-12-16 浏览次数:0 返回列表
<?php
function dp_textPic($file, $title, $smalltext, $config)
{
$path = dirname(__FILE__) . '/';
$width = (int)$config['imgWidth'];
$height = (int)$config['imgHeight'];
$titleFont = $path . 'font/' . $config['titleFont'];
if (!file_exists($titleFont)) {
return false;
}
$introFont = $path . 'font/' . $config['introFont'];
$bg_c_r = explode('|', $config['bgColor']);
$bgColor = $bg_c_r[array_rand($bg_c_r, 1)];
$c1 = dp_str2rgb($bgColor);
$fontColor = $config['color'];
$isBorder = $config['border'];
$borderColor = $config['lineColor'];
$q = $config['q'];
$padding = 20;
$im = imagecreatetruecolor($width, $height);
imagefill($im, 0, 0, imagecolorallocate($im, $c1['red'], $c1['green'], $c1['blue']));
if ($isBorder) {
$c2 = dp_str2rgb($borderColor);
$colorLine = imagecolorallocate($im, $c2['red'], $c2['green'], $c2['blue']);
$im = dp_writeLine($im, $width, $height, $colorLine, $padding, 2);
$padding += 8;
$im = dp_writeLine($im, $width, $height, $colorLine, $padding, 1);
}
$lineHeight = 2;
$fontSize = 30;
$wrapSize = 14;
$paddingTop = $padding + 30;
$paddingLeft = $paddingTop;
$r = dp_writeText($im, $wrapSize, $paddingTop, $paddingLeft, $lineHeight, $fontSize, $fontColor, $titleFont, $title, 2);
$im = $r['im'];
$lineNum = $r['num'];
$water_w = 0;
if ($config['waterPicPath']) {
$waterImage = dp_getPic($path . $config['waterPicPath']);
if ($waterImage) {
$posX = $paddingLeft;
$posY = $paddingTop + $lineNum * $fontSize * $lineHeight + 30;
$water_w = 150;
dp_waterPic($im, $waterImage, $posX, $posY, $water_w);
}
}
if (file_exists($introFont) && $smalltext) {
$lineHeight = 2.2;
$fontSize = 22;
$wrapSize = 14;
$posX = $paddingLeft + $water_w + 25;
$posY = $paddingTop + $lineNum * $fontSize * $lineHeight + 30 + $fontSize;
$r = dp_writeText($im, $wrapSize, $posY, $posX, $lineHeight, $fontSize, $fontColor, $introFont, $smalltext, 5);
$im = $r['im'];
}
imagejpeg($im, $file, $q);
imagedestroy($im);
return true;
}
function dp_getPic($path)
{
$hand = @opendir($path);
$ar = array();
while (($file = @readdir($hand)) !== false) {
$sfile = strtolower($file);
if (strpos($sfile, '.png') === false) {
continue;
}
$ar[] = $path . '/' . $file;
}
$num = count($ar);
if ($num < 1) {
return '';
}
$key = rand(0, ($num - 1));
return $ar[array_rand($ar, 1)];
}
function dp_waterPic($im, $waterImage, $posX, $posY, $water_w = 0, $water_h = 0)
{
$water_info = getimagesize($waterImage);
if ($water_info[2] != 3) {
return $im;
}
$w = $water_info[0];
$h = $water_info[1];
imagealphablending($im, true);
$water_im = imagecreatefrompng($waterImage);
if ($water_w != $w && $w) {
if ($water_w) {
$water_h = ceil(($h * $water_w) / $w);
} else {
if ($water_h) {
$water_w = ceil(($water_h * $w) / $h);
}
}
if (!$water_h && !$water_w) {
$water_w = $w;
$water_h = $h;
}
$new_water_im = imagecreatetruecolor($water_w, $water_h);
imagealphablending($new_water_im, false);
$color = imagecolorallocatealpha($new_water_im, 0, 0, 0, 127);
imagefill($new_water_im, 0, 0, $color);
imagesavealpha($new_water_im, true);
imagecopyresampled($new_water_im, $water_im, 0, 0, 0, 0, $water_w, $water_h, $w, $h);
} else {
$new_water_im = $water_im;
}
imagecopy($im, $new_water_im, $posX, $posY, 0, 0, $water_w, $water_h);
return $im;
}
function dp_writeText($im, $wrapSize, $paddingTop, $paddingLeft, $lineHeight, $fontSize, $fontColor, $fontStyle, $text, $maxLineNum)
{
$cr = dp_str2rgb($fontColor);
$color = imagecolorallocate($im, $cr['red'], $cr['green'], $cr['blue']);
$textArr = array();
$i = 0;
$arr = dp_strDiv($text, $wrapSize);
foreach ($arr as $v) {
$textArr[] = $v;
$i += 1;
if ($i >= $maxLineNum) {
break;
}
}
$height = intval($fontSize * $lineHeight);
foreach ($textArr as $k => $text) {
$offset = $paddingTop + $height * ($k + 1) - intval(($height - $fontSize) / 2);
imagettftext($im, $fontSize, 0, $paddingLeft, $offset, $color, $fontStyle, $text);
}
return array('im' => $im, 'num' => $i);
}
function dp_strDiv($str, $width = 10)
{
$strArr = array();
$len = strlen($str);
$count = 0;
$flag = 0;
while ($flag < $len) {
if (ord($str[$flag]) > 128) {
$count += 1;
$flag += 3;
} else {
$count += 0.5;
$flag += 1;
}
if ($count >= $width) {
$strArr[] = substr($str, 0, $flag);
$str = substr($str, $flag);
$len -= $flag;
$count = 0;
$flag = 0;
}
}
$strArr[] = $str;
return $strArr;
}
function dp_str2rgb($str)
{
$color = array('red' => 0, 'green' => 0, 'blue' => 0);
$str = str_replace('#', '', $str);
$len = strlen($str);
if ($len == 6) {
$arr = str_split($str, 2);
$color['red'] = (int)base_convert($arr[0], 16, 10);
$color['green'] = (int)base_convert($arr[1], 16, 10);
$color['blue'] = (int)base_convert($arr[2], 16, 10);
return $color;
}
if ($len == 3) {
$arr = str_split($str, 1);
$color['red'] = (int)base_convert($arr[0] . $arr[0], 16, 10);
$color['green'] = (int)base_convert($arr[1] . $arr[1], 16, 10);
$color['blue'] = (int)base_convert($arr[2] . $arr[2], 16, 10);
return $color;
}
return $color;
}
function dp_imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
if ($thick == 1) {
return imageline($image, $x1, $y1, $x2, $y2, $color);
}
$t = $thick / 2 - 0.5;
if ($x1 == $x2 || $y1 == $y2) {
return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
}
$k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
$a = $t / sqrt(1 + pow($k, 2));
$points = array(
round($x1 - (1 + $k) * $a), round($y1 + (1 - $k) * $a),
round($x1 - (1 - $k) * $a), round($y1 - (1 + $k) * $a),
round($x2 + (1 + $k) * $a), round($y2 - (1 - $k) * $a),
round($x2 + (1 - $k) * $a), round($y2 + (1 + $k) * $a),
);
imagefilledpolygon($image, $points, 4, $color);
return imagepolygon($image, $points, 4, $color);
}
function dp_writeLine($im, $canvasWidth, $canvasHeight, $colorLine, $padding, $lineWidth)
{
$d1_x = $padding;
$d1_y = $padding;
$d2_x = $canvasWidth - $padding;
$d2_y = $padding;
$d3_x = $padding;
$d3_y = $canvasHeight - $padding;
$d4_x = $canvasWidth - $padding;
$d4_y = $canvasHeight - $padding;
dp_imagelinethick($im, $d1_x, $d1_y, $d2_x, $d2_y, $colorLine, $lineWidth);
dp_imagelinethick($im, $d3_x, $d3_y, $d4_x, $d4_y, $colorLine, $lineWidth);
dp_imagelinethick($im, $d1_x, $d1_y, $d3_x, $d3_y, $colorLine, $lineWidth);
dp_imagelinethick($im, $d2_x, $d2_y, $d4_x, $d4_y, $colorLine, $lineWidth);
return $im;
|