Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3293 | | 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
    /**
3293 efrain 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
 
111
     * @return boolean
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);
120
            $img = imagecreatefromstring($data);
121
 
122
            if(!file_exists($target_path)) {
123
                mkdir($target_path, 0755);
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
                }
156
 
157
 
158
                imagepng($new_image, $target);
159
            }
160
 
161
            unlink($source);
162
 
163
            return true;
164
 
165
        }
166
        catch (\Throwable $e)
167
        {
168
            error_log($e->getTraceAsString());
169
            return false;
170
        }
171
    }
172
 
173
    /**
1 www 174
     *
175
     * @param string $source
176
     * @param string $target_path
177
     * @param string $target_filename
178
     * @param number $target_width
179
     * @param number $target_height
180
     * @param boolean $crop_to_dimensions
181
     * @return boolean
182
     */
183
    public  static function uploadImage($source, $target_path, $target_filename, $target_width, $target_height,  $crop_to_dimensions = true )
184
    {
185
 
186
 
187
 
188
        try {
189
            $data = file_get_contents($source);
190
            $img = imagecreatefromstring($data);
191
 
192
            if(!file_exists($target_path)) {
193
                mkdir($target_path, 0755);
194
            }
195
 
196
            if($img) {
197
                list($source_width, $source_height) = getimagesize($source);
198
 
199
                if($crop_to_dimensions) {
200
                    $width_ratio = $target_width / $source_width;
201
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
202
                    $height_ratio = $target_height / $source_height;
203
                    //print 'Height Ratio: ' . $height_ratio . '<br />';
204
                    if($width_ratio > $height_ratio) {
205
                        //print 'Width Wins<br />';
206
                        $resized_width = $target_width;
207
                        $resized_height = $source_height * $width_ratio;
208
                    } else {
209
                        //print 'Height Wins<br />';
210
                        $resized_height = $target_height;
211
                        $resized_width = $source_width * $height_ratio;
212
                    }
213
                } else {
214
                    $width_ratio = $target_width / $source_width;
215
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
216
                    $resized_width = $target_width;
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
                }
227
                /*
228
                $width_ratio    = $target_width / $source_width;
229
                $height_ratio   = $target_height / $source_height;
230
                if($width_ratio > $height_ratio) {
231
                    $resized_width = $target_width;
232
                    $resized_height = $source_height * $width_ratio;
233
                } else {
234
                    $resized_height = $target_height;
235
                    $resized_width = $source_width * $height_ratio;
236
                }*/
237
 
238
                $resized_width = round($resized_width);
239
                $resized_height = round($resized_height);
240
 
241
                $offset_width = round(($target_width - $resized_width) / 2);
242
                $offset_height = round(($target_height - $resized_height) / 2);
243
 
244
 
245
                $new_image = imageCreateTrueColor($target_width, $target_height);
246
                imageAlphaBlending($new_image, False);
247
                imageSaveAlpha($new_image, True);
248
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
249
                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);
252
 
253
 
254
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
255
                if(file_exists($target)) {
256
                    @unlink($target);
257
                }
258
 
259
 
260
                imagepng($new_image, $target);
261
            }
262
 
263
            unlink($source);
264
 
265
            return true;
266
 
267
        }
268
        catch (\Throwable $e)
269
        {
270
            error_log($e->getTraceAsString());
271
            return false;
272
        }
273
    }
274
 
275
    /**
276
     *
277
     * @param string $source
278
     * @param string $target_path
279
     * @param string $target_filename
280
     * @return boolean
281
     */
282
    public  static function uploadFile($source, $target_path, $target_filename)
283
    {
284
 
285
 
286
 
287
        try {
288
            $data = file_get_contents($source);
289
            $img = imagecreatefromstring($data);
290
 
291
            if(!file_exists($target_path)) {
292
                mkdir($target_path, 0755);
293
            }
294
 
295
            if($img) {
296
                list($source_width, $source_height) = getimagesize($source);
297
 
7016 efrain 298
                $target_width = $source_width;
299
                $target_height =  $source_height;
300
 
1 www 301
                $width_ratio    = $target_width / $source_width;
302
                $height_ratio   = $target_height / $source_height;
303
                if($width_ratio > $height_ratio) {
304
                    $resized_width = $target_width;
305
                    $resized_height = $source_height * $width_ratio;
306
                } else {
307
                    $resized_height = $target_height;
308
                    $resized_width = $source_width * $height_ratio;
309
                }
310
 
311
                $resized_width = round($resized_width);
312
                $resized_height = round($resized_height);
313
 
314
                $offset_width = round(($target_width - $resized_width) / 2);
315
                $offset_height = round(($target_height - $resized_height) / 2);
316
 
317
 
318
                $new_image = imageCreateTrueColor($target_width, $target_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 , $offset_width, $offset_height, 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
    }
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
}