imagefilter

(PHP 5, PHP 7, PHP 8)

imagefilter β€” ΠŸΡ€ΠΈΠΌΠ΅Π½ΡΠ΅Ρ‚ Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ ΠΊ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ

ОписаниС

function imagefilter(GdImage $image, int $filter, array|int|float|bool ...$args): bool

Ѐункция imagefilter() примСняСт Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ filter ΠΊ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ image.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.
filter

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ filter ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ значСния:

  • IMG_FILTER_NEGATE: Π˜Π½Π²Π΅Ρ€Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ всС Ρ†Π²Π΅Ρ‚Π° изобраТСния.
  • IMG_FILTER_GRAYSCALE: ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ Ρ†Π²Π΅Ρ‚Π° изобраТСния Π² Π³Ρ€Π°Π΄Π°Ρ†ΠΈΠΈ сСрого ΠΏΡƒΡ‚Ρ‘ΠΌ прСобразования ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚ красного, Π·Π΅Π»Ρ‘Π½ΠΎΠ³ΠΎ ΠΈ синСго Π² ΠΈΡ… сумму с ΡƒΡ‡Ρ‘Ρ‚ΠΎΠΌ вСсов, Ρ‚Π°ΠΊΠΈΡ… ΠΆΠ΅ ΠΊΠ°ΠΊ ΠΏΡ€ΠΈ вычислСнии яркости (Y') ΠΏΠΎ Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΈ REC.601. ΠΠ»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π» сохраняСтся. Для ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ с ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠΉ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΈΠ½ΠΎΠ³Π΄Π° отличаСтся ΠΈΠ·-Π·Π° ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π½Π°ΠΊΠ»Π°Π΄Ρ‹Π²Π°Π΅Ρ‚ ΠΏΠ°Π»ΠΈΡ‚Ρ€Π°.
  • IMG_FILTER_BRIGHTNESS: Π˜Π·ΠΌΠ΅Π½ΡΠ΅Ρ‚ ΡΡ€ΠΊΠΎΡΡ‚ΡŒ изобраТСния. Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ ΡΡ€ΠΊΠΎΡΡ‚ΡŒ ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°ΡŽΡ‚ Ρ‡Π΅Ρ€Π΅Π· ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ args. Π”ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½ яркости ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ значСния ΠΎΡ‚ -255 Π΄ΠΎ 255.
  • IMG_FILTER_CONTRAST: Π˜Π·ΠΌΠ΅Π½ΡΠ΅Ρ‚ ΠΊΠΎΠ½Ρ‚Ρ€Π°ΡΡ‚Π½ΠΎΡΡ‚ΡŒ изобраТСния. Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ контрастности ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°ΡŽΡ‚ Ρ‡Π΅Ρ€Π΅Π· ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ args.
  • IMG_FILTER_COLORIZE: Π’ΠΎ ΠΆΠ΅, Ρ‡Ρ‚ΠΎ ΠΈ IMG_FILTER_GRAYSCALE, Π·Π° ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ΠΌ Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ Ρ€Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ Ρ†Π²Π΅Ρ‚. АргумСнты args, arg2 ΠΈ arg3 ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°ΡŽΡ‚ ΠΊΠ°Π½Π°Π»Ρ‹ red, green, blue, Π° Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ arg4 устанавливаСт ΠΊΠ°Π½Π°Π» alpha. Π”ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΊΠ°Π½Π°Π»Π° ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ значСния ΠΎΡ‚ 0 Π΄ΠΎ 255.
  • IMG_FILTER_EDGEDETECT: Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ Π³Ρ€Π°Π½ΠΈΡ† для ΠΈΡ… подсвСтки.
  • IMG_FILTER_EMBOSS: ДобавляСт Ρ€Π΅Π»ΡŒΠ΅Ρ„.
  • IMG_FILTER_GAUSSIAN_BLUR: Π Π°Π·ΠΌΡ‹Π²Π°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠΌ Гаусса.
  • IMG_FILTER_SELECTIVE_BLUR: Π Π°Π·ΠΌΡ‹Π²Π°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅.
  • IMG_FILTER_MEAN_REMOVAL: Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ усрСднСниС для достиТСния эффСкта «эскиза».
  • IMG_FILTER_SMOOTH: Π”Π΅Π»Π°Π΅Ρ‚ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ Π±ΠΎΠ»Π΅Π΅ ΠΏΠ»Π°Π²Π½Ρ‹ΠΌΠΈ, Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΌΠ΅Π½Π΅Π΅ Ρ‡Ρ‘Ρ‚ΠΊΠΈΠΌ. Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ гладкости ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°ΡŽΡ‚ Ρ‡Π΅Ρ€Π΅Π· ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ args.
  • IMG_FILTER_PIXELATE: ΠŸΡ€ΠΈΠΌΠ΅Π½ΡΠ΅Ρ‚ эффСкт пиксСлирования. Π Π°Π·ΠΌΠ΅Ρ€ Π±Π»ΠΎΠΊΠ° ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°ΡŽΡ‚ Ρ‡Π΅Ρ€Π΅Π· ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ args, Π° Ρ€Π΅ΠΆΠΈΠΌ эффСкта пиксСлизации Ρ‡Π΅Ρ€Π΅Π· ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ arg2.
  • IMG_FILTER_SCATTER: ΠŸΡ€ΠΈΠΌΠ΅Π½ΡΠ΅Ρ‚ эффСкт рассСивания ΠΊ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ. Π‘ΠΈΠ»Ρƒ эффСкта ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΡΡŽΡ‚ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π°ΠΌΠΈ args ΠΈ arg2, Π° Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ arg3 ΠΏΠ΅Ρ€Π΅Π΄Π°ΡŽΡ‚ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΡ€ΠΈΠΌΠ΅Π½ΠΈΡ‚ΡŒ эффСкт Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΊ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½Ρ‹ΠΌ Ρ†Π²Π΅Ρ‚Π°ΠΌ пиксСлСй.
args

  • IMG_FILTER_BRIGHTNESS: Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ яркости.
  • IMG_FILTER_CONTRAST: Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ контрастности.
  • IMG_FILTER_COLORIZE: Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ красного ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π° Ρ†Π²Π΅Ρ‚Π°.
  • IMG_FILTER_SMOOTH: Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ сглаТивания.
  • IMG_FILTER_PIXELATE: Π Π°Π·ΠΌΠ΅Ρ€ Π±Π»ΠΎΠΊΠ° Π² пиксСлях.
  • IMG_FILTER_SCATTER: Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ ΡƒΠΌΠ΅Π½ΡŒΡˆΠ΅Π½ΠΈΡ эффСкта. Π—Π½Π°Ρ‡Π΅Π½ΠΈΡŽ нСльзя Π±Ρ‹Ρ‚ΡŒ большС ΠΈΠ»ΠΈ Ρ€Π°Π²Π½Ρ‹ΠΌ ΡƒΡ€ΠΎΠ²Π½ΡŽ добавлСния эффСкта, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ установили Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ arg2.
arg2

  • IMG_FILTER_COLORIZE: Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π·Π΅Π»Ρ‘Π½ΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π° Ρ†Π²Π΅Ρ‚Π°.
  • IMG_FILTER_PIXELATE: ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚, ΠΏΡ€ΠΈΠΌΠ΅Π½ΡΡ‚ΡŒ Π»ΠΈ ΡƒΡΠΎΠ²Π΅Ρ€ΡˆΠ΅Π½ΡΡ‚Π²ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ эффСкт пиксСлизации. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ равняСтся false.
  • IMG_FILTER_SCATTER: Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ добавлСния эффСкта.
arg3

  • IMG_FILTER_COLORIZE: Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ синСго ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π° Ρ†Π²Π΅Ρ‚Π°.
  • IMG_FILTER_SCATTER: ΠΠ΅ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ массив Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ индСксации Ρ†Π²Π΅Ρ‚Π° для примСнСния эффСкта.
arg4

  • IMG_FILTER_COLORIZE: ΠΠ»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π», Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΌΠ΅ΠΆΠ΄Ρƒ 0 ΠΈ 127. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 0 ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ Π½Π΅ΠΏΡ€ΠΎΠ·Ρ€Π°Ρ‡Π½ΠΎΡΡ‚ΡŒ, Π° 127 соотвСтствуСт Π°Π±ΡΠΎΠ»ΡŽΡ‚Π½ΠΎΠΉ прозрачности.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Ошибки

Ѐункция выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ ValueError, Ссли Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° sub ΠΈΠ»ΠΈ plus ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Ρ‚ ΠΊ ΠΏΠ΅Ρ€Π΅ΠΏΠΎΠ»Π½Π΅Π½ΠΈΡŽ ΠΈΠ»ΠΈ нСдополнСнию со Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ΠΌ IMG_FILTER_SCATTER для ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° filter.

Бписок измСнСний

ВСрсия ОписаниС
8.4.0 Ѐункция Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ ValueError, Ссли Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° sub ΠΈΠ»ΠΈ plus ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Ρ‚ ΠΊ ΠΏΠ΅Ρ€Π΅ΠΏΠΎΠ»Π½Π΅Π½ΠΈΡŽ ΠΈΠ»ΠΈ нСдополнСнию со Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ΠΌ IMG_FILTER_SCATTER для ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° filter.
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π» ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ gd-рСсурс (resource).
7.4.0 Π”ΠΎΠ±Π°Π²ΠΈΠ»ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΡƒ рассСивания β€” IMG_FILTER_SCATTER.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ налоТСния Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π° Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagefilter() с Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠΌ grayscale

<?php

$im
= imagecreatefrompng('dave.png');

if (
$im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
echo
'Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΎ ΠΊ градациям сСрого.';

imagepng($im, 'dave.png');
} else {
echo
'ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ.';
}

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ налоТСния Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagefilter() с Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠΌ яркости

<?php
$im
= imagecreatefrompng('sean.png');

if (
$im && imagefilter($im, IMG_FILTER_BRIGHTNESS, 20)) {
echo
'Π―Ρ€ΠΊΠΎΡΡ‚ΡŒ изобраТСния ΠΈΠ·ΠΌΠ΅Π½Π΅Π½Π°.';

imagepng($im, 'sean.png');

} else {
echo
'Π˜Π·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ ΡΡ€ΠΊΠΎΡΡ‚ΡŒ Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ.';
}

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #3 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ налоТСния Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagefilter() с Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠΌ colorize

<?php

$im
= imagecreatefrompng('philip.png');

/* R, G, B, so 0, 255, 0 is green */
if ($im && imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0)) {
echo
'Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΎ ΠΊ ΠΎΡ‚Ρ‚Π΅Π½ΠΊΠ°ΠΌ Π·Π΅Π»Π΅Π½ΠΎΠ³ΠΎ.';

imagepng($im, 'philip.png');
} else {
echo
'ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ.';
}

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #4 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ налоТСния Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagefilter() с Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠΌ negate

