Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16979 | Rev 17012 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
namespace LeadersLinked\Library;
3
 
4
 
17002 efrain 5
class Image
1 www 6
{
17002 efrain 7
 
1 www 8
    /**
17002 efrain 9
     *
10
     * @var \LeadersLinked\Library\Image
11
     */
12
    private static $_instance;
13
 
14
    /**
15
     *
16
     * @var \LeadersLinked\Library\Storage
17
     */
18
    private $storage;
19
 
20
    /**
1 www 21
     *
17002 efrain 22
     * @var array
23
     */
24
    private $config;
25
 
26
    /**
27
     *
28
     * @param array $config
29
     */
30
    private function __construct($config)
1 www 31
    {
17002 efrain 32
        $this->config = $config;
33
        $this->storage = Storage::getInstance($config);
34
    }
35
 
36
    /**
37
     *
38
     * @param array $config
39
     * @return \LeadersLinked\Library\Image
40
     */
41
    public static function getInstance($config)
42
    {
43
        if(self::$_instance == null) {
44
            self::$_instance = new Image($config);
45
        }
1 www 46
 
17002 efrain 47
        return self::$_instance;
48
    }
1 www 49
 
50
    /**
51
     *
17002 efrain 52
     * @return \LeadersLinked\Library\Storage
53
     */
54
    public function getStorage()
55
    {
56
        return $this->storage;
57
    }
58
 
59
 
60
 
61
    /**
62
     *
1 www 63
     * @param string $source
64
     * @param string $target_path
17002 efrain 65
     * @param string $target_code
1 www 66
     * @param string $target_filename
67
     * @param number $target_width
68
     * @param number $target_height
69
     * @param boolean $crop_to_dimensions
16798 efrain 70
     * @param boolean $unlink_source
1 www 71
     * @return boolean
72
     */
17002 efrain 73
    public function uploadImageChangeSize($source, $target_path, $target_code, $target_filename, $target_width, $target_height,  $crop_to_dimensions, $unlink_source)
1 www 74
    {
75
 
76
 
77
 
78
        try {
79
            $data = file_get_contents($source);
80
 
17002 efrain 81
            if($unlink_source) {
82
                unlink($source);
1 www 83
            }
84
 
17002 efrain 85
            $img = imagecreatefromstring($data);
86
 
87
 
1 www 88
            if($img) {
89
                list($source_width, $source_height) = getimagesize($source);
90
 
91
                if($crop_to_dimensions) {
92
                    $width_ratio = $target_width / $source_width;
93
                    $height_ratio = $target_height / $source_height;
94
                    if($width_ratio > $height_ratio) {
95
                        $resized_width = $target_width;
96
                        $resized_height = $source_height * $width_ratio;
97
                    } else {
98
                        $resized_height = $target_height;
99
                        $resized_width = $source_width * $height_ratio;
100
                    }
101
                } else {
102
                    $width_ratio = $target_width / $source_width;
103
                    $resized_width = $target_width;
104
                    $resized_height = $source_height * $width_ratio;
105
                    if($resized_height > $target_height) {
106
                        $height_ratio = $target_height / $resized_height;
107
                        $resized_height = $target_height;
108
                        $resized_width = $resized_width * $height_ratio;
109
                    }
110
                }
17002 efrain 111
 
1 www 112
 
113
                $resized_width = round($resized_width);
114
                $resized_height = round($resized_height);
115
 
116
                $offset_width = round(($target_width - $resized_width) / 2);
117
                $offset_height = round(($target_height - $resized_height) / 2);
118
 
119
 
120
                $new_image = imageCreateTrueColor($target_width, $target_height);
121
                imageAlphaBlending($new_image, False);
122
                imageSaveAlpha($new_image, True);
123
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
124
                imagefill($new_image, 0, 0, $transparent);
125
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
126
 
127
 
17002 efrain 128
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
129
                imagepng($new_image, $temp_filename);
130
 
131
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
132
                unlink($temp_filename);
133
 
134
 
135
                return $result;
136
            } else {
1 www 137
 
17002 efrain 138
                return true;
16798 efrain 139
            }
1 www 140
 
141
        }
142
        catch (\Throwable $e)
143
        {
144
            error_log($e->getTraceAsString());
145
            return false;
146
        }
147
    }
148
 
149
    /**
150
     *
151
     * @param string $source
152
     * @param string $target_path
17002 efrain 153
     * @param string $target_code
1 www 154
     * @param string $target_filename
17002 efrain 155
     * @param boolean $unlink_source
1 www 156
     * @return boolean
157
     */
17002 efrain 158
    public function uploadImageRaw($source, $target_path, $target_code, $target_filename, $unlink_source)
1 www 159
    {
160
 
161
 
162
 
163
        try {
164
            $data = file_get_contents($source);
17002 efrain 165
            if($unlink_source) {
166
                unlink($source);
1 www 167
            }
168
 
17002 efrain 169
            $img = imagecreatefromstring($data);
170
 
1 www 171
            if($img) {
172
                list($source_width, $source_height) = getimagesize($source);
173
 
17002 efrain 174
                $target_width = $source_width;
175
                $target_height =  $source_height;
176
 
1 www 177
                $width_ratio    = $target_width / $source_width;
178
                $height_ratio   = $target_height / $source_height;
179
                if($width_ratio > $height_ratio) {
180
                    $resized_width = $target_width;
181
                    $resized_height = $source_height * $width_ratio;
182
                } else {
183
                    $resized_height = $target_height;
184
                    $resized_width = $source_width * $height_ratio;
185
                }
186
 
187
                $resized_width = round($resized_width);
188
                $resized_height = round($resized_height);
189
 
190
                $offset_width = round(($target_width - $resized_width) / 2);
191
                $offset_height = round(($target_height - $resized_height) / 2);
192
 
193
 
194
                $new_image = imageCreateTrueColor($target_width, $target_height);
195
                imageAlphaBlending($new_image, False);
196
                imageSaveAlpha($new_image, True);
197
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
198
                imagefill($new_image, 0, 0, $transparent);
199
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
200
 
201
 
202
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
203
                if(file_exists($target)) {
204
                    @unlink($target);
205
                }
206
 
17002 efrain 207
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
208
                imagepng($new_image, $temp_filename);
1 www 209
 
17002 efrain 210
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
211
                unlink($temp_filename);
15077 efrain 212
 
213
 
17002 efrain 214
                return $result;
15077 efrain 215
            }
216
 
217
 
218
            return true;
219
 
220
        }
221
        catch (\Throwable $e)
222
        {
223
            error_log($e->getTraceAsString());
224
            return false;
225
        }
226
    }
1 www 227
}