Skip to content

Commit

Permalink
Not found image
Browse files Browse the repository at this point in the history
If the original image not exist creates an error image
  • Loading branch information
tck committed Nov 3, 2014
1 parent dea5c04 commit d427528
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 50 deletions.
56 changes: 23 additions & 33 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
<property name="test.dir" value="${application.startdir}/test" />
<property name="vendor.dir" value="vendor" />

<property name="build.out.dir" value="data/build" />
<property name="build.out.dir" value="build" />
<property name="logs.out.dir" value="${build.out.dir}/logs" />
<property name="phpdox.out.dir" value="${build.out.dir}/phpdox/bin" />
<property name="phpunit.out.dir" value="${build.out.dir}/phpunit" />
<property name="phpcpd.out.dir" value="${build.out.dir}/phpcpd" />
<property name="pdepend.out.dir" value="${build.out.dir}/pdepend" />
<property name="phpcs.out.dir" value="${build.out.dir}/phpcs" />
<property name="phpmd.out.dir" value="${build.out.dir}/phpmd" />
<property name="phploc.out.dir" value="${build.out.dir}/phploc" />

<target name="build"
depends="basic, docs"
Expand All @@ -27,13 +22,8 @@

<delete dir="${build.out.dir}" />
<mkdir dir="${build.out.dir}" />
<mkdir dir="${phpunit.out.dir}" />
<mkdir dir="${logs.out.dir}" />
<mkdir dir="${phpdox.out.dir}" />
<mkdir dir="${phpcpd.out.dir}" />
<mkdir dir="${pdepend.out.dir}" />
<mkdir dir="${phpcs.out.dir}" />
<mkdir dir="${phpmd.out.dir}" />
<mkdir dir="${phploc.out.dir}" />

</target>

Expand Down Expand Up @@ -74,44 +64,44 @@
<exec executable="${vendor.dir}/bin/phpunit" logoutput="true" checkreturn="true">
<arg value="-c"/>
<arg value="${test.dir}/phpunit.xml"/>
<arg value="--coverage-clover"/>
<arg value="${application.startdir}/${phpunit.out.dir}/clover.xml"/>
<arg value="--coverage-html"/>
<arg value="${application.startdir}/${phpunit.out.dir}/coverage"/>
<arg value="${application.startdir}/${logs.out.dir}/coverage"/>
<arg value="--coverage-clover"/>
<arg value="${application.startdir}/${logs.out.dir}/coverage/index.xml"/>
<arg value="--log-junit"/>
<arg value="${application.startdir}/${phpunit.out.dir}/junit.xml"/>
<arg value="${application.startdir}/${logs.out.dir}/junit.xml"/>
</exec>

</target>

<target name="docs">

<exec executable="${vendor.dir}/bin/phpdox" logoutput="true" />

</target>

<target name="metrics"
description="Executes metrics tools">

<echo msg="Executing PHP copy/paste detector..." />
<exec command="${vendor.dir}/bin/phpcpd --log-pmd=${phpcpd.out.dir}/cpd.xml ${src.dir}"
logoutput="true" />
<exec command="phpcpd --log-pmd=../../${logs.out.dir}/cpd.xml ../../${src.dir}"
dir="${vendor.dir}/bin" logoutput="true" />

<echo msg="Executing PDepend..." />
<exec command="${vendor.dir}/bin/pdepend --jdepend-xml=${pdepend.out.dir}/jdepend.xml --summary-xml=${pdepend.out.dir}/jdepend-summary.xml --jdepend-chart=${pdepend.out.dir}/jdepend.svg --overview-pyramid=${pdepend.out.dir}/jdepend-pyramid.svg ${src.dir}"
logoutput="true" />
<exec command="pdepend --jdepend-xml=../../${logs.out.dir}/jdepend.xml --summary-xml=../../${logs.out.dir}/jdepend-summary.xml --jdepend-chart=../../${logs.out.dir}/jdepend.svg --overview-pyramid=../../${logs.out.dir}/jdepend-pyramid.svg ../../${src.dir}"
dir="${vendor.dir}/bin" logoutput="true" />