<?php

// ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ negate(), ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ получится пСрСнСсти Π² PHP
// Π±Π΅Π· Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imagefilter()
function negate($im)
{
if (
function_exists('imagefilter')) {
return
imagefilter($im, IMG_FILTER_NEGATE);
}

for (
$x = 0; $x < imagesx($im); ++$x) {
for (
$y = 0; $y < imagesy($im); ++$y) {
$index = imagecolorat($im, $x, $y);
$rgb = imagecolorsforindex($index);
$color = imagecolorallocate($im, 255 - $rgb['red'], 255 - $rgb['green'], 255 - $rgb['blue']);

imagesetpixel($im, $x, $y, $color);
}
}

return (
true);
}

$im = imagecreatefromjpeg('kalle.jpg');

if (
$im && negate($im)) {
echo
'Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈΠ½Π²Π΅Ρ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ.';

imagejpeg($im, 'kalle.jpg', 100);
} else {
echo
'Π˜Π½Π²Π΅Ρ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ.';
}

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #5 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ налоТСния Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagefilter() с Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠΌ pixelate

<?php

// Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° PHP Π»ΠΎΠ³ΠΎΡ‚ΠΈΠΏΠ°, Π½Π°ΠΌ Π½ΡƒΠΆΠ½ΠΎ 2 ΡˆΡ‚ΡƒΠΊΠΈ
// для сравнСния
$logo1 = imagecreatefrompng('./php.png');
$logo2 = imagecreatefrompng('./php.png');

