Skip to content

Commit

Permalink
support phar
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Sep 28, 2022
1 parent 193eae2 commit 971db3f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/CaptchaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function build($width = 150, $height = 40, $font = null, $fingerprint = n
}

if ($font === null) {
$font = __DIR__ . '/Font/captcha'.$this->rand(0, 5).'.ttf';
$font = $this->getFontPath(__DIR__ . '/Font/captcha'.$this->rand(0, 5).'.ttf');
}

if (empty($this->backgroundImages)) {
Expand Down Expand Up @@ -490,6 +490,27 @@ public function build($width = 150, $height = 40, $font = null, $fingerprint = n
return $this;
}

/**
* @param $font
* @return string
*/
protected function getFontPath($font)
{
static $fontPathMap = [];
if (!\class_exists(\Phar::class, false) || !\Phar::running()) {
return $font;
}

$tmpPath = sys_get_temp_dir() ?: '/tmp';
$filePath = "$tmpPath/" . basename($font);
clearstatcache();
if (!isset($fontPathMap[$font]) || !is_file($filePath)) {
file_put_contents($filePath, file_get_contents($font));
$fontPathMap[$font] = $filePath;
}
return $fontPathMap[$font];
}

/**
* Distorts the image
*/
Expand Down

0 comments on commit 971db3f

Please sign in to comment.