Completed
Push — master ( f7e67b...61a2fb )
by Tim
11s
created

Classes/ViewHelpers/ImageViewHelper.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Override the ViewHelper with ratio.
4
 */
5
6
namespace HDNET\Focuspoint\ViewHelpers;
7
8
use HDNET\Focuspoint\Service\FocusCropService;
9
use TYPO3\CMS\Core\Resource\FileInterface;
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
12
/**
13
 * Override the ViewHelper with ratio.
14
 */
15
class ImageViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
16
{
17
    /**
18
     * Initialize ViewHelper arguments.
19
     */
20
    public function initializeArguments()
21
    {
22
        parent::initializeArguments();
23
        $this->registerArgument('ratio', 'string', 'Ratio of the image', false, '1:1');
24
        $this->registerArgument('realCrop', 'boolean', 'Crop the image in real', false, true);
25
        $this->registerArgument('additionalClassDiv', 'string', 'Additional class for focus point div', false, '');
26
    }
27
28
    /**
29
     * Resize a given image (if required) and renders the respective img tag.
30
     *
31
     * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
32
     *
33
     * @return string Rendered tag
34
     */
35
    public function render()
36
    {
37
        /** @var FocusCropService $service */
38
        $service = GeneralUtility::makeInstance(FocusCropService::class);
39
        $internalImage = null;
40
        try {
41
            $internalImage = $service->getViewHelperImage($this->arguments['src'], $this->arguments['image'], $this->arguments['treatIdAsReference']);
42
            if ($this->arguments['realCrop'] && $internalImage instanceof FileInterface) {
43
                $src = $service->getCroppedImageSrcByFile($internalImage, $this->arguments['ratio']);
0 ignored issues
show
$src is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
                $treatIdAsReference = false;
0 ignored issues
show
$treatIdAsReference is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
                $image = null;
46
            }
47
        } catch (\Exception $ex) {
48
            $this->arguments['realCrop'] = true;
49
        }
50
51
        try {
52
            parent::render();
53
        } catch (\Exception $ex) {
54
            return 'Missing image!';
55
        }
56
57
        if ($this->arguments['realCrop']) {
58
            return $this->tag->render();
59
        }
60
61
        // Ratio calculation
62
        if (null !== $internalImage) {
63
            $focusPointY = $internalImage->getProperty('focus_point_y');
64
            $focusPointX = $internalImage->getProperty('focus_point_x');
65
66
            $additionalClassDiv = 'focuspoint';
67
            if (!empty($this->arguments['additionalClassDiv'])) {
68
                $additionalClassDiv .= ' ' . $this->arguments['additionalClassDiv'];
69
            }
70
71
            $focusTag = '<div class="' . $additionalClassDiv . '" data-image-imageSrc="' . $this->tag->getAttribute('src') . '" data-focus-x="' . ($focusPointX / 100) . '" data-focus-y="' . ($focusPointY / 100) . '" data-image-w="' . $this->tag->getAttribute('width') . '" data-image-h="' . $this->tag->getAttribute('height') . '">';
72
73
            return $focusTag . $this->tag->render() . '</div>';
74
        } else {
75
            return 'Missing internal image!';
76
        }
77
    }
78
}
79