Editor::cropImage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: egorov
5
 * Date: 29.08.2015
6
 * Time: 11:12
7
 */
8
namespace samsoncms\app\gallery;
9
10
/**
11
 * Image editor logic
12
 * @package samsoncms\app\gallery
13
 */
14
class Editor
15
{
16
    /**
17
     * Function to reduce code size in __async_edit method
18
     * @param resource $imageResource Image to crop
19
     * @return bool|resource Cropped image
20
     */
21
    public function cropImage($imageResource)
0 ignored issues
show
Coding Style introduced by
cropImage uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
22
    {
23
        /** @var int $imageTransparency Transparent color */
24
        $imageTransparency = imagecolorallocatealpha($imageResource, 255, 255, 255, 127);
25
        /** @var resource $rotatedImage Rotated image resource */
26
        $rotatedImage = imagerotate($imageResource, -($_POST['rotate']), $imageTransparency);
27
        /** @var resource $croppedImage Cropped image resource */
28
        $croppedImage = imagecrop($rotatedImage, array('x' => $_POST['crop_x'],
29
            'y' => $_POST['crop_y'],
30
            'width' => $_POST['crop_width'],
31
            'height' => $_POST['crop_height']));
32
        // Delete temp image resource
33
        imagedestroy($rotatedImage);
34
        // Return cropped image
35
        return $croppedImage;
36
    }
37
38
    /**
39
     * Same as cropImage() for transparent images
40
     * @param resource $imageResource Image to crop
41
     * @return bool|resource Cropped image
42
     */
43
    public function cropTransparentImage($imageResource)
0 ignored issues
show
Coding Style introduced by
cropTransparentImage uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
44
    {
45
        /** @var int $imageTransparency Transparent color */
46
        $imageTransparency = imagecolorallocatealpha($imageResource, 255, 255, 255, 127);
47
        /** @var resource $croppedImage Cropped image resource */
48
        $croppedImage = imagecreatetruecolor($_POST['crop_width'], $_POST['crop_height']);
49
        // Fill new image with transparent color
50
        imagefill($croppedImage, 0, 0, $imageTransparency);
51
        // Save Alpha chanel
52
        imagesavealpha($croppedImage, true);
53
        /** @var resource $rotatedImage Rotated image resource */
54
        $rotatedImage = imagerotate($imageResource, -($_POST['rotate']), $imageTransparency);
55
        // Copy rotated image to cropped one
56
        imagecopy(
57
            $croppedImage,
58
            $rotatedImage,
59
            0,
60
            0,
61
            $_POST['crop_x'],
62
            $_POST['crop_y'],
63
            $_POST['crop_width'],
64
            $_POST['crop_height']
65
        );
66
        // Delete temp image resource
67
        imagedestroy($rotatedImage);
68
        // Return result image
69
        return $croppedImage;
70
    }
71
}
72