// ΠŸΠΎΠ΄ΠΎΠΏΡ‹Ρ‚Π½Ρ‹ΠΉ экзСмпляр
$output = imagecreatetruecolor(imagesx($logo1) * 2, imagesy($logo1));

// ΠŸΡ€ΠΈΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ пиксСлирования ΠΊ ΠΊΠ°ΠΆΠ΄ΠΎΠΌΡƒ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ с Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ Π±Π»ΠΎΠΊΠ° Π² 3 пиксСля
imagefilter($logo1, IMG_FILTER_PIXELATE, 3);
imagefilter($logo2, IMG_FILTER_PIXELATE, 3, true);

// Π‘ΠΎΠ²ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ Ρ€Π°Π·Π»ΠΈΡ‡ΠΈΠΉ Π² Π²Ρ‹Ρ…ΠΎΠ΄Π½ΠΎΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ
imagecopy($output, $logo1, 0, 0, 0, 0, imagesx($logo1) - 1, imagesy($logo1) - 1);
imagecopy($output, $logo2, imagesx($logo2), 0, 0, 0, imagesx($logo2) - 1, imagesy($logo2) - 1);

// Π’Ρ‹Π²ΠΎΠ΄ Ρ€Π°Π·Π»ΠΈΡ‡ΠΈΠΉ
header('Content-Type: image/png');
imagepng($output);

?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°: imagefilter() pixelate

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #6 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ налоТСния Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagefilter() с Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠΌ scatter

<?php

// Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° изобраТСния
$logo = imagecreatefrompng('./php.png');

// ΠŸΡ€ΠΈΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΎΡ‡Π΅Π½ΡŒ мягкого эффСкта рассСивания ΠΊ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ
imagefilter($logo, IMG_FILTER_SCATTER, 3, 5);

// Π’Ρ‹Π²ΠΎΠ² изобраТСния с эффСктом рассСивания
header('Content-Type: image/png');
imagepng($logo);

?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°: imagefilter() scatter

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π° IMG_FILTER_SCATTER всСгда случайный.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imageconvolution() - НалоТСниС ΠΈΡΠΊΡ€ΠΈΠ²Π»ΡΡŽΡ‰Π΅ΠΉ ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ 3Ρ…3, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ коэффициСнт ΠΈ смСщСниС
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 24 notes

