Размытие gif картинок
Я думал брать цвета н-го количества пикселей и присваивать им среднее арифметическое.
Может можно как-то по-другому сделать. Буду рад любым советам.
Вот мой код
Код:
$img = imagecreatefromjpeg("123.jpg");
$width =imagesx($img);
$height =imagesy($img);
$step = "3";
$count = 0;
for($x=0;$x<$width;$x++)
{
for($y=0;$y<$height;$y++)
{
$count = 0;
$rgb = array('red'=>0,'green'=>0,'blue'=>0);
for($i=$x-$step; $i < $x+$step; $i++)
for($j=$y-$step; $j < $y+$step; $j++)
{
if( $i < 0 || $i > $width || $j < 0 || $j > $height)
continue;
$cur = imagecolorsforindex($img, imagecolorat($img, $x, $y));
$rgb['red']+=$cur['red'];
$rgb['green']+=$cur['green'];
$rgb['blue']+=$cur['blue'];
$count++;
}
$rgb['red']/=$count;
$rgb['green']/=$count;
$rgb['blue']/=$count;
imagesetpixel($img,$x,$y,imagecolorallocate($img,$rgb['red'],$rgb['green'],$rgb['blue']));
}
}
imagejpeg($img,"1234.jpg");
$width =imagesx($img);
$height =imagesy($img);
$step = "3";
$count = 0;
for($x=0;$x<$width;$x++)
{
for($y=0;$y<$height;$y++)
{
$count = 0;
$rgb = array('red'=>0,'green'=>0,'blue'=>0);
for($i=$x-$step; $i < $x+$step; $i++)
for($j=$y-$step; $j < $y+$step; $j++)
{
if( $i < 0 || $i > $width || $j < 0 || $j > $height)
continue;
$cur = imagecolorsforindex($img, imagecolorat($img, $x, $y));
$rgb['red']+=$cur['red'];
$rgb['green']+=$cur['green'];
$rgb['blue']+=$cur['blue'];
$count++;
}
$rgb['red']/=$count;
$rgb['green']/=$count;
$rgb['blue']/=$count;
imagesetpixel($img,$x,$y,imagecolorallocate($img,$rgb['red'],$rgb['green'],$rgb['blue']));
}
}
imagejpeg($img,"1234.jpg");
Если нет необходимости, например для экономии места на диске, динамически получать размытие, то можно использовать Автоматизацию в PhotoShop'e. File->Automate->Batch.
imagejpeg($img,"1234.jpg",5);
Matrices can be used for sharpening, blurring, edge detection, etc, ala Photoshop.
A sharpening example:
Код:
<?php
$sharpenMatrix = array(-1,-1,-1,-1,16,-1,-1,-1,-1);
$divisor = 8;
$offset = 0;
imageconvolution($myImage, $sharpenMatrix, $divisor, $offset);
?>
$sharpenMatrix = array(-1,-1,-1,-1,16,-1,-1,-1,-1);
$divisor = 8;
$offset = 0;
imageconvolution($myImage, $sharpenMatrix, $divisor, $offset);
?>
Below is some information on building different kinds of matrices. (If you have photoshop (or PSP, GIMP) you can test out your matrices before applying them in PHP)
http://loriweb.pair.com/8udf-basics.html (covers blurs)
[/quote]
нашёл на php.net в комментах.
Код:
function Blur(&$gdimg, $radius=0.5) {
// Taken from Torstein Hønsi's phpUnsharpMask (see phpthumb.unsharp.php)
$radius = round(max(0, min($radius, 50)) * 2);
if (!$radius) {
return false;
}
$w = ImageSX($gdimg);
$h = ImageSY($gdimg);
if ($imgBlur = ImageCreateTrueColor($w, $h)) {
// Gaussian blur matrix:
// 1 2 1
// 2 4 2
// 1 2 1
// Move copies of the image around one pixel at the time and merge them with weight
// according to the matrix. The same matrix is simply repeated for higher radii.
for ($i = 0; $i < $radius; $i++) {
ImageCopy ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left
ImageCopyMerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50.00000); // down right
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25.00000); // up right
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25.00000); // right
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20.00000); // up
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 16.666667); // down
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50.000000); // center
ImageCopy ($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h);
}
return true;
}
return false;
}
// Taken from Torstein Hønsi's phpUnsharpMask (see phpthumb.unsharp.php)
$radius = round(max(0, min($radius, 50)) * 2);
if (!$radius) {
return false;
}
$w = ImageSX($gdimg);
$h = ImageSY($gdimg);
if ($imgBlur = ImageCreateTrueColor($w, $h)) {
// Gaussian blur matrix:
// 1 2 1
// 2 4 2
// 1 2 1
// Move copies of the image around one pixel at the time and merge them with weight
// according to the matrix. The same matrix is simply repeated for higher radii.
for ($i = 0; $i < $radius; $i++) {
ImageCopy ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left
ImageCopyMerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50.00000); // down right
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25.00000); // up right
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25.00000); // right
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20.00000); // up
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 16.666667); // down
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50.000000); // center
ImageCopy ($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h);
}
return true;
}
return false;
}