<echo msg="Executing PHP Code Sniffer..." />
<exec command="${vendor.dir}/bin/phpcs --standard=PSR2 -p -s --report-full=${phpcs.out.dir}/codesniffer-full.txt --report-summary=${phpcs.out.dir}/codesniffer-summary.txt --report-xml=${phpcs.out.dir}/codesniffer.xml --report-checkstyle=${phpcs.out.dir}/codesniffer-checkstyle.xml --report-gitblame=${phpcs.out.dir}/codesniffer-gitblame.txt ${src.dir}"
logoutput="true" />
<exec command="phpcs --standard=PSR2 -p -s --report-full=../../${logs.out.dir}/phpcs-full.txt --report-summary=../../${logs.out.dir}/phpcs-summary.txt --report-xml=../../${logs.out.dir}/phpcs.xml --report-checkstyle=../../${logs.out.dir}/checkstyle.xml --report-gitblame=../../${logs.out.dir}/phpcs-gitblame.txt ../../${src.dir}"
dir="${vendor.dir}/bin" logoutput="true" />

<echo msg="Executing PHP Mess Detector..." />
<exec command="${vendor.dir}/bin/phpmd ${src.dir} xml codesize,controversial,design,naming,unusedcode --reportfile ${phpmd.out.dir}/phpmd.xml"
logoutput="true" />
<exec command="phpmd ../../${src.dir} xml codesize,controversial,design,naming,unusedcode --reportfile ../../${logs.out.dir}/pmd.xml"
dir="${vendor.dir}/bin" logoutput="true" />

<echo msg="Executing PHP Loc..." />
<exec command="${vendor.dir}/bin/phploc --progress --log-xml=${phploc.out.dir}/phploc.xml --log-csv=${phploc.out.dir}/phploc.csv ${src.dir}"
logoutput="true" />
<exec command="phploc --progress --log-xml=../../${logs.out.dir}/phploc.xml --log-csv=../../${logs.out.dir}/phploc.csv ../../${src.dir}"
dir="${vendor.dir}/bin" logoutput="true" />

</target>

<target name="docs" depends="metrics">

<exec executable="${vendor.dir}/bin/phpdox" logoutput="true" />

</target>

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"imagine/imagine": "0.6.*"
},
"require-dev": {
"phpunit/phpunit": ">=3.7",
"phpunit/phpunit": "4.3.*",
"mikey179/vfsStream": "1.*",
"squizlabs/php_codesniffer": "1.*",
"phploc/phploc": "*",
"pdepend/pdepend": "1.1.0",
"phpmd/phpmd": "1.4.*",
"sebastian/phpcpd": "*",
"theseer/phpdox": "0.6.5"
"phploc/phploc": "2.*",
"pdepend/pdepend": "2.*",
"phpmd/phpmd": "2.*",
"sebastian/phpcpd": "2.*",
"theseer/phpdox": "0.7.*"
},
"autoload": {
"psr-0" : {
Expand Down
20 changes: 16 additions & 4 deletions phpdox.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
<phpdox xmlns="http://phpdox.net/config">
<project name="zf2-tckimageresizer" source="${basedir}/src" workdir="${basedir}/data/build/phpdox/bin">
<project name="zf2-tckimageresizer" source="${basedir}/src" workdir="${basedir}/build/phpdox/bin">
<collector publiconly="false">
<include mask="*.php"/>
<exclude mask="*Autoload.php"/>
<exclude mask="${basedir}/vendor/*"/>

<inheritance resolve="true" />
</collector>

<generator output="${basedir}/data/build/phpdox">
<build engine="html" enabled="true" output="docs"/>
<generator output="${basedir}/build/phpdox">
<enrich base="${basedir}/build/logs">
<source type="build" />
<source type="git" />
<source type="checkstyle" />
<source type="phpcs" />
<source type="pmd" />
<source type="phpunit" />
<source type="phploc" />
</enrich>

<build engine="html" enabled="true" output="api" />
</generator>
</project>
</phpdox>
</phpdox>
18 changes: 12 additions & 6 deletions src/TckImageResizer/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,35 @@ public function resizeAction()
. $this->params('file')
. '.' . $this->params('extension');

$targetExtension = $this->params('extension');
if (!file_exists($source)) {
return $this->notFoundAction();
$source = null;
$targetExtension .= '.404.png';
}

$target = $this->publicDirectory . '/processed/'
. $this->params('file')
. '.$' . $this->params('command')
. '.' . $this->params('extension');
. '.' . $targetExtension;

$this->getImageProcessing()->process($source, $target, $this->params('command'));
if ($source) {
$this->getImageProcessing()->process($source, $target, $this->params('command'));

} else {
$this->getImageProcessing()->process404($target, $this->params('command'));
}

$imageInfo = getimagesize($target);
$mimeType = $imageInfo['mime'];

$response = $this->getResponse();
$response->setContent(file_get_contents($target));
$response->setStatusCode($source ? 200 : 404);
$response
->getHeaders()
->addHeaderLine('Content-Transfer-Encoding', 'binary')
->addHeaderLine('Content-Type', $mimeType);

return $response;
}

}

