Skip to content

Commit

Permalink
改进
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Aug 7, 2024
1 parent 751d450 commit d1d748c
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ public function save($pathname, $type = null, $quality = 80, $interlace = true)
*/
public function output()
{
$type =$this->info['type'] ;
$type = $this->info['type'];
header("content-type: image/{$type}");
switch ($type)
{
switch ($type) {
case 'png':
imagepng($this->im);
break;
Expand Down Expand Up @@ -283,16 +282,17 @@ public function flip($direction = self::FLIP_X)
public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
{
//设置保存尺寸

empty($width) && $width = $w;
empty($height) && $height = $h;
do {
//创建新图像
$img = imagecreatetruecolor($width, $height);
$img = imagecreatetruecolor((int) $width, (int) $height);
// 调整默认颜色
$color = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $color);
//裁剪
imagecopyresampled($img, $this->im, 0, 0, $x, $y, $width, $height, $w, $h);
imagecopyresampled($img, $this->im, 0, 0, (int) $x, (int) $y, (int) $width, (int) $height, $w, $h);
imagedestroy($this->im); //销毁原图
//设置新图像
$this->im = $img;
Expand Down Expand Up @@ -373,8 +373,8 @@ public function thumb($width, $height, $type = self::THUMB_SCALING)
$newh = (int) ($h * $scale);
$x = $this->info['width'] - $w;
$y = $this->info['height'] - $h;
$posx = (int)(($width - $w * $scale) / 2);
$posy = (int)(($height - $h * $scale) / 2);
$posx = (int) (($width - $w * $scale) / 2);
$posy = (int) (($height - $h * $scale) / 2);
do {
//创建新图像
$img = imagecreatetruecolor($width, $height);
Expand Down Expand Up @@ -483,9 +483,9 @@ public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100)
// 调整默认颜色
$color = imagecolorallocate($src, 255, 255, 255);
imagefill($src, 0, 0, $color);
imagecopy($src, $this->im, 0, 0, $x, $y, $info[0], $info[1]);
imagecopy($src, $this->im, 0, 0, (int) $x, (int) $y, $info[0], $info[1]);
imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha);
imagecopymerge($this->im, $src, (int) $x, (int) $y, 0, 0, $info[0], $info[1], $alpha);
//销毁临时图片资源
imagedestroy($src);
} while (!empty($this->gif) && $this->gifNext());
Expand Down Expand Up @@ -586,15 +586,17 @@ public function text($text, $font, $size, $color = '#00000000',
$ox = $oy = $offset;
}
/* 图片黑白检测 */
if ($color == "auto") {
if ("auto" == $color) {
//X方向采集宽度:单英文字符占据宽度约为字体大小/1.6,单中文字符占据宽度约为字体大小*4/3;Y方向采集宽度:英文字符高度约为字体大小,中文会高一些。
//使用保守宽度,以免在纯英文情况下采集区域超出图像范围,并且精度完全可以满足本功能。
$pickX = intval(mb_strwidth($text) * ($size / 1.6));
$pickY = $size;

$brightness = 0;
for ($i = $x + $ox; $i < $pickX + $x + $ox; $i++) { //根据文字基线确定要进行遍历的像素
for ($j = $y + $oy - $pickY; $j < $y + $oy; $j++) { //基线修正
for ($i = $x + $ox; $i < $pickX + $x + $ox; $i++) {
//根据文字基线确定要进行遍历的像素
for ($j = $y + $oy - $pickY; $j < $y + $oy; $j++) {
//基线修正
$brightness += self::getBrightnessOfPixel($i, $j);
}
}
Expand Down Expand Up @@ -625,9 +627,9 @@ public function text($text, $font, $size, $color = '#00000000',
private function getBrightnessOfPixel($x, $y)
{
$rgb = imagecolorat($this->im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

//红绿蓝能量不同,亮度不同,对应系数也不同(参考https://www.w3.org/TR/AERT/#color-contrast)
$brightness = intval($r * 0.299 + $g * 0.587 + $b * 0.114);
Expand Down

0 comments on commit d1d748c

Please sign in to comment.