|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|
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: