Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15077 | Rev 16979 | 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
 
5
abstract class Image
6
{
7
 
8
    /**
9
     *
10
     * @param string $source
11
     * @param string $target_path
12
     * @param string $target_prefix
13
     * @param array $sizes
14
     * @param string $sufix
15
     * @return boolean
16
     */
17
    /*
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);
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
            }
90
 
91
            unlink($source);
92
 
93
           return true;
94
 
95
        }
96
        catch (\Throwable $e)
97
        {
98
            error_log($e->getTraceAsString());
99
            return false;
100
        }
101
    }*/
102
 
103
    /**
104
     *
105
     * @param string $source
106
     * @param string $target_path
107
     * @param string $target_filename
108
     * @param number $target_width
109
     * @param number $target_height
110
     * @param boolean $crop_to_dimensions
16798 efrain 111
     * @param boolean $unlink_source
1 www 112
     * @return boolean
113
     */
16798 efrain 114
    public  static function uploadImage($source, $target_path, $target_filename, $target_width, $target_height,  $crop_to_dimensions = true, $unlink_source = true )
1 www 115
    {
116
 
117
 
118
 
119
        try {
120
            $data = file_get_contents($source);
121
            $img = imagecreatefromstring($data);
122
 
123
            if(!file_exists($target_path)) {
124
                mkdir($target_path, 0755);
125
            }
126
 
127
            if($img) {
128
                list($source_width, $source_height) = getimagesize($source);
129
 
130
                if($crop_to_dimensions) {
131
                    $width_ratio = $target_width / $source_width;
132
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
133
                    $height_ratio = $target_height / $source_height;
134
                    //print 'Height Ratio: ' . $height_ratio . '<br />';
135
                    if($width_ratio > $height_ratio) {
136
                        //print 'Width Wins<br />';
137
                        $resized_width = $target_width;
138
                        $resized_height = $source_height * $width_ratio;
139
                    } else {
140
                        //print 'Height Wins<br />';
141
                        $resized_height = $target_height;
142
                        $resized_width = $source_width * $height_ratio;
143
                    }
144
                } else {
145
                    $width_ratio = $target_width / $source_width;
146
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
147
                    $resized_width = $target_width;
148
                    $resized_height = $source_height * $width_ratio;
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
                /*
159
                $width_ratio    = $target_width / $source_width;
160
                $height_ratio   = $target_height / $source_height;
161
                if($width_ratio > $height_ratio) {
162
                    $resized_width = $target_width;
163
                    $resized_height = $source_height * $width_ratio;
164
                } else {
165
                    $resized_height = $target_height;
166
                    $resized_width = $source_width * $height_ratio;
167
                }*/
168
 
169
                $resized_width = round($resized_width);
170
                $resized_height = round($resized_height);
171
 
172
                $offset_width = round(($target_width - $resized_width) / 2);
173
                $offset_height = round(($target_height - $resized_height) / 2);
174
 
175
 
176
                $new_image = imageCreateTrueColor($target_width, $target_height);
177
                imageAlphaBlending($new_image, False);
178
                imageSaveAlpha($new_image, True);
179
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
180
                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);
183
 
184
 
185
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
186
                if(file_exists($target)) {
187
                    @unlink($target);
188
                }
189
 
190
 
191
                imagepng($new_image, $target);
192
            }
193
 
16798 efrain 194
            if($unlink_source) {
195
                @unlink($source);
196
            }
1 www 197
 
198
            return true;
199
 
200
        }
201
        catch (\Throwable $e)
202
        {
203
            error_log($e->getTraceAsString());
204
            return false;
205
        }
206
    }
207
 
208
    /**
209
     *
210
     * @param string $source
211
     * @param string $target_path
212
     * @param string $target_filename
213
     * @return boolean
214
     */
215
    public  static function uploadFile($source, $target_path, $target_filename)
216
    {
217
 
218
 
219
 
220
        try {
221
            $data = file_get_contents($source);
222
            $img = imagecreatefromstring($data);
223
 
224
            if(!file_exists($target_path)) {
225
                mkdir($target_path, 0755);
226
            }
227
 
228
            if($img) {
229
                list($source_width, $source_height) = getimagesize($source);
230
 
231
                $width_ratio    = $target_width / $source_width;
232
                $height_ratio   = $target_height / $source_height;
233
                if($width_ratio > $height_ratio) {
234
                    $resized_width = $target_width;
235
                    $resized_height = $source_height * $width_ratio;
236
                } else {
237
                    $resized_height = $target_height;
238
                    $resized_width = $source_width * $height_ratio;
239
                }
240
 
241
                $resized_width = round($resized_width);
242
                $resized_height = round($resized_height);
243
 
244
                $offset_width = round(($target_width - $resized_width) / 2);
245
                $offset_height = round(($target_height - $resized_height) / 2);
246
 
247
 
248
                $new_image = imageCreateTrueColor($target_width, $target_height);
249
                imageAlphaBlending($new_image, False);
250
                imageSaveAlpha($new_image, True);
251
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
252
                imagefill($new_image, 0, 0, $transparent);
253
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
254
 
255
 
256
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
257
                if(file_exists($target)) {
258
                    @unlink($target);
259
                }
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
    }
15077 efrain 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);
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
 
317
 
318
                $new_image = imageCreateTrueColor($resized_width, $resized_height);
319
                imageAlphaBlending($new_image, False);
320
                imageSaveAlpha($new_image, True);
321
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
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
 
325
 
326
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
327
                if(file_exists($target)) {
328
                    @unlink($target);
329
                }
330
 
331
 
332
                imagepng($new_image, $target);
333
            }
334
 
335
            unlink($source);
336
 
337
            return true;
338
 
339
        }
340
        catch (\Throwable $e)
341
        {
342
            error_log($e->getTraceAsString());
343
            return false;
344
        }
345
    }
1 www 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());
378
            return false;
379
        }
380
    }
381
 
382
}