Skip to content

Commit

Permalink
Code format and improved exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Aug 6, 2019
1 parent 57821a3 commit 95e61ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
12 changes: 10 additions & 2 deletions docs-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@

require __DIR__ . '/vendor/autoload.php';

$docsGenerator = new IvoPetkov\DocsGenerator(__DIR__, ['/src']);
$docsGenerator->generateMarkdown(__DIR__ . '/docs/markdown');
$docsGenerator = new IvoPetkov\DocsGenerator(__DIR__);
$docsGenerator->addSourceDir('/src');
$docsGenerator->addExamplesDir('/examples');
$options = [
'showProtected' => false,
'showPrivate' => false
];
$docsGenerator->generateMarkdown(__DIR__ . '/docs/markdown', $options);
//$docsGenerator->generateHTML(__DIR__ . '/docs/html', $options);
//$docsGenerator->generateJSON(__DIR__ . '/docs/json');
62 changes: 26 additions & 36 deletions src/App/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,20 @@ public function __construct(\BearFramework\App $app)
{
$this->app = $app;
$this->app->routes
->add($this->internalPathPrefix . '*', function(\BearFramework\App\Request $request) {
$response = $this->getResponse($request);
if ($response !== null) {
return $response;
}
});
->add($this->internalPathPrefix . '*', function (\BearFramework\App\Request $request) {
$response = $this->getResponse($request);
if ($response !== null) {
return $response;
}
});

$this
->defineProperty('pathPrefix', [
'get' => function() {
return $this->internalPathPrefix;
},
'readonly' => true
])
;
->defineProperty('pathPrefix', [
'get' => function () {
return $this->internalPathPrefix;
},
'readonly' => true
]);
}

/**
Expand Down Expand Up @@ -263,7 +262,7 @@ public function getContent(string $filename, array $options = []): ?string
*/
public function getResponse(\BearFramework\App\Request $request): ?\BearFramework\App\Response
{
$parsePath = function($path) {
$parsePath = function ($path) {
if (strpos($path, $this->internalPathPrefix) !== 0) {
return null;
}
Expand Down Expand Up @@ -523,9 +522,7 @@ private function getImageSize(string $filename): array
$result = [(int) imagesx($sourceImage), (int) imagesy($sourceImage)];
imagedestroy($sourceImage);
}
} catch (\Exception $e) {

}
} catch (\Exception $e) { }
return $result;
}

Expand Down Expand Up @@ -622,9 +619,9 @@ private function resize(string $sourceFilename, string $destinationFilename, arr
}

if (!$sourceImage) {
throw new \InvalidArgumentException('Cannot read the source image (' . $sourceFilename . ')');
throw new \InvalidArgumentException('Cannot read the source image or unsupported format (' . $sourceFilename . ')');
}
$result = false;
$tempFilename = $this->app->data->getFilename('.temp/assets/resize' . uniqid());
try {
$resultImage = imagecreatetruecolor($width, $height);
imagealphablending($resultImage, false);
Expand All @@ -639,39 +636,33 @@ private function resize(string $sourceFilename, string $destinationFilename, arr
} else {
$resizedImageHeight = ceil($sourceHeight / $widthRatio);
}
$destinationX = - ($resizedImageWidth - $width) / 2;
$destinationY = - ($resizedImageHeight - $height) / 2;

$tempFilename = $this->app->data->getFilename('.temp/assets/resize' . uniqid());
$destinationX = -($resizedImageWidth - $width) / 2;
$destinationY = -($resizedImageHeight - $height) / 2;
if (imagecopyresampled($resultImage, $sourceImage, floor($destinationX), floor($destinationY), 0, 0, $resizedImageWidth, $resizedImageHeight, $sourceWidth, $sourceHeight)) {
if ($outputType === 'jpg') {
$result = imagejpeg($resultImage, $tempFilename, 100);
imagejpeg($resultImage, $tempFilename, 100);
} elseif ($outputType === 'png') {
$result = imagepng($resultImage, $tempFilename, 9);
imagepng($resultImage, $tempFilename, 9);
} elseif ($outputType === 'gif') {
$result = imagegif($resultImage, $tempFilename);
imagegif($resultImage, $tempFilename);
} elseif ($outputType === 'webp') {
$result = imagewebp($resultImage, $tempFilename, 100);
imagewebp($resultImage, $tempFilename, 100);
}
}
imagedestroy($resultImage);
} catch (\Exception $e) {

}
} catch (\Exception $e) { }
imagedestroy($sourceImage);
if ($result) {
if (is_file($tempFilename)) {
$e = null;
try {
copy($tempFilename, $destinationFilename);
} catch (\Exception $e) {

}
} catch (\Exception $e) { }
unlink($tempFilename);
if ($e !== null) {
throw $e;
}
} else {
throw new \Exception('Cannot save resized asset (' . $destinationFilename . ')');
throw new \Exception('Cannot resize image (' . $sourceFilename . ')');
}
}
}
Expand Down Expand Up @@ -1714,5 +1705,4 @@ private function getMimeType(string $filename)
}
return null;
}

}

0 comments on commit 95e61ba

Please sign in to comment.