swatty007 /
faker-image-generator
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Swatty007\FakerImageGenerator\Providers; |
||
| 4 | |||
| 5 | use Faker\Generator; |
||
| 6 | use Faker\Provider\Base; |
||
| 7 | |||
| 8 | class FakerImageGenerationProvider extends Base |
||
| 9 | { |
||
| 10 | public string $directory; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 11 | public bool $fullPath; |
||
| 12 | |||
| 13 | public int $width; |
||
| 14 | public int $height; |
||
| 15 | public string $format; |
||
| 16 | |||
| 17 | public $backgroundColor; |
||
| 18 | |||
| 19 | public $text; |
||
| 20 | public $textColor; |
||
| 21 | public $fontFace; |
||
| 22 | |||
| 23 | public function __construct( |
||
| 24 | Generator $generator, |
||
| 25 | $directory = null, |
||
| 26 | $width = 640, |
||
| 27 | $height = 480, |
||
| 28 | $format = 'png', |
||
| 29 | $fullPath = true, |
||
| 30 | $text = true, |
||
| 31 | $backgroundColor = null, |
||
| 32 | $textColor = null, |
||
| 33 | $fontFace = null |
||
| 34 | ) { |
||
| 35 | // Allow our users to define a configuration for our entire instance. |
||
| 36 | // Or fallback to our default config values |
||
| 37 | $this->directory = $directory ?? config('faker-image-generator.directory'); |
||
| 38 | $this->width = $width ?? config('faker-image-generator.width'); |
||
| 39 | $this->height = $height ?? config('faker-image-generator.height'); |
||
| 40 | $this->format = $format ?? config('faker-image-generator.format'); |
||
| 41 | $this->fullPath = $fullPath ?? config('faker-image-generator.full_path'); |
||
| 42 | $this->text = $text ?? config('faker-image-generator.text'); |
||
| 43 | $this->backgroundColor = $backgroundColor ?? config('faker-image-generator.background_color'); |
||
| 44 | $this->textColor = $textColor ?? config('faker-image-generator.text_color'); |
||
| 45 | $this->fontFace = $fontFace ?? config('faker-image-generator.font_face'); |
||
| 46 | |||
| 47 | parent::__construct($generator); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Generate a new image to disk and return its location |
||
| 52 | * |
||
| 53 | * Requires gd (default in most PHP setup). |
||
| 54 | * |
||
| 55 | * @param null $directory |
||
| 56 | * @param null $width |
||
| 57 | * @param null $height |
||
| 58 | * @param null $format |
||
| 59 | * @param null $fullPath |
||
| 60 | * @param null $text |
||
| 61 | * @param null $backgroundColor |
||
| 62 | * @param null $textColor |
||
| 63 | * @param null $fontFace |
||
| 64 | * @return false|\RuntimeException|string |
||
| 65 | * @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' |
||
| 66 | */ |
||
| 67 | public function imageGenerator( |
||
| 68 | $directory = null, |
||
| 69 | $width = null, |
||
| 70 | $height = null, |
||
| 71 | $format = null, |
||
| 72 | $fullPath = null, |
||
| 73 | $text = null, |
||
| 74 | $backgroundColor = null, |
||
| 75 | $textColor = null, |
||
| 76 | $fontFace = null |
||
| 77 | ) { |
||
| 78 | // Allow our users to overwrite our settings per item, if they desire so |
||
| 79 | $directory = $directory ?? $this->directory; |
||
| 80 | $width = $width ?? $this->width; |
||
| 81 | $height = $height ?? $this->height; |
||
| 82 | $format = $format ?? $this->format; |
||
| 83 | $fullPath = $fullPath ?? $this->fullPath; |
||
| 84 | $text = $text ?? $this->text; |
||
| 85 | $backgroundColor = $backgroundColor ?? $this->backgroundColor; |
||
| 86 | $textColor = $textColor ?? $this->textColor; |
||
| 87 | $fontFace = $fontFace ?? $this->fontFace; |
||
| 88 | |||
| 89 | // Validate directory path |
||
| 90 | if (! is_dir($directory) || ! is_writable($directory)) { |
||
| 91 | throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $directory)); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Generate a random filename. Use the server address so that a file |
||
| 95 | // generated at the same time on a different server won't have a collision. |
||
| 96 | $name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)); |
||
| 97 | $filename = $name . '.' . $format; |
||
| 98 | $filepath = $directory . DIRECTORY_SEPARATOR . $filename; |
||
| 99 | |||
| 100 | // Create our Image, if our driver exists |
||
| 101 | if (function_exists('imagecreate')) { |
||
| 102 | $image = imagecreate($width, $height); |
||
| 103 | |||
| 104 | if ($backgroundColor) { |
||
| 105 | if (substr($backgroundColor, 0, 1) == '#') { |
||
| 106 | $rgb = str_split(substr($backgroundColor, 1), 2); |
||
| 107 | } else { |
||
| 108 | $rgb = str_split($backgroundColor, 2); |
||
| 109 | } |
||
| 110 | imagecolorallocate($image, hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2])); |
||
| 111 | } else { |
||
| 112 | imagecolorallocate($image, 0, 0, 0); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($text === true) { |
||
| 116 | $text = $width . 'x' . $height; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (! is_null($text)) { |
||
| 120 | if ($textColor) { |
||
| 121 | if (substr($textColor, 0, 1) == '#') { |
||
| 122 | $rgb = str_split(substr($textColor, 1), 2); |
||
| 123 | } else { |
||
| 124 | $rgb = str_split($textColor, 2); |
||
| 125 | } |
||
| 126 | $text_color = imagecolorallocate($image, hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2])); |
||
| 127 | } else { |
||
| 128 | $text_color = imagecolorallocate($image, 255, 255, 255); |
||
| 129 | } |
||
| 130 | |||
| 131 | $fontSize = 200; |
||
| 132 | $textBoundingBox = imagettfbbox($fontSize, 0, $fontFace, $text); |
||
| 133 | // decrease the default fonts size until it fits nicely within the image - Code adapted from https://github.com/img-src/placeholder |
||
| 134 | while (((($width - ($textBoundingBox[2] - $textBoundingBox[0])) < 10) || (($height - ($textBoundingBox[1] - $textBoundingBox[7])) < 10)) && ($fontSize > 1)) { |
||
| 135 | $fontSize --; |
||
| 136 | $textBoundingBox = imagettfbbox($fontSize, 0, $fontFace, $text); |
||
| 137 | } |
||
| 138 | imagettftext($image, $fontSize, 0, ($width / 2) - (($textBoundingBox[2] - $textBoundingBox[0]) / 2), ($height / 2) + (($textBoundingBox[1] - $textBoundingBox[7]) / 2), $text_color, $fontFace, $text); |
||
| 139 | } |
||
| 140 | |||
| 141 | switch (strtolower($format)) { |
||
| 142 | case 'jpg': |
||
| 143 | case 'jpeg': |
||
| 144 | default: |
||
| 145 | $success = imagejpeg($image, $filepath); |
||
| 146 | break; |
||
| 147 | case 'png': |
||
| 148 | $success = imagepng($image, $filepath); |
||
| 149 | } |
||
| 150 | |||
| 151 | $success = imagedestroy($image); |
||
| 152 | } else { |
||
| 153 | // @codeCoverageIgnoreStart |
||
| 154 | return new \RuntimeException('GD is not available on this PHP installation. Impossible to generate image.'); |
||
| 155 | // @codeCoverageIgnoreEnd |
||
| 156 | } |
||
| 157 | |||
| 158 | if (! $success) { |
||
| 159 | // @codeCoverageIgnoreStart |
||
| 160 | // could not save the file - fail silently. |
||
| 161 | return false; |
||
| 162 | // @codeCoverageIgnoreEnd |
||
| 163 | } |
||
| 164 | |||
| 165 | return $fullPath ? $filepath : $filename; |
||
| 166 | } |
||
| 167 | } |
||
| 168 |