up
34
PanuWorld ΒΆ
19 years ago
The documentation misses the exact meaning and valid ranges of the arguments for ImageFilter(). According to the 5.2.0 sources the arguments are:
IMG_FILTER_BRIGHTNESS
-255 = min brightness, 0 = no change, +255 = max brightness

IMG_FILTER_CONTRAST
-100 = max contrast, 0 = no change, +100 = min contrast (note the direction!)

IMG_FILTER_COLORIZE
Adds (subtracts) specified RGB values to each pixel. The valid range for each color is -255...+255, not 0...255. The correct order is red, green, blue.
-255 = min, 0 = no change, +255 = max
This has not much to do with IMG_FILTER_GRAYSCALE.

IMG_FILTER_SMOOTH
Applies a 9-cell convolution matrix where center pixel has the weight arg1 and others weight of 1.0. The result is normalized by dividing the sum with arg1 + 8.0 (sum of the matrix).
any float is accepted, large value (in practice: 2048 or more) = no change

ImageFilter seem to return false if the argument(s) are out of range for the chosen filter.
up
15
martijn at martijnfrazer dot nl ΒΆ
12 years ago
I needed an especially strong blur effect today and had a hard time achieving adequate results with the built-in IMG_FILTER_GAUSSIAN_BLUR filter. In order to achieve the strength of the blur I required I had to repeat the filter up to  100 times, which took way too long to be acceptable.

After a bit of searching, I found this answer to be quite a good solution to this problem: http://stackoverflow.com/a/20264482

Based on that technique, I wrote the following generic function to achieve a very strong blur in a reasonable amount of processing:

<?php 

/**
 * Strong Blur
 *
 * @param resource $gdImageResource 
 * @param int $blurFactor optional 
 *  This is the strength of the blur
 *  0 = no blur, 3 = default, anything over 5 is extremely blurred
 * @return GD image resource
 * @author Martijn Frazer, idea based on http://stackoverflow.com/a/20264482
 */
function blur($gdImageResource, $blurFactor = 3)
{
  // blurFactor has to be an integer
  $blurFactor = round($blurFactor);
  
  $originalWidth = imagesx($gdImageResource);
  $originalHeight = imagesy($gdImageResource);

  $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));
  $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));

  // for the first run, the previous image is the original input
  $prevImage = $gdImageResource;
  $prevWidth = $originalWidth;
  $prevHeight = $originalHeight;

  // scale way down and gradually scale back up, blurring all the way
  for($i = 0; $i < $blurFactor; $i += 1)
  {    
    // determine dimensions of next image
    $nextWidth = $smallestWidth * pow(2, $i);
    $nextHeight = $smallestHeight * pow(2, $i);

    // resize previous image to next size
    $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);
    imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0, 
      $nextWidth, $nextHeight, $prevWidth, $prevHeight);

    // apply blur filter
    imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);

    // now the new image becomes the previous image for the next step
    $prevImage = $nextImage;
    $prevWidth = $nextWidth;
      $prevHeight = $nextHeight;
  }

  // scale back to original size and blur one more time
  imagecopyresized($gdImageResource, $nextImage, 
    0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);
  imagefilter($gdImageResource, IMG_FILTER_GAUSSIAN_BLUR);

  // clean up
  imagedestroy($prevImage);

  // return result
  return $gdImageResource;
}
?>
up
11
yoann at yoone dot eu ΒΆ
12 years ago
Here is an alternative to IMG_FILTER_COLORIZE filter, but taking the alpha parameter of each pixel in account.

<?php
function rgba_colorize($img, $color)
{
    imagesavealpha($img, true);
    imagealphablending($img, true);

    $img_x = imagesx($img);
    $img_y = imagesy($img);
    for ($x = 0; $x < $img_x; ++$x)
    {
        for ($y = 0; $y < $img_y; ++$y)
        {
            $rgba = imagecolorsforindex($img, imagecolorat($img, $x, $y));
            $color_alpha = imagecolorallocatealpha($img, $color[0], $color[1], $color[2], $rgba['alpha']);
            imagesetpixel($img, $x, $y, $color_alpha);
            imagecolordeallocate($img, $color_alpha);
        }
    }
}
?>
up
7
aiden dot mail at freemail dot hu ΒΆ
18 years ago
Function to change the transparency of a png image on the fly. Works only with PNG, and with a browser supporting alpha channel.
The function stretches the opacity-range of the image, so that the most opaque pixel(s) will be set to the given opacity. (Other opacity values in pixels are modified accordingly.)
Returns success or failure.

