You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.0 KiB

3 years ago
  1. <?php
  2. namespace App\Image;
  3. use Jcupitt\Vips\Image;
  4. use Jcupitt\Vips\Interpretation;
  5. class ImageProcessor
  6. {
  7. public function brightness($image, float $brightness = 1.0, float $saturation = 1.0, float $hue = 0.0)
  8. {
  9. $oldInterpretation = $image->interpretation;
  10. $hue %= 360;
  11. if ($hue < 0) {
  12. $hue = 360 + $hue;
  13. }
  14. if ($image->hasAlpha()) {
  15. $imageWithoutAlpha = $image->extract_band(0, ['n' => $image->bands - 1]);
  16. $alpha = $image->extract_band($image->bands - 1, ['n' => 1]);
  17. return $imageWithoutAlpha
  18. ->colourspace(Interpretation::LCH)
  19. ->linear([$brightness, $saturation, 1.0], [0.0, 0.0, $hue])
  20. ->colourspace($oldInterpretation)
  21. ->bandjoin($alpha);
  22. }
  23. return $image
  24. ->colourspace(Interpretation::LCH)
  25. ->linear([$brightness, $saturation, 1.0], [0.0, 0.0, $hue])
  26. ->colourspace($oldInterpretation);
  27. }
  28. public function process(string $filename, string $target, array $options = ["w" => null, "h" => null, "r" => null,"flip" => null , "canv" => null,"rotation" => null],$saveOptions = ['Q' => 100])
  29. {
  30. if (!isset($options['w']) && !isset($options['h'])) {
  31. $image = Image::thumbnail($filename, getimagesize($filename)[0]);
  32. }
  33. if (isset($options['r'])) {
  34. $rArray = explode(':', $options['r']);
  35. if (isset($options['w']) && !isset($options['h'])) {
  36. $options['h'] = $options['w'] * $rArray[1];
  37. $options['w'] = $options['w'] * $rArray[0];
  38. }
  39. if (!isset($options['w']) && isset($options['h'])) {
  40. $options['h'] = $options['h'] * $rArray[1];
  41. $options['w'] = $options['h'] * $rArray[0];
  42. }
  43. if (!isset($options['w']) && !isset($options['h'])) {
  44. $options['h'] = getimagesize($filename)[0] * $rArray[1];
  45. $options['w'] = getimagesize($filename)[0] * $rArray[0];
  46. }
  47. }
  48. if (isset($options['w']) && !isset($options['h'])) {
  49. $image = Image::thumbnail($filename, $options['w'], ['height' => getimagesize($filename)[1]]);
  50. }
  51. if (!isset($options['w']) && isset($options['h'])) {
  52. $image = Image::thumbnail($filename, getimagesize($filename)[0], ['height' => $options['h']]);
  53. }
  54. if (isset($options['w']) && isset($options['h']) && !($options['canv'] == true)) {
  55. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']);
  56. }
  57. if (isset($options['w']) && isset($options['h']) && $options['canv'] == true) {
  58. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h']]);
  59. $widthH = ($options['h'] - $image->height) / 2;
  60. $widthW = ($options['w'] - $image->width) / 2;
  61. $image = $image->embed(
  62. $widthW,
  63. $widthH,
  64. $options['w'],
  65. $options['h'],
  66. ['extend' => 'background', 'background' => 1024]
  67. );
  68. }
  69. if (isset($options['brightness']) || isset($options['saturation']) || isset($options['hue'])) {
  70. $image = $this->brightness($image, isset($options['brightness']) ? $options['brightness'] : 1.0, isset($options['saturation']) ? $options['saturation'] : 1.0, isset($options['hue']) ? $options['hue'] : 0.0);
  71. }
  72. if (isset($options['rotation'])) {
  73. $image = $image->rotate($options['rotation']);
  74. }
  75. if (isset($options['flip'])) {
  76. if ($options['flip'] == "h") {
  77. $image = $image->fliphor();
  78. }
  79. if ($options['flip'] == "v") {
  80. $image = $image->flipver();
  81. }
  82. if ($options['flip'] == "hv") {
  83. $image = $image->fliphor();
  84. $image = $image->flipver();
  85. }
  86. }
  87. $image->writeToFile($target,$saveOptions);
  88. return $target;
  89. }
  90. }