5 changes: 5 additions & 0 deletions src/TckImageResizer/Service/CommandRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

use TckImageResizer\Exception\BadMethodCallException;

/**
* self defined command registry
*
* @package TckImageResizer
*/
class CommandRegistry
{
/**
Expand Down
128 changes: 127 additions & 1 deletion src/TckImageResizer/Service/ImageProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Imagine\Image\AbstractImagine;
use Imagine\Image\ImageInterface;
use Imagine\Image\Box;
use Imagine\Image\Point;
use Imagine\Image\Palette\RGB;
use TckImageResizer\Util\UrlSafeBase64;
use TckImageResizer\Exception\BadMethodCallException;

/**
Expand Down Expand Up @@ -92,6 +95,58 @@ public function process($source, $target, $commands)
$this->image->save($target);
}

/**
* Process command to create 404 image and save to target
*
* @param string $target
* @param string $commands
*
* @return void
*/
public function process404($target, $commands)
{
if (file_exists($target)) {
return;
}

$targetFolder = pathinfo($target, PATHINFO_DIRNAME);
if (!file_exists($targetFolder)) {
mkdir($targetFolder, 0777, true);
}

$text = 'Not found';
$backgroundColor = null;
$color = null;
$width = null;
$height = null;
foreach ($this->analyseCommands($commands) as $command) {
if ('thumb' === $command['command'] || 'resize' === $command['command']) {
$width = $command['params'][0];
$height = $command['params'][1];

} elseif ('404' === $command['command']) {
if (isset($command['params'][0]) && UrlSafeBase64::valid($command['params'][0])) {
$text = UrlSafeBase64::decode($command['params'][0]);
}
if (isset($command['params'][1])) {
$backgroundColor = $command['params'][1];
}
if (isset($command['params'][2])) {
$color = $command['params'][2];
}
if (isset($command['params'][3])) {
$width = $command['params'][3];
}
if (isset($command['params'][4])) {
$height = $command['params'][4];
}
}
}
$this->image404($text, $backgroundColor, $color, $width, $height);

$this->image->save($target);
}

/**
* Analyse commands string and returns array with command/params keys
*
Expand Down Expand Up @@ -267,5 +322,76 @@ protected function imageBlur($sigma = 1)

$this->image->effects()->blur($sigma);
}
}

/**
* Command image resize
*
* @param string $text
* @param string $backgroundColor
* @param string $color
* @param int $width
* @param int $height
*
* @return void
*/
protected function image404($text, $backgroundColor, $color, $width, $height)
{
$text = (string) $text;
$backgroundColor = (string) $backgroundColor;
$color = (string) $color;
$width = (int) $width;
$height = (int) $height;

if (strlen($backgroundColor) != 6 || !preg_match('![0-9abcdef]!i', $backgroundColor)) {
$backgroundColor = 'F8F8F8';
}
if (strlen($color) != 6 || !preg_match('![0-9abcdef]!i', $color)) {
$color = '777777';
}
if ($width <= 0) {
$width = 100;
}
if ($height <= 0) {
$height = 100;
}

$palette = new RGB();
$size = new Box($width, $height);
$this->image = $this->getImagineService()->create($size, $palette->color('#' . $backgroundColor, 0));

if ($text) {
$this->drawCenteredText($text, $color);
}
}

/**
* Draw centered text in current image
*
* @param string $text
* @param string $color
*
* @return void
*/
protected function drawCenteredText($text, $color)
{
$width = $this->image->getSize()->getWidth();
$height = $this->image->getSize()->getHeight();
$fontColor = $this->image->palette()->color('#' . $color);
$fontSize = 48;
$widthFactor = $width > 160 ? 0.8 : 0.9;
$heightFactor = $height > 160 ? 0.8 : 0.9;
do {
$font = $this->getImagineService()
->font(__DIR__ . '/../../../data/font/Roboto-Regular.ttf', $fontSize, $fontColor);
$fontBox = $font->box($text);
$fontSize = round($fontSize * 0.8);

} while ($fontSize > 5
&& ($width * $widthFactor < $fontBox->getWidth() || $height * $heightFactor < $fontBox->getHeight()));

$pointX = max(0, floor(($width - $fontBox->getWidth()) / 2));
$pointY = max(0, floor(($height - $fontBox->getHeight()) / 2));

$this->image->draw()->text($text, $font, new Point($pointX, $pointY));
}
}
Loading

0 comments on commit d427528

Please sign in to comment.