<?php
function filter_opacity( &$img, $opacity ) //params: image resource id, opacity in percentage (eg. 80)
        {
            if( !isset( $opacity ) )
                { return false; }
            $opacity /= 100;
            
            //get image width and height
            $w = imagesx( $img );
            $h = imagesy( $img );
            
            //turn alpha blending off
            imagealphablending( $img, false );
            
            //find the most opaque pixel in the image (the one with the smallest alpha value)
            $minalpha = 127;
            for( $x = 0; $x < $w; $x++ )
                for( $y = 0; $y < $h; $y++ )
                    {
                        $alpha = ( imagecolorat( $img, $x, $y ) >> 24 ) & 0xFF;
                        if( $alpha < $minalpha )
                            { $minalpha = $alpha; }
                    }
            
            //loop through image pixels and modify alpha for each
            for( $x = 0; $x < $w; $x++ )
                {
                    for( $y = 0; $y < $h; $y++ )
                        {
                            //get current alpha value (represents the TANSPARENCY!)
                            $colorxy = imagecolorat( $img, $x, $y );
                            $alpha = ( $colorxy >> 24 ) & 0xFF;
                            //calculate new alpha
                            if( $minalpha !== 127 )
                                { $alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha ); }
                            else
                                { $alpha += 127 * $opacity; }
                            //get the color index with new alpha
                            $alphacolorxy = imagecolorallocatealpha( $img, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
                            //set pixel with the new color + opacity
                            if( !imagesetpixel( $img, $x, $y, $alphacolorxy ) )
                                { return false; }
                        }
                }
            return true;
        }
?>

Example for use:

 <?php
  $image = imagecreatefrompng( "img.png" );
  filter_opacity( $image, 75 );
  header( "content-type: image/png" );
  imagepng( $image );
  imagedestroy( $image );
?>
up
4
ssttoo at gmail dot com ΒΆ
18 years ago
Here's a page that shows the different filters in action
http://www.phpied.com/image-fun-with-php-part-2/
Also shows some quick ways to do sepia.
up
4
cookie at backbone dot sk ΒΆ
13 years ago
a function to create nice vignette effect:

<?php

function vignette($im){
    global $width, $height;    
    $width = imagesx($im);
    $height = imagesy($im);

    function effect($x, $y, &$rgb){
        global $width, $height;    
            
        $sharp = 0.4; // 0 - 10 small is sharpnes,  
        $level = 0.7; // 0 - 1 small is brighter

        $l = sin(M_PI / $width * $x) * sin(M_PI / $height * $y);
        $l = pow($l, $sharp);
        
        $l = 1 - $level * (1 - $l);
        
        $rgb['red'] *= $l;
        $rgb['green'] *= $l;
        $rgb['blue'] *= $l;
    }

    for($x = 0; $x < imagesx($im); ++$x){
        for($y = 0; $y < imagesy($im); ++$y){   
            $index = imagecolorat($im, $x, $y);
            $rgb = imagecolorsforindex($im, $index);
            effect($x, $y, $rgb);
            $color = imagecolorallocate($im, $rgb['red'], $rgb['green'], $rgb['blue']);

            imagesetpixel($im, $x, $y, $color);   
        }
    }
    return(true);
}

$im = imagecreatefromjpeg('cars7_134.jpg');

if($im and vignette($im)){
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);
}else{
    echo 'Vignette failed.';
}
?>
up
2
kees at tweakers dot net ΒΆ
21 years ago
From what i have been able to find from this function, it accepts the following arguments:
        IMG_FILTER_NEGATE
        IMG_FILTER_GRAYSCALE
        IMG_FILTER_EDGEDETECT
        IMG_FILTER_GAUSSIAN_BLUR
        IMG_FILTER_SELECTIVE_BLUR
        IMG_FILTER_EMBOSS
        IMG_FILTER_MEAN_REMOVAL

The following arguments need one or more arguments.
        IMG_FILTER_SMOOTH, -1924.124
        IMG_FILTER_COLORIZE, -127.12, -127.98, 127
        IMG_FILTER_CONTRAST, -90
        IMG_FILTER_BRIGHTNESS, 98
        
I haven't tested them all, the names speak for themselves.
up
0
Manolo at msalsas dot com ΒΆ
12 years ago
filtertype is an integer. So if you want to use it as a variable and need to use, e.g. preg_match function you can do it in this way:

<?php
    $filter = IMG_FILTER_BRIGHTNESS;
    if( preg_match( '/^[0-9]{1,2}$/', $filter ) )
    {
        //Do something
    }
?>

The order of the filtertypes in this manual determines the number of each filter, from 0 to 11. E.g. IMG_FILTER_NEGATE=0.
up
0
martijn(97+1) at gmail dot com (solve math) ΒΆ
13 years ago
Simple pixelate function, just in case you are < 5.3

<?php
function pixelate(&$image, $pixelsize){
    $maxX = imagesx($image);
    $maxY = imagesy($image);
    $rad=floor($pixelsize/2);
    for($x=$rad;$x<$maxX;$x+=$pixelsize)
        for($y=$rad;$y<$maxY;$y+=$pixelsize){
            $color = imagecolorat($image, $x, $y);
            imagefilledrectangle ($image, $x-$rad, $y-$rad, $x+$pixelsize-1, $y+$pixelsize-1,$color);
        }
}
?>
up
0
hadrien dot jouet at grownseed dot net ΒΆ
14 years ago
For people looking to apply a 'multiply' effect on images like the one in Photoshop (generally b&w ones), you can achieve it with the IMG_FILTER_COLORIZE filter.

