Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16979 | Rev 17012 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 16979 Rev 17002
Línea 1... Línea 1...
1
<?php
1
<?php
2
namespace LeadersLinked\Library;
2
namespace LeadersLinked\Library;
Línea 3... Línea 3...
3
 
3
 
4
 
4
 
5
abstract class Image 
5
class Image 
-
 
6
{
-
 
7
    
-
 
8
    /**
-
 
9
     *
-
 
10
     * @var \LeadersLinked\Library\Image
-
 
11
     */
-
 
12
    private static $_instance;
-
 
13
    
-
 
14
    /**
-
 
15
     *
-
 
16
     * @var \LeadersLinked\Library\Storage
-
 
17
     */
6
{
18
    private $storage;
7
 
19
    
8
    /**
20
    /**
9
     * 
-
 
10
     * @param string $source
-
 
11
     * @param string $target_path
21
     * 
12
     * @param string $target_prefix
22
     * @var array
-
 
23
     */
13
     * @param array $sizes
24
    private $config;
14
     * @param string $sufix
25
    
-
 
26
    /**
15
     * @return boolean
27
     *
16
     */  
28
     * @param array $config
17
    /*
29
     */
18
    public  static function upload($source, $target_path, $target_prefix,  $sizes = [], $sufix = '')
-
 
19
    {
-
 
20
        
30
    private function __construct($config)
21
        try {
31
    {
22
            $data = file_get_contents($source);
-
 
23
            $img = imagecreatefromstring($data);
-
 
24
            
-
 
25
            if(!file_exists($target_path)) {
32
        $this->config = $config;
26
                mkdir($target_path, 0755, true);
-
 
27
            }
-
 
28
            
-
 
29
            if($img) {
-
 
30
                list($source_width, $source_height) = getimagesize($source);
-
 
31
                
-
 
32
                foreach($sizes as $size)
-
 
33
                {
-
 
34
                    list($target_width, $target_height) = explode('x', $size);
-
 
35
                    
-
 
36
                    $x = false;
-
 
37
                    if($x) {
-
 
38
                    $width_ratio    = $target_width / $source_width;
-
 
39
                    $height_ratio   = $target_height / $source_height;
-
 
40
                    if($width_ratio > $height_ratio) {
-
 
41
                        $resized_width = $target_width;
-
 
42
                        $resized_height = $source_height * $width_ratio;
-
 
43
                    } else {
-
 
44
                        $resized_height = $target_height;
-
 
45
                        $resized_width = $source_width * $height_ratio;
-
 
46
                    }
-
 
47
                    
-
 
48
                    $resized_width = round($resized_width);
-
 
49
                    $resized_height = round($resized_height);
-
 
50
                    
-
 
51
                    $offset_width = round(($target_width - $resized_width) / 2);
-
 
52
                    $offset_height = round(($target_height - $resized_height) / 2);
-
 
53
                    
-
 
54
                    
-
 
55
                    $new_image = imageCreateTrueColor($target_width, $target_height);
-
 
56
                    imageAlphaBlending($new_image, False);
-
 
57
                    imageSaveAlpha($new_image, True);
-
 
58
                    $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
-
 
59
                    imagefill($new_image, 0, 0, $transparent);
-
 
60
                    imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
-
 
61
                    }
-
 
62
                    
-
 
63
                    $width_ratio    = $target_width / $source_width;
-
 
64
                    $height_ratio   = $target_height / $source_height;
-
 
65
                    $radio = $width_ratio > $height_ratio ?  $height_ratio : $width_ratio;
-
 
66
                    
-
 
67
                    $resized_width = round($source_width * $radio);
-
 
68
                    $resized_height = round($source_height * $radio);
-
 
69
                    
-
 
70
                    $offset_width = round(($target_width - $resized_width) / 2);
-
 
71
                    $offset_height = round(($target_height - $resized_height) / 2);
-
 
72
                    
-
 
73
                    $new_image = imageCreateTrueColor($target_width, $target_height);
-
 
74
                    imageAlphaBlending($new_image, False);
-
 
75
                    imageSaveAlpha($new_image, True);
-
 
76
                    $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
-
 
77
                    imagefill($new_image, 0, 0, $transparent);
-
 
78
                    imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
-
 
79
                    
-
 
80
                    
-
 
81
                    $target = $target_path . DIRECTORY_SEPARATOR . $target_prefix  . $size . $sufix . '.png';
-
 
82
                    if(file_exists($target)) {
-
 
83
                        @unlink($target);
-
 
84
                    }
33
        $this->storage = Storage::getInstance($config);
85
                    
-
 
86
                    
34
    }
87
                    imagepng($new_image, $target);
35
    
88
                }
-
 
89
            }
36
    /**
90
            
-
 
91
            unlink($source);
37
     *
92
            
-
 
93
           return true;
38
     * @param array $config
94
            
39
     * @return \LeadersLinked\Library\Image
95
        } 
40
     */
96
        catch (\Throwable $e)
41
    public static function getInstance($config)
97
        {
42
    {
98
            error_log($e->getTraceAsString());
43
        if(self::$_instance == null) {
-
 
44
            self::$_instance = new Image($config);
-
 
45
        }
-
 
46
        
-
 
47
        return self::$_instance;
-
 
48
    }
-
 
49
    
-
 
50
    /**
99
            return false;
51
     * 
-
 
52
     * @return \LeadersLinked\Library\Storage
-
 
53
     */
-
 
54
    public function getStorage()
-
 
55
    {
-
 
56
        return $this->storage;
-
 
57
    }
Línea 100... Línea 58...
100
        }
58
    
101
    }*/
59
   
102
    
60
    
103
    /**
61
    /**
-
 
62
     * 
104
     * 
63
     * @param string $source
105
     * @param string $source
64
     * @param string $target_path
106
     * @param string $target_path
65
     * @param string $target_code
107
     * @param string $target_filename
66
     * @param string $target_filename
108
     * @param number $target_width
67
     * @param number $target_width
109
     * @param number $target_height
68
     * @param number $target_height
110
     * @param boolean $crop_to_dimensions
69
     * @param boolean $crop_to_dimensions
111
     * @param boolean $unlink_source
70
     * @param boolean $unlink_source
112
     * @return boolean
71
     * @return boolean
Línea 113... Línea 72...
113
     */
72
     */
114
    public  static function uploadImage($source, $target_path, $target_filename, $target_width, $target_height,  $crop_to_dimensions = true, $unlink_source = true )
73
    public function uploadImageChangeSize($source, $target_path, $target_code, $target_filename, $target_width, $target_height,  $crop_to_dimensions, $unlink_source)
115
    {
-
 
Línea 116... Línea 74...
116
        
74
    {
117
       
75
        
118
        
76
       
Línea -... Línea 77...
-
 
77
        
-
 
78
        try {
-
 
79
            $data = file_get_contents($source);
119
        try {
80
            
120
            $data = file_get_contents($source);
81
            if($unlink_source) {
Línea 121... Línea 82...
121
            $img = imagecreatefromstring($data);
82
                unlink($source);
122
            
83
            }
123
            if(!file_exists($target_path)) {
-
 
124
                mkdir($target_path, 0755, true);
84
            
125
            }
-
 
126
            
85
            $img = imagecreatefromstring($data);
127
            if($img) {
-
 
128
                list($source_width, $source_height) = getimagesize($source);
86
            
129
                
87
            
130
                if($crop_to_dimensions) {
88
            if($img) {
131
                    $width_ratio = $target_width / $source_width;
-
 
132
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
89
                list($source_width, $source_height) = getimagesize($source);
133
                    $height_ratio = $target_height / $source_height;
90
                
134
                    //print 'Height Ratio: ' . $height_ratio . '<br />';
91
                if($crop_to_dimensions) {
135
                    if($width_ratio > $height_ratio) {
92
                    $width_ratio = $target_width / $source_width;
136
                        //print 'Width Wins<br />';
93
                    $height_ratio = $target_height / $source_height;
137
                        $resized_width = $target_width;
-
 
138
                        $resized_height = $source_height * $width_ratio;
94
                    if($width_ratio > $height_ratio) {
139
                    } else {
95
                        $resized_width = $target_width;
140
                        //print 'Height Wins<br />';
-
 
141
                        $resized_height = $target_height;
96
                        $resized_height = $source_height * $width_ratio;
142
                        $resized_width = $source_width * $height_ratio;
97
                    } else {
143
                    }
98
                        $resized_height = $target_height;
144
                } else {
-
 
145
                    $width_ratio = $target_width / $source_width;
99
                        $resized_width = $source_width * $height_ratio;
146
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
-
 
147
                    $resized_width = $target_width;
100
                    }
148
                    $resized_height = $source_height * $width_ratio;
101
                } else {
149
                    //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';
-
 
150
                    if($resized_height > $target_height) {
-
 
151
                        $height_ratio = $target_height / $resized_height;
-
 
152
                        $resized_height = $target_height;
-
 
153
                        //print 'Height Ratio: ' . $height_ratio . '<br />';
-
 
154
                        $resized_width = $resized_width * $height_ratio;
-
 
155
                        //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';
-
 
156
                    }
-
 
157
                }
-
 
158
                /*
-
 
-
 
102
                    $width_ratio = $target_width / $source_width;
Línea 159... Línea 103...
159
                $width_ratio    = $target_width / $source_width;
103
                    $resized_width = $target_width;
160
                $height_ratio   = $target_height / $source_height;
104
                    $resized_height = $source_height * $width_ratio;
Línea 161... Línea 105...
161
                if($width_ratio > $height_ratio) {
105
                    if($resized_height > $target_height) {
Línea 176... Línea 120...
176
                $new_image = imageCreateTrueColor($target_width, $target_height);
120
                $new_image = imageCreateTrueColor($target_width, $target_height);
177
                imageAlphaBlending($new_image, False);
121
                imageAlphaBlending($new_image, False);
178
                imageSaveAlpha($new_image, True);
122
                imageSaveAlpha($new_image, True);
179
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
123
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
180
                imagefill($new_image, 0, 0, $transparent);
124
                imagefill($new_image, 0, 0, $transparent);
181
               // imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
-
 
182
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
125
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
Línea 183... Línea 126...
183
                
126
                
184
                    
127
                    
-
 
128
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
185
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
129
                imagepng($new_image, $temp_filename);
186
                if(file_exists($target)) {
130
                
187
                    @unlink($target);
131
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
188
                }
132
                unlink($temp_filename);
189
                    
133
                
190
                    
134
                
Línea 191... Línea -...
191
                imagepng($new_image, $target);
-
 
192
            }
135
                return $result;
193
            
136
            } else {
Línea 194... Línea -...
194
            if($unlink_source) {
-
 
195
                @unlink($source);
-
 
196
            }
137
            
197
            
138
                return true;
198
            return true;
139
            }
199
            
140
            
200
        }
141
        }
Línea 207... Línea 148...
207
    
148
    
208
    /**
149
    /**
209
     * 
150
     * 
210
     * @param string $source
151
     * @param string $source
-
 
152
     * @param string $target_path
211
     * @param string $target_path
153
     * @param string $target_code
-
 
154
     * @param string $target_filename
212
     * @param string $target_filename
155
     * @param boolean $unlink_source
213
     * @return boolean
156
     * @return boolean
214
     */
157
     */
215
    public  static function uploadFile($source, $target_path, $target_filename)
158
    public function uploadImageRaw($source, $target_path, $target_code, $target_filename, $unlink_source)
Línea 216... Línea 159...
216
    {
159
    {
217
        
160
        
218
        
-
 
219
        
-
 
220
        try {
161
        
221
            $data = file_get_contents($source);
162
        
222
            $img = imagecreatefromstring($data);
163
        try {
Línea -... Línea 164...
-
 
164
            $data = file_get_contents($source);
-
 
165
            if($unlink_source) {
223
            
166
                unlink($source);
224
            if(!file_exists($target_path)) {
167
            }
Línea -... Línea 168...
-
 
168
            
-
 
169
            $img = imagecreatefromstring($data);
-
 
170
 
225
                mkdir($target_path, 0755, true);
171
            if($img) {
226
            }
172
                list($source_width, $source_height) = getimagesize($source);
227
            
173
                
228
            if($img) {
174
                $target_width = $source_width;
229
                list($source_width, $source_height) = getimagesize($source);
175
                $target_height =  $source_height;
Línea 256... Línea 202...
256
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
202
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
257
                if(file_exists($target)) {
203
                if(file_exists($target)) {
258
                    @unlink($target);
204
                    @unlink($target);
259
                }
205
                }
Línea -... Línea 206...
-
 
206
                
-
 
207
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
Línea 260... Línea -...
260
                
-
 
261
                
-
 
262
                imagepng($new_image, $target);
-
 
263
            }
-
 
264
            
-
 
265
            unlink($source);
-
 
266
            
-
 
267
            return true;
-
 
268
            
-
 
269
        }
-
 
270
        catch (\Throwable $e)
-
 
271
        {
-
 
272
            error_log($e->getTraceAsString());
-
 
273
            return false;
-
 
274
        }
-
 
275
    }
-
 
276
    
-
 
277
    /**
-
 
278
     *
-
 
279
     * @param string $source
-
 
280
     * @param string $target_path
-
 
281
     * @param string $target_filename
-
 
282
     * @param number $target_width
-
 
283
     * @param number $target_height
-
 
284
     
-
 
285
     * @return boolean
-
 
286
     */
-
 
287
    public  static function uploadImageWithoutCompletationSize($source, $target_path, $target_filename, $target_width, $target_height)
-
 
288
    {
-
 
289
        
-
 
290
        
-
 
291
        
-
 
292
        try {
-
 
293
            $data = file_get_contents($source);
-
 
294
            $img = imagecreatefromstring($data);
-
 
295
            
-
 
296
            if(!file_exists($target_path)) {
-
 
297
                mkdir($target_path, 0755, true);
-
 
298
            }
-
 
299
            
-
 
300
            if($img) {
-
 
301
                list($source_width, $source_height) = getimagesize($source);
-
 
302
                
-
 
303
                $width_ratio    = $target_width / $source_width;
-
 
304
                $height_ratio   = $target_height / $source_height;
-
 
305
                if($width_ratio > $height_ratio) {
-
 
306
                    $resized_width = $target_width;
-
 
307
                    $resized_height = $source_height * $width_ratio;
-
 
308
                } else {
-
 
309
                    $resized_height = $target_height;
-
 
310
                    $resized_width = $source_width * $height_ratio;
-
 
311
                }
-
 
312
                
-
 
313
                $resized_width = round($resized_width);
-
 
314
                $resized_height = round($resized_height);
-
 
315
                
-
 
316
                
208
                imagepng($new_image, $temp_filename);
317
                
-
 
318
                $new_image = imageCreateTrueColor($resized_width, $resized_height);
209
                
319
                imageAlphaBlending($new_image, False);
-
 
320
                imageSaveAlpha($new_image, True);
-
 
321
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
-
 
Línea 322... Línea -...
322
                imagefill($new_image, 0, 0, $transparent);
-
 
323
                imageCopyResampled($new_image, $img , 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
-
 
324
                
210
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
325
                
-
 
326
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
-
 
327
                if(file_exists($target)) {
-
 
328
                    @unlink($target);
-
 
329
                }
211
                unlink($temp_filename);
Línea 330... Línea -...
330
                
-
 
Línea 331... Línea 212...
331
                
212
                
Línea 332... Línea 213...
332
                imagepng($new_image, $target);
213
                
333
            }
214
                return $result;
334
            
215
            }
335
            unlink($source);
216
            
336
            
217
            
337
            return true;
218
            return true;
338
            
219
            
339
        }
-
 
340
        catch (\Throwable $e)
-
 
341
        {
-
 
342
            error_log($e->getTraceAsString());
-
 
343
            return false;
-
 
344
        }
-
 
345
    }
-
 
346
 
-
 
347
    /**
-
 
348
     * 
-
 
349
     * @param string $path
-
 
350
     * @param string $filename
-
 
351
     * @return boolean
-
 
352
     */
-
 
353
    public static function delete($path, $filename)
-
 
354
    {
-
 
355
        try {
-
 
356
            if (is_dir($path)){
-
 
357
                if ($dh = opendir($path)) {
-
 
358
                    while (($file = readdir($dh)) !== false)
-
 
359
                    {
-
 
360
                        if($file == '.' || $file == '..') {
-
 
361
                            continue;
-
 
362
                        }
-
 
363
                        
-
 
364
                        if($file == $filename) {
-
 
365
                            unlink($path . DIRECTORY_SEPARATOR . $file);
-
 
366
                        }
-
 
367
                    }
-
 
368
                    closedir($dh);
-
 
369
                }
-
 
370
            }
-
 
371
        
-
 
372
            return true;
-
 
373
            
-
 
374
        }
-
 
375
        catch (\Throwable $e)
220
        }
376
        {
221
        catch (\Throwable $e)