Differential D25209 Diff 716 src/applications/files/builtin/PhabricatorFilesComposeAvatarBuiltinFile.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/builtin/PhabricatorFilesComposeAvatarBuiltinFile.php
Show First 20 Lines • Show All 160 Lines • ▼ Show 20 Lines | private function composeImage($color, $image, $border) { | ||||||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||||||
$border_thickness = imagesetthickness($canvas, 64); | $border_thickness = imagesetthickness($canvas, 64); | ||||||||||||||||||||||||||||
if (!$border_thickness) { | if (!$border_thickness) { | ||||||||||||||||||||||||||||
throw new Exception( | throw new Exception( | ||||||||||||||||||||||||||||
pht('Failed to save builtin avatar image data (imagesetthickness).')); | pht('Failed to save builtin avatar image data (imagesetthickness).')); | ||||||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||||||
$image_rectangle = imagerectangle($canvas, 0, 0, 400, 400, $true_border); | $image_rectangle = imagerectangle($canvas, 0, 0, 400, 400, $true_border); | ||||||||||||||||||||||||||||
valerio.bozzolan: Interestingly here we are doing a totally-undocumented thing: the function `imagerectangle()`… | |||||||||||||||||||||||||||||
if (!$image_rectangle) { | if (!$image_rectangle) { | ||||||||||||||||||||||||||||
throw new Exception( | throw new Exception( | ||||||||||||||||||||||||||||
pht('Failed to save builtin avatar image data (imagerectangle).')); | pht('Failed to save builtin avatar image data (imagerectangle).')); | ||||||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||||||
$image_copy = imagecopy($canvas, $img, 0, 0, 0, 0, 400, 400); | $image_copy = imagecopy($canvas, $img, 0, 0, 0, 0, 400, 400); | ||||||||||||||||||||||||||||
if (!$image_copy) { | if (!$image_copy) { | ||||||||||||||||||||||||||||
throw new Exception( | throw new Exception( | ||||||||||||||||||||||||||||
pht('Failed to save builtin avatar image data (imagecopy).')); | pht('Failed to save builtin avatar image data (imagecopy).')); | ||||||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||||||
return PhabricatorImageTransformer::saveImageDataInAnyFormat( | return PhabricatorImageTransformer::saveImageDataInAnyFormat( | ||||||||||||||||||||||||||||
$canvas, | $canvas, | ||||||||||||||||||||||||||||
'image/png'); | 'image/png'); | ||||||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||||||
/** | |||||||||||||||||||||||||||||
* Convert a color from RGBA to a value usable in PHP-GD. | |||||||||||||||||||||||||||||
* Each RGB color should be expressed as an integer from 0 to 255. | |||||||||||||||||||||||||||||
* The Alpha Channel should be expressed as a float from 0 to 1. | |||||||||||||||||||||||||||||
* @param array $rgba array( int Red, int Green, int Blue, float Alpha ) | |||||||||||||||||||||||||||||
* @return int | |||||||||||||||||||||||||||||
*/ | |||||||||||||||||||||||||||||
private static function rgba2gd($rgba) { | private static function rgba2gd($rgba) { | ||||||||||||||||||||||||||||
// When working with a truecolor image, we can use bitwise operations | |||||||||||||||||||||||||||||
// https://www.php.net/manual/en/function.imagecolorallocate.php#49168 | |||||||||||||||||||||||||||||
$r = $rgba[0]; | $r = $rgba[0]; | ||||||||||||||||||||||||||||
$g = $rgba[1]; | $g = $rgba[1]; | ||||||||||||||||||||||||||||
$b = $rgba[2]; | $b = $rgba[2]; | ||||||||||||||||||||||||||||
$a = $rgba[3]; | $a = $rgba[3]; | ||||||||||||||||||||||||||||
Done Inline ActionsI tried to write down the input domain:
valerio.bozzolan: I tried to write down the input domain:
| $a | (1 - $a) * 255 |
|----------|---------|
|… | |||||||||||||||||||||||||||||
$a = (1 - $a) * 255; | $a = round(((1 - $a) * 255), 0); | ||||||||||||||||||||||||||||
Not Done Inline ActionsNote that the bitwise operator automatically casts to int. That is why the exception is raised, since we loss precision in some cases like 122.7. https://www.php.net/manual/en/language.operators.bitwise.php valerio.bozzolan: Note that the bitwise operator automatically casts to int. That is why the exception is raised… | |||||||||||||||||||||||||||||
Not Done Inline ActionsPersonally I would suggest something like this in order to explicit the most optimal type for the consequent bitwise operator (that works with integers): $a = (int) round(((1 - $a) * 255), 0); In any case, according to this page: https://www.php.net/manual/en/language.types.integer.php The proposed use of round($a, 0) seems the minimal change necessary to obtain an "integral float" that could be converted to int without any loss of precision, and so without any deprecation warning. ✅ So green light from me valerio.bozzolan: Personally I would suggest something like this in order to explicit the most optimal type for… | |||||||||||||||||||||||||||||
return ($a << 24) | ($r << 16) | ($g << 8) | $b; | return ($a << 24) | ($r << 16) | ($g << 8) | $b; | ||||||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||||||
private function newImageMap() { | private function newImageMap() { | ||||||||||||||||||||||||||||
$root = dirname(phutil_get_library_root('phabricator')); | $root = dirname(phutil_get_library_root('phabricator')); | ||||||||||||||||||||||||||||
$root = $root.'/resources/builtin/alphanumeric/'; | $root = $root.'/resources/builtin/alphanumeric/'; | ||||||||||||||||||||||||||||
$map = array(); | $map = array(); | ||||||||||||||||||||||||||||
▲ Show 20 Lines • Show All 270 Lines • Show Last 20 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0
Interestingly here we are doing a totally-undocumented thing: the function imagerectangle() should only accept identifiers created from imagecolorallocatealpha() but here we don't use it but a value received from our self-made rgba2gd().
Just to clarify I like this patch but I don't like the already existing source and I'm sorry for that.