<?php
function multiplyColor(&$im, $color = array(255, 0, 0))
{
   //get opposite color
   $opposite = array(255 - $color[0], 255 - $color[1], 255 - $color[2]);

   //now we subtract the opposite color from the image
   imagefilter($im, IMG_FILTER_COLORIZE, -$opposite[0], -$opposite[1], -$opposite[2]);
}
?>
up
1
fananf at nerdshack dot com ΒΆ
19 years ago
If you're looking for fast sepia effect that can be used for on-the-fly thumbnails generation you can't use sophisticated functions. The faster and much better way than described by webmaster at qudi dot de in the note from 31-Jan-2006 is applying colorize filter AFTER grayscale.

<?php

(...)

imagefilter($yourimage, IMG_FILTER_GRAYSCALE); imagefilter($yourimage, IMG_FILTER_COLORIZE, 90, 60, 40); 

(...)

?>

I used (90,60,40) for my sepia after couple of tests, however, if you need darker or lighter just check what suits you best.
up
0
bushmakin stas (bushstas at mail dot ru) ΒΆ
15 years ago
a function to make all colors gray except the only one
i made it myself so the code is note so beautiful )

<?php
function imagecolorfilter($im){
    
    $height = imagesy($im);
    $width = imagesx($im);
    for($x=0; $x<$width; $x++){
        for($y=0; $y<$height; $y++){
            $rgb = ImageColorAt($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $c=($r+$g+$b)/3;
            
//if($g<$r || $g<$b+20){$r=$c;$g=$c; $b=$c;}//leaves only green 
//if($b<$r || $b<$g){$r=$c;$g=$c; $b=$c;}//only blue
if($r<$g+30 || $r<$b){$r=$c;$g=$c; $b=$c;}//only red
//if($r<$g-1 || $r>$g+60 || $b>$g-50){$r=$c;$g=$c; $b=$c;}//only yellow
            
            
imagesetpixel($im, $x, $y,imagecolorallocate($im, $r,$g,$b));
        }
    }
}

header ("Content-type: image/jpeg");
$im = imagecreatefromjpeg("image.jpg");
imagecolorfilter($im);
imagejpeg($im);
?>
up
1
vdepizzol at hotmail dot com ΒΆ
21 years ago
Examples using imagefilter():

<?php
$im = imagecreatefrompng('dave.png');
if ($im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
    echo 'Image converted to grayscale.';
    imagepng($im, 'dave.png');
} else {
    echo 'Conversion to grayscale failed.';
}

imagedestroy($im);
?>

/////////////////////////////

<?php
$im = imagecreatefrompng('sean.png');
if ($im && imagefilter($im, IMG_FILTER_BRIGHTNESS, 20)) {
    echo 'Image brightness changed.';
    imagepng($im, 'sean.png');
} else {
    echo 'Image brightness change failed.';
}

imagedestroy($im);
?>

/////////////////////////////

<?php
$im = imagecreatefrompng('philip.png');

/* R, G, B, so 0, 255, 0 is green */
if ($im && imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0)) {
    echo 'Image successfully shaded green.';
    imagepng($im, 'philip.png');
} else {
    echo 'Green shading failed.';
}

imagedestroy($im);
?>
up
0
michaeln no at spam associationsplus ca ΒΆ
18 years ago
Note: applying IMG_FILTER_EMBOSS to text and using in a customization to the CAPTCHA image script in phpBB or a project of your own is a very good way to stop OCR-ing bots from getting through. Embossed serif fonts are fairly easy for the human eye to understand but to an OCR script it is extremely difficult because it seems to give it the illusion of 3D. 

If you only allocate 2 or 3 colours in the image, it uses the background colour alot in the embossed text, which greatly contributes to this.

I made my own custom CAPTCHA script to stop phpBB post spam for a client site I was developing and I have gone from getting 2-3 new spam users created every day to zero.

Anything with the source code freely available out there right now is possible to be defeated by spammers once one of them stars sharing code with the other spammers, but if you run something at least someone custom, their bots will pass you over.
up
0
webmaster at designsbykosi dot info ΒΆ
19 years ago
This will only work if you have php5. For php4, you'll have to use the sepia function set webmaster at qudi dot de suggested.
up
0
nancy at hypertextdigital dot com ΒΆ
19 years ago
This routine was just what I was looking for, I wanted web admin users to be able to recolour their uploaded photos (to go with a news item) either a blue tint or sepia to match the appearance of other colours used on the website. 

