GD库实例1
程序员文章站
2022-06-19 16:11:55
**图片转字符**//图片转字符// $lei->color_dodge_blend($im,2,$width,$height);$image_path = "1.jpg";// $image_path="2.png";$im = imagecreatefromjpeg($image_path); $imgWidth = imagesx($im);$imgHeight = imagesy($im);for($i=0;$i<$imgHeight;$i++)for($j=...
图片转素描
$image_path = "1.jpg";
// $image_path="2.png";
$im_gray = imagecreatefromjpeg($image_path);
$width = imagesx($im_gray);
$height = imagesy($im_gray);
//对图像使用过滤器
imagefilter($im_gray, IMG_FILTER_GRAYSCALE);
//用于创建画布
$im_invert = imagecreatetruecolor($width, $height);
//在画布中绘制图像
imagecopy($im_invert, $im_gray, 0, 0, 0, 0, $width, $height);
//对图像使用过滤器
imagefilter($im_invert, IMG_FILTER_NEGATE);
imagefilter($im_invert, IMG_FILTER_GAUSSIAN_BLUR);
//这个算法最后再给出
$image = color_dodge_blend($im_gray, $im_invert, $width, $height);
header('Content-Type: image/jpeg');
//设置图片类型,同样如果想生成gif或者png图象,要替换成相应的头
imagejpeg($image);
//输出图象,如果是gif就用imagegif,如果是png就用imagepng……
imagedestroy($image);
function color_dodge($mask, $image)
{
$GAUSS_COUNT = 10;
$COLOR_STEP = 30;
$result = ($image === 255) ? $image : min(255, (($mask << 8 ) / (255 - $image)));
//$result = ($result <= 210) ? 0 : $result;
// var_dump($result,$COLOR_STEP);die;
$result = (int) ($result / $COLOR_STEP) * $GAUSS_COUNT;
return $result;
}
function kmax1($array)
{
return array_search(max($array), $array);
}
function color_dodge_blend($source, $layer, $width, $height)
{
//用于创建画布
$image = imagecreatetruecolor($width, $height);
$color_array = array();
for ($x = 0; $x < $width; $x++)
{
for ($y = 0; $y < $height; $y++)
{
//计算灰度
$rgb = ImageColorAt($source, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$rgb2 = ImageColorAt($layer, $x, $y);
$r2 = ($rgb2 >> 16) & 0xFF;
$g2 = ($rgb2 >> 8) & 0xFF;
$b2 = $rgb2 & 0xFF;
$r = color_dodge($r, $r2);
$g = color_dodge($g, $g2);
$b = color_dodge($b, $b2);
$rgb = ($r << 16) | ($g << 8) | $b;
$color_array[$rgb] = key_exists($rgb, $color_array) ? $color_array[$rgb] + 1 : 1;
//在指定的坐标处绘制像素。
imagesetpixel($image, $x, $y, $rgb);
}
}
$background = kmax1($color_array);
for ($x = 0; $x < $width; $x++)
{
for ($y = 0; $y < $height; $y++)
{
//获取颜色
$rgb = ImageColorAt($image, $x, $y);
if ($rgb === $background)
{
//在指定的坐标处绘制像素。
imagesetpixel($image, $x, $y, 0xFFFFFF);
}
}
}
return $image;
}
图片转字符
//图片转字符
// $lei->color_dodge_blend($im,2,$width,$height);
$image_path = "1.jpg";
// $image_path="2.png";
$im = imagecreatefromjpeg($image_path);
$imgWidth = imagesx($im);
$imgHeight = imagesy($im);
for($i=0;$i<$imgHeight;$i++)
for($j=0;$j<$imgWidth;$j++)
{
$rgb = ImageColorAt($im, $j,$i);
$r=($rgb>>16); //取得红色分量
$g=($rgb&0x00ff00)>>8; //取得绿色分量
$b=$rgb&0x0000ff; //取得蓝色分量
$t=($r*3+$g*6+$b)/10;//转成灰度
$t=floor($t);
if($t<25) //输出ascii
{
echo "@";
}else if($t<50)
{
echo "B";
}else if($t<75)
{
echo "A";
}else if($t<100)
{
echo "@";
}else if($t<125)
{
echo "9";
}else if($t<150)
{
echo "*";
}else if($t<175)
{
echo "+";
}else if($t<200)
{
echo ";";
}else if($t<=225)
{
echo ",";
}else{
echo ".";
}
if($j>$imgWidth-2) //跟着图片换行
echo "\r\n";
}
die;
图片转黑白灰
// 黑白灰
$image_path = "1.jpg";
// $image_path="2.png";
$im = imagecreatefromjpeg($image_path);
//如果是Gif用imagecreatefromgif,PNG用imagecreatefrompng……
if (imageistruecolor($im)) {
imagetruecolortopalette($im, false, 256);
//如果是真彩色图象,将真彩色图像转换为调色板图像
}
for ($i = 0; $i < imagecolorstotal($im);/*获得调色板中颜色的数目*/ $i++) {
//获得颜色i点的颜色值
$rgb = imagecolorsforindex($im, $i);
//获得颜色灰度值
$gray = round(0.229 * $rgb['red'] + 0.587 * $rgb['green'] + 0.114 * $rgb['blue']);
//设置i点颜色值
if ($gray <= 150){
$gray = $gray+50;
}else if ($gray <= 200){
$gray = $gray+40;
}else if ($gray <= 218){
$gray = $gray+30;
}else if ($gray <= 240){
$gray = $gray+10;
} else if ($gray <= 250){
$gray = $gray+5;
} else{
$gray = 255;
}
imagecolorset($im, $i, $gray, $gray, $gray);
}
header('Content-Type: image/jpeg');
//设置图片类型,同样如果想生成gif或者png图象,要替换成相应的头
imagejpeg($im);
//输出图象,如果是gif就用imagegif,如果是png就用imagepng……
imagedestroy($im);
//销毁图象,释放资源
图片转黑白
//转黑白
$image_path = "1.jpg";
// $image_path="2.png";
$im = imagecreatefromjpeg($image_path);
//如果是Gif用imagecreatefromgif,PNG用imagecreatefrompng……
if (imageistruecolor($im)) {
imagetruecolortopalette($im, false, 256);
//如果是真彩色图象,将真彩色图像转换为调色板图像
}
for ($i = 0; $i < imagecolorstotal($im);/*获得调色板中颜色的数目*/ $i++) {
$rgb = imagecolorsforindex($im, $i);//获得颜色i点的颜色值
$gray = round(0.229 * $rgb['red'] + 0.587 * $rgb['green'] + 0.114 * $rgb['blue']);
//获得颜色灰度值
if ($gray <= 128) $gray = 0; else $gray = 255;
imagecolorset($im, $i, $gray, $gray, $gray);//设置i点颜色值
}
header('Content-Type: image/jpeg');
//设置图片类型,同样如果想生成gif或者png图象,要替换成相应的头
imagejpeg($im);
//输出图象,如果是gif就用imagegif,如果是png就用imagepng……
imagedestroy($im);
//销毁图象,释放资源
本文地址:https://blog.csdn.net/wangzhae/article/details/107379850
下一篇: 批处理批量打开网页