Proyectos de Subversion LeadersLinked - Services

Rev

Rev 179 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 179 Rev 283
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
        
-
 
21
        try {
-
 
22
            $data = file_get_contents($source);
-
 
23
            $img = imagecreatefromstring($data);
-
 
24
            
-
 
25
            if(!file_exists($target_path)) {
-
 
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
                    }
-
 
85
                    
-
 
86
                    
-
 
87
                    imagepng($new_image, $target);
-
 
88
                }
-
 
89
            }
30
    private function __construct($config)
90
            
-
 
91
            unlink($source);
-
 
92
            
-
 
93
           return true;
-
 
94
            
-
 
95
        } 
-
 
96
        catch (\Throwable $e)
31
    {
97
        {
-
 
98
            error_log($e->getTraceAsString());
-
 
99
            return false;
32
        $this->config = $config;
Línea 100... Línea 33...
100
        }
33
        $this->storage = Storage::getInstance($config);
101
    }*/
34
    }
102
    
35
    
103
    /**
-
 
104
     *
36
    /**
105
     * @param string $source
-
 
106
     * @param string $target_path
-
 
107
     * @param string $target_filename
-
 
108
     * @param number $target_width
-
 
109
     * @param number $target_height
37
     *
110
 
38
     * @param array $config
111
     * @return boolean
39
     * @return \LeadersLinked\Library\Image
112
     */
-
 
113
    public  static function uploadImageWithoutCompletationSize($source, $target_path, $target_filename, $target_width, $target_height)
-
 