Using a form with a select box containing the RGB values, I can give them the option of either of the two tints or no colourization at all, plus resize their images to the viewing size and a thumbnail image on the fly without having to use any other image editing software.
up
0
webmaster at qudi dot de ΒΆ
20 years ago
for a quick, ok-looking, sepia-effect (also in php4) I just use this little fellow, since a real implementation of sepia was just way too slow.

function pseudosepia(&$im,$percent){
      $sx=imagesx($im);
      $sy=imagesy($im);
      $filter=imagecreatetruecolor($sx,$sy);
      $c=imagecolorallocate($filter,100,50,50);
      imagefilledrectangle($filter,0,0,$sx,$sy,$c);
      imagecopymerge($im,$filter,0,0,0,0,$sx,$sy,$percent);
}
up
-1
mail at kavisiegel dot com ΒΆ
16 years ago
Searching for a way to easily change the color of the image, I tried IMG_FILTER_COLORIZE. I was unable to get the quality results I wanted. It turns out PHP's Colorize is the equivalent of Photoshop's "Linear Dodge" layer filter. 

Hue adjustments have always worked well for me, so I figured I could try with PHP.
This function is kind of slow on larger images, but on small images like what I'm using it for, the difference is trivial.

The script calculates the ratio or red, to green, to blue in the color provided, then scales the image appropriately... unfortunately, it does it pixel by pixel.

Here's a demo and comparison of this function, to photoshop's hue function, to PHP's colorize. http://img146.imageshack.us/img146/3167/imagefilterhuedemo.png

