Skip to content

Commit

Permalink
Allow URL logos
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed May 25, 2020
1 parent 322ade7 commit a44bae8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

namespace Endroid\QrCode\Exception;

class InvalidPathException extends QrCodeException
class InvalidFontException extends QrCodeException
{
}
10 changes: 2 additions & 8 deletions src/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Endroid\QrCode;

use BaconQrCode\Encoder\Encoder;
use Endroid\QrCode\Exception\InvalidPathException;
use Endroid\QrCode\Exception\InvalidFontException;
use Endroid\QrCode\Exception\UnsupportedExtensionException;
use Endroid\QrCode\Writer\WriterInterface;

Expand Down Expand Up @@ -200,12 +200,6 @@ public function getErrorCorrectionLevel(): ErrorCorrectionLevel

public function setLogoPath(string $logoPath): void
{
$logoPath = realpath($logoPath);

if (false === $logoPath || !is_file($logoPath)) {
throw new InvalidPathException('Invalid logo path: '.$logoPath);
}

$this->logoPath = $logoPath;
}

Expand Down Expand Up @@ -281,7 +275,7 @@ public function setLabelFontPath(string $labelFontPath): void
$resolvedLabelFontPath = (string) realpath($labelFontPath);

if (!is_file($resolvedLabelFontPath)) {
throw new InvalidPathException('Invalid label font path: '.$labelFontPath);
throw new InvalidFontException('Invalid label font path: '.$labelFontPath);
}

$this->labelFontPath = $resolvedLabelFontPath;
Expand Down
20 changes: 20 additions & 0 deletions src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
abstract class AbstractWriter implements WriterInterface
{
protected function getMimeType(string $path): string
{
if (false !== filter_var($path, FILTER_VALIDATE_URL)) {
return $this->getMimeTypeFromUrl($path);
}

return $this->getMimeTypeFromPath($path);
}

private function getMimeTypeFromUrl(string $url): string
{
$headers = get_headers($url, 1);

if (!is_array($headers) || !isset($headers['Content-Type'])) {
throw new InvalidLogoException(sprintf('Content type could not be determined for logo URL "%s"', $url));
}

return $headers['Content-Type'];
}

private function getMimeTypeFromPath(string $path): string
{
if (!function_exists('mime_content_type')) {
throw new MissingExtensionException('You need the ext-fileinfo extension to determine logo mime type');
Expand Down

0 comments on commit a44bae8

Please sign in to comment.