114
    {
-
 
115
        
-
 
116
        
-
 
117
        
-
 
118
        try {
-
 
119
            $data = file_get_contents($source);
40
     */
120
            $img = imagecreatefromstring($data);
-
 
121
            
-
 
122
            if(!file_exists($target_path)) {
-
 
123
                mkdir($target_path, 0755, true);
-
 
124
            }
-
 
125
            
-
 
126
            if($img) {
-
 
127
                list($source_width, $source_height) = getimagesize($source);
-
 
128
 
-
 
129
                 $width_ratio    = $target_width / $source_width;
-
 
130
                 $height_ratio   = $target_height / $source_height;
-
 
131
                 if($width_ratio > $height_ratio) {
-
 
132
                    $resized_width = $target_width;
-
 
133
                    $resized_height = $source_height * $width_ratio;
-
 
134
                 } else {
-
 
135
                    $resized_height = $target_height;
-
 
136
                    $resized_width = $source_width * $height_ratio;
-
 
137
                 }
-
 
138
                
-
 
139
                $resized_width = round($resized_width);
-
 
140
                $resized_height = round($resized_height);
-
 
141
 
-
 
142
                
-
 
143
                
-
 
144
                $new_image = imageCreateTrueColor($resized_width, $resized_height);
-
 
145
                imageAlphaBlending($new_image, False);
-
 
146
                imageSaveAlpha($new_image, True);
-
 
147
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
-
 
148
                imagefill($new_image, 0, 0, $transparent);
-
 
149
                imageCopyResampled($new_image, $img , 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
-
 
150
                
-
 
151
   
-
 
152
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
-
 
153
                if(file_exists($target)) {
-
 
154
                    @unlink($target);
-
 
155
                }
41
    public static function getInstance($config)
156
                
-
 
157
                
-
 
158
                imagepng($new_image, $target);
-
 
159
            }
-
 
160
            
-
 
161
            unlink($source);
-
 
162
            
-
 
163
            return true;
-
 
164
            
-
 
165
        }
-
 
166
        catch (\Throwable $e)
-
 
167
        {
42
    {
-
 
43
        if(self::$_instance == null) {
-
 
44
            self::$_instance = new Image($config);
168
            error_log($e->getTraceAsString());
45
        }
Línea 169... Línea 46...
169
            return false;
46
        
170
        }
47
        return self::$_instance;
-
 
48
    }
-
 
49
    
-
 
50
    /**
-
 
51
     * 
-
 
52
     * @return \LeadersLinked\Library\Storage
-
 
53
     */
-
 
54
    public function getStorage()
-
 
55
    {
-
 
56
        return $this->storage;
-
 
57
    }
-
 
58
    
171
    }
59
  
172
    
60
    
-
 
61
    /**
173
    /**
62
     * 
174
     * 
63
     * @param string $source
175
     * @param string $source
64
     * @param string $target_path
176
     * @param string $target_path
65
     * @param string $target_code
-
 
66
     * @param string $target_filename
177
     * @param string $target_filename
67
     * @param number $target_width
178
     * @param number $target_width
68
     * @param number $target_height
179
     * @param number $target_height
69
     * @param boolean $crop_to_dimensions
180
     * @param boolean $crop_to_dimensions
70
     * @param boolean $unlink_source
Línea 181... Línea 71...
181
     * @return boolean
71
     * @return boolean
182
     */
72
     */
-
 
73
    public function uploadImageChangeSize($source, $target_path, $target_code, $target_filename, $target_width, $target_height,  $crop_to_dimensions, $unlink_source)
-
 
74
    {
-
 
75
        
-
 
76
       
183
    public  static function uploadImage($source, $target_path, $target_filename, $target_width, $target_height,  $crop_to_dimensions = true )
77
        
Línea 184... Línea -...
184
    {
-
 
185
        
-
 
186
       
-
 
Línea 187... Línea 78...
187
        
78
        try {
188
        try {
79
            $data = file_get_contents($source);
Línea 189... Línea 80...
189
            $data = file_get_contents($source);
80
            if($unlink_source) {
190
            $img = imagecreatefromstring($data);
81
                unlink($source);
191
            
-
 
192
            if(!file_exists($target_path)) {
82
            }
193
                mkdir($target_path, 0755, true);
-
 
194
            }
83
            
195
            
-
 
196
            if($img) {
84
            $img = imagecreatefromstring($data);
197
                list($source_width, $source_height) = getimagesize($source);
85
            
198
                
86
            
199
                if($crop_to_dimensions) {
-
 
200
                    $width_ratio = $target_width / $source_width;
87
            if($img) {
201
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
88
                list($source_width, $source_height) = getimagesize($source);
202
                    $height_ratio = $target_height / $source_height;
89
                
203
                    //print 'Height Ratio: ' . $height_ratio . '<br />';
90
                if($crop_to_dimensions) {
204
                    if($width_ratio > $height_ratio) {
91
                    $width_ratio = $target_width / $source_width;
205
                        //print 'Width Wins<br />';
-
 
206
                        $resized_width = $target_width;
92
                    $height_ratio = $target_height / $source_height;
207
                        $resized_height = $source_height * $width_ratio;
93
                    if($width_ratio > $height_ratio) {
208
                    } else {
-
 
209
                        //print 'Height Wins<br />';
94
                        $resized_width = $target_width;
210
                        $resized_height = $target_height;
95
                        $resized_height = $source_height * $width_ratio;
211
                        $resized_width = $source_width * $height_ratio;
96
                    } else {
212
                    }
-
 
213
                } else {
97
                        $resized_height = $target_height;
214
                    $width_ratio = $target_width / $source_width;
-
 
215
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
98
                        $resized_width = $source_width * $height_ratio;
216
                    $resized_width = $target_width;
99
                    }
217
                    $resized_height = $source_height * $width_ratio;
-
 
218
                    //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';
-
 
219
                    if($resized_height > $target_height) {
-
 
220
                        $height_ratio = $target_height / $resized_height;
-
 
221
                        $resized_height = $target_height;
-
 
222
                        //print 'Height Ratio: ' . $height_ratio . '<br />';
-
 
223
                        $resized_width = $resized_width * $height_ratio;
-
 
224
                        //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';
-
 
225
                    }
-
 
226
                }
-
 
-
 
100
                } else {
Línea 227... Línea 101...
227
                /*
101
                    $width_ratio = $target_width / $source_width;
228
                $width_ratio    = $target_width / $source_width;
102
                    $resized_width = $target_width;
Línea 229... Línea 103...
229
                $height_ratio   = $target_height / $source_height;
103
                    $resized_height = $source_height * $width_ratio;
Línea 245... Línea 119...
245
                $new_image = imageCreateTrueColor($target_width, $target_height);
119
                $new_image = imageCreateTrueColor($target_width, $target_height);
246
                imageAlphaBlending($new_image, False);
120
                imageAlphaBlending($new_image, False);
247
                imageSaveAlpha($new_image, True);
121
                imageSaveAlpha($new_image, True);
248
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
122
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
249
                imagefill($new_image, 0, 0, $transparent);
123
                imagefill($new_image, 0, 0, $transparent);
250
               // imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
-
 
251
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
124
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
Línea 252... Línea 125...
252
                
125
                
253
                    
126
                    
-
 
127
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
254
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
128
                imagepng($new_image, $temp_filename);
255
                if(file_exists($target)) {
129
                
256
                    @unlink($target);
130
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
257
                }
131
                unlink($temp_filename);
258
                    
132
                
259
                    
133
                
260
                imagepng($new_image, $target);
-
 
261
            }
-
 
Línea 262... Línea 134...
262
            
134
                return $result;
-
 
135
            } else {
Línea 263... Línea 136...
263
            unlink($source);
136
            
264
            
137
                return true;
265
            return true;
138
            }
266
            
139
            
Línea 274... Línea 147...
274
    
147
    
275
    /**
148
    /**
276
     * 
149
     * 
277
     * @param string $source
150
     * @param string $source
-
 
151
     * @param string $target_path
278
     * @param string $target_path
152
     * @param string $target_code
-
 
153
     * @param string $target_filename
279
     * @param string $target_filename
154
     * @param boolean $unlink_source
280
     * @return boolean
155
     * @return boolean
281
     */
156
     */
282
    public  static function uploadFile($source, $target_path, $target_filename)
157
    public function uploadImageRaw($source, $target_path, $target_code, $target_filename, $unlink_source)
Línea 283... Línea 158...
283
    {
158
    {
284
        
159
        
285
        
-
 
286
        
-
 
287
        try {
160
        
288
            $data = file_get_contents($source);
161
        
289
            $img = imagecreatefromstring($data);
162
        try {
Línea -... Línea 163...
-
 
163
            $data = file_get_contents($source);
-
 
164
            if($unlink_source) {
290
            
165
                unlink($source);
291
            if(!file_exists($target_path)) {
166
            }
Línea 292... Línea 167...
292
                mkdir($target_path, 0755, true);
167
            
293
            }
168
            $img = imagecreatefromstring($data);
Línea 326... Línea 201...
326
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
201
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
327
                if(file_exists($target)) {
202
                if(file_exists($target)) {
328
                    @unlink($target);
203
                    @unlink($target);
329
                }
204
                }
Línea -... Línea 205...
-
 
205
                
-
 
206
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
-
 
207
                imagepng($new_image, $temp_filename);
-
 
208
                
-
 
209
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
-
 
210
                unlink($temp_filename);
Línea 330... Línea 211...
330
                
211
                
331
                
212
                
Línea 332... Línea -...
332
                imagepng($new_image, $target);
-
 
Línea 333... Línea 213...
333
            }
213
                return $result;
Línea 334... Línea 214...
334
            
214
            }
335
            unlink($source);
215
            
336
            
216
            
337
            return true;
217
            return true;
338
            
218
            
339
        }
219
        }
340
        catch (\Throwable $e)
220
        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)
-
 
376
        {
-
 
377
            error_log($e->getTraceAsString());
221
        {
378
            return false;
222
            error_log($e->getTraceAsString());