<?php
function imagefilterhue($im,$r,$g,$b){
    $rgb = $r+$g+$b;
    $col = array($r/$rgb,$b/$rgb,$g/$rgb);
    $height = imagesy($im);
    $width = imagesx($im);
    for($x=0; $x<$width; $x++){
        for($y=0; $y<$height; $y++){
            $rgb = ImageColorAt($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $newR = $r*$col[0] + $g*$col[1] + $b*$col[2];
            $newG = $r*$col[2] + $g*$col[0] + $b*$col[1];
            $newB = $r*$col[1] + $g*$col[2] + $b*$col[0];
            imagesetpixel($im, $x, $y,imagecolorallocate($im, $newR, $newG, $newB));
        }
    }
}
header ("Content-type: image/jpeg");
$im = imagecreatefromjpeg("test.jpg");

// Usage: Just as imagefilter(), except with no filtertype.
// imagefilterhue(resource $image, int $red, int $green , int $blue)
imagefilterhue($im,2,70,188);

// The equivalent with colorize, as tested in demo image: imagefilter($im, IMG_FILTER_COLORIZE, 2, 70, 188);

imagejpeg($im);
?>
up
-1
a php user at nowhere dot com ΒΆ
20 years ago
http://www.hudzilla.org/phpbook/read.php/11_2_15
for more detailed info, and some <i>arg</i> guidelines.
up
-1
shahilahmed4242 at gmail dot com ΒΆ
10 years ago
PHP Sepia Effect

  $myImage = imagecreatefromjpeg($f);
  imagefilter($myImage,IMG_FILTER_GRAYSCALE);
  imagefilter($myImage,IMG_FILTER_BRIGHTNESS,-30);
  imagefilter($myImage,IMG_FILTER_COLORIZE, 90, 55, 30);  
  header("Content-type: image/jpeg");
  imagejpeg($myImage );
  imagejpeg($myImage,$f);
  imagedestroy( $myImage );
up
-2
santibari at fibertel dot com ΒΆ
20 years ago
A colorize algorithm wich preserves color luminosity (i.e black 
will output black, and white will output white).
This works in PHP4 and is great for customizing interfaces 
dinamically.

<?php
function colorize($img_src,$img_dest, $r, $g, $b) 
{
if(!$im = imagecreatefromgif($img_src))
  return "Could not use image $img_src"; 
    
//We will create a monochromatic palette based on
//the input color
//which will go from black to white
//Input color luminosity: this is equivalent to the 
//position of the input color in the monochromatic
//palette
$lum_inp=round(255*($r+$g+$b)/765); //765=255*3

//We fill the palette entry with the input color at its 
//corresponding position

$pal[$lum_inp]['r']=$r;
$pal[$lum_inp]['g']=$g;
$pal[$lum_inp]['b']=$b;

//Now we complete the palette, first we'll do it to
//the black,and then to the white.

//FROM input to black
//===================
//how many colors between black and input
$steps_to_black=$lum_inp;        

//The step size for each component
if($steps_to_black)
 {
 $step_size_red=$r/$steps_to_black;    
 $step_size_green=$g/$steps_to_black;    
 $step_size_blue=$b/$steps_to_black;    
 }

for($i=$steps_to_black;$i>=0;$i--)
 {
 $pal[$steps_to_black-$i]['r']=$r-round($step_size_red*$i);
 $pal[$steps_to_black-$i]['g']=$g-round($step_size_green*$i);
 $pal[$steps_to_black-$i]['b']=$b-round($step_size_blue*$i);
 }

//From input to white:
//===================
//how many colors between input and white
$steps_to_white=255-$lum_inp;

if($steps_to_white)
  {
  $step_size_red=(255-$r)/$steps_to_white;    
  $step_size_green=(255-$g)/$steps_to_white;    
  $step_size_blue=(255-$b)/$steps_to_white;    
  }
 else
  $step_size_red=$step_size_green=$step_size_blue=0;

 //The step size for each component
 for($i=($lum_inp+1);$i<=255;$i++)
  {
  $pal[$i]['r']=$r + round($step_size_red*($i-$lum_inp));
  $pal[$i]['g']=$g + round($step_size_green*($i-$lum_inp));
  $pal[$i]['b']=$b + round($step_size_blue*($i-$lum_inp));
  }
//--- End of palette creation

//Now,let's change the original palette into the one we
//created
for($c = 0; $c < $palette_size; $c++)
 { 
 $col = imagecolorsforindex($im, $c);          
 $lum_src=round(255*($col['red']+$col['green']
                +$col['blue'])/765);
 $col_out=$pal[$lum_src];
 imagecolorset($im, $c, $col_out['r'], 
                                $col_out['g'],
                                $col_out['b']);
 }

 //save the image file
 imagepng($im,$img_dest);
 imagedestroy($im);
}//end function colorize
?>
up
-1
Padde ΒΆ
13 years ago
I played with IMG_FILTER_SMOOTH and tried some negative
values.

-1 to -7: looks like a mix of smoothness and edgedetect

-8: image seems to be completely broken

-9 and lower: kind of sharpening effect (-9 sharper than -10)

I think the sharpening effect in particular could be useful.
up
-2
jonathan dot gotti at gmail dot com ΒΆ
15 years ago
IMG_FILTER_COLORIZE doesn't seem to work on palette image, here's a way to achieve same result with palette image:

<?php
//$color is an array containing rvb infos (ie: array(255,80,0))
function paletteColorize($imgResource,array $color){
    $nbColors = imagecolorstotal($imgResource);
    for($i=0; $i<$nbColors; $i++){
        $c = array_values(imagecolorsforindex($imgRes,$i));
        for($y=0;$y<3;$y++)
            $c[$y] = max(0,min(255,$c[$y]+$color[$y]));
        imagecolorset($imgResource,$i,$c[0],$c[1],$c[2]);
    }
}
?>

Here's also a function that work on both truecolor and palette images that try to do something similar to greyscale with a given color
<?php
function colorScale($imgRes,array $color){
        imagefilter($imgRes,IMG_FILTER_GRAYSCALE);
        $color = self::_read_color($color);
        $luminance=($color[0]+$color[1]+$color[2])/3; // average luminance added by the color
        $brightnessCorrection = $luminance/3; // quantity of brightness to correct for each channel
        if( $luminance < 127 ){
            $brightnessCorrection -= 127/3; // color is dark so we have to negate the brightness correction 
        }
        if(! imageistruecolor($imgRes) ){
            $nbColors = imagecolorstotal($imgRes);
            for($i=0; $i<$nbColors; $i++){
                $c = array_values(imgagecolorsforindex($imgRes,$i));
                for($y=0;$y<3;$y++){
                    $c[$y] = max(0, min(255, $c[$y] + ($color[$y]-$luminance) + $brightnessCorrection) ); // parentheses just for better comprehension
                }
                imagecolorset($omgRes,$i,$c[0],$c[1],$c[2]);
            }
        }else{ // much easier with truecolor
            imagefilter($imgRes, IMG_FILTER_COLORIZE, $color[0]-$luminance, $color[1]-$luminance, $color[2]-$luminance);
            imagefilter($imgRes, IMG_FILTER_BRIGHTNESS, $brightnessCorrection);
        }
}
?>

with hope that someone will find this useful
up
-3
mail at pedrocandeias dot com ΒΆ
14 years ago
// With transparent PNG file you can colorize the "positive" items and stand the transparent has it is - Beta code

<?php
header('Content-Type: image/png');

$im = imagecreatefrompng('image.png');
$width = imagesx($im);
$height = imagesy($im);
$imn = imagecreatetruecolor($width, $height);
imagealphablending($imn,false);
$col=imagecolorallocatealpha($imn,255,255,255,127);
imagesavealpha($imn,true);
imagefilledrectangle($imn,0,0,$width,$height,$col);
imagealphablending($imn,true);
imagecopy($imn, $im, 0, 0, 0, 0, $width, $height);
imagefilter($imn, IMG_FILTER_NEGATE);

// FOR A TRANSPARENT PNG FILE WITH SOMETHING INSIDE, YOU CAN CHANGE THE COLOR HERE: I HAVE RGB: 0, 255, 0
imagefilter($imn, IMG_FILTER_COLORIZE, 0, 255, 0);

imagepng($imn);
imagedestroy($imn);

?>