mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-10-23 04:05:58 +00:00
:octocat: examples cleanup
This commit is contained in:
@@ -4,17 +4,77 @@
|
||||
* @author smiley <smiley@chillerlan.net>
|
||||
* @copyright 2020 smiley
|
||||
* @license MIT
|
||||
*
|
||||
* @noinspection PhpComposerExtensionStubsInspection, PhpIllegalPsrClassPathInspection
|
||||
*/
|
||||
|
||||
namespace chillerlan\QRCodeExamples;
|
||||
|
||||
use chillerlan\QRCode\{QRCode, QROptions};
|
||||
use chillerlan\QRCode\Common\EccLevel;
|
||||
use chillerlan\QRCode\Data\QRMatrix;
|
||||
use chillerlan\QRCode\Output\{QRGdImage, QRCodeOutputException};
|
||||
|
||||
require_once __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
$data = 'https://github.com';
|
||||
/*
|
||||
* Class definition
|
||||
*/
|
||||
|
||||
class QRImageWithLogo extends QRGdImage{
|
||||
|
||||
/**
|
||||
* @param string|null $file
|
||||
* @param string|null $logo
|
||||
*
|
||||
* @return string
|
||||
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
|
||||
*/
|
||||
public function dump(string $file = null, string $logo = null):string{
|
||||
// set returnResource to true to skip further processing for now
|
||||
$this->options->returnResource = true;
|
||||
|
||||
// of course you could accept other formats too (such as resource or Imagick)
|
||||
// i'm not checking for the file type either for simplicity reasons (assuming PNG)
|
||||
if(!is_file($logo) || !is_readable($logo)){
|
||||
throw new QRCodeOutputException('invalid logo');
|
||||
}
|
||||
|
||||
// there's no need to save the result of dump() into $this->image here
|
||||
parent::dump($file);
|
||||
|
||||
$im = imagecreatefrompng($logo);
|
||||
|
||||
// get logo image size
|
||||
$w = imagesx($im);
|
||||
$h = imagesy($im);
|
||||
|
||||
// set new logo size, leave a border of 1 module (no proportional resize/centering)
|
||||
$lw = ($this->options->logoSpaceWidth - 2) * $this->options->scale;
|
||||
$lh = ($this->options->logoSpaceHeight - 2) * $this->options->scale;
|
||||
|
||||
// get the qrcode size
|
||||
$ql = $this->matrix->size() * $this->options->scale;
|
||||
|
||||
// scale the logo and copy it over. done!
|
||||
imagecopyresampled($this->image, $im, ($ql - $lw) / 2, ($ql - $lh) / 2, 0, 0, $lw, $lh, $w, $h);
|
||||
|
||||
$imageData = $this->dumpImage();
|
||||
|
||||
if($file !== null){
|
||||
$this->saveToFile($imageData, $file);
|
||||
}
|
||||
|
||||
if($this->options->imageBase64){
|
||||
$imageData = $this->base64encode($imageData, 'image/'.$this->options->outputType);
|
||||
}
|
||||
|
||||
return $imageData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Runtime
|
||||
*/
|
||||
|
||||
$options = new QROptions([
|
||||
'version' => 5,
|
||||
@@ -31,11 +91,14 @@ $options = new QROptions([
|
||||
]);
|
||||
|
||||
$qrcode = new QRCode($options);
|
||||
$qrcode->addByteSegment($data);
|
||||
$qrcode->addByteSegment('https://github.com');
|
||||
|
||||
header('Content-type: image/png');
|
||||
|
||||
$qrOutputInterface = new QRImageWithLogo($options, $qrcode->getMatrix());
|
||||
|
||||
// dump the output, with an additional logo
|
||||
// the logo could also be supplied via the options, see the svgWithLogo example
|
||||
echo $qrOutputInterface->dump(null, __DIR__.'/octocat.png');
|
||||
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user