Skip to content

Commit

Permalink
Require valid image type when setting
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Jun 25, 2016
1 parent 4638f11 commit af9b4f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ use Endroid\QrCode\QrCode;

$qrCode = new QrCode();
$qrCode
->setText("Life is too short to be generating QR codes")
->setText('Life is too short to be generating QR codes')
->setSize(300)
->setPadding(10)
->setErrorCorrection('high')
->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
->setLabel('My label')
->setLabel('Scan the code')
->setLabelFontSize(16)
->render()
->setImageType(QrCode::IMAGE_TYPE_PNG)
;

// now we can directly output the qrcode
header('Content-Type: '.$qrCode->getContentType());
$qrCode->render();

// or create a response object
$response = new Response($qrCode->get(), 200, array('Content-Type' => $qrCode->getContentType()));

```

![QR Code](http://endroid.nl/qrcode/Life%20is%20too%20short%20to%20be%20generating%20QR%20codes.png)
Expand Down
14 changes: 14 additions & 0 deletions src/Exceptions/ImageTypeInvalidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* (c) Jeroen van den Enden <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Endroid\QrCode\Exceptions;

class ImageTypeInvalidException extends \Exception
{
}
21 changes: 19 additions & 2 deletions src/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Endroid\QrCode\Exceptions\DataDoesntExistsException;
use Endroid\QrCode\Exceptions\FreeTypeLibraryMissingException;
use Endroid\QrCode\Exceptions\ImageFunctionFailedException;
use Endroid\QrCode\Exceptions\ImageTypeInvalidException;
use Endroid\QrCode\Exceptions\VersionTooLargeException;
use Endroid\QrCode\Exceptions\ImageSizeTooLargeException;
use Endroid\QrCode\Exceptions\ImageFunctionUnknownException;
Expand Down Expand Up @@ -289,13 +290,17 @@ public function getModuleSize()
* @param string $image_type Image type
*
* @return QrCode
*
* @throws ImageTypeInvalidException
*/
public function setImageType($image_type)
{
if (in_array($image_type, $this->image_types_available)) {
$this->image_type = $image_type;
if (!in_array($image_type, $this->image_types_available)) {
throw new ImageTypeInvalidException('QRCode: image type '.$image_type.' is invalid.');
}

$this->image_type = $image_type;

return $this;
}

Expand Down Expand Up @@ -792,6 +797,18 @@ public function render($filename = null, $format = 'png')
return $this;
}

/**
* Returns the content type corresponding to the image type.
*
* @return string
*/
public function getContentType()
{
$contentType = 'image/'.$this->image_type;

return $contentType;
}

/**
* Create QR Code and return its content.
*
Expand Down

0 comments on commit af9b4f3

Please sign in to comment.