Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3293 | Ir a la última revisión | | 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
111
     * @return boolean
112
     */
113
    public  static function uploadImage($source, $target_path, $target_filename, $target_width, $target_height,  $crop_to_dimensions = true )
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
                if($crop_to_dimensions) {
130
                    $width_ratio = $target_width / $source_width;
131
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
132
                    $height_ratio = $target_height / $source_height;
133
                    //print 'Height Ratio: ' . $height_ratio . '<br />';
134
                    if($width_ratio > $height_ratio) {
135
                        //print 'Width Wins<br />';
136
                        $resized_width = $target_width;
137
                        $resized_height = $source_height * $width_ratio;
138
                    } else {
139
                        //print 'Height Wins<br />';
140
                        $resized_height = $target_height;
141
                        $resized_width = $source_width * $height_ratio;
142
                    }
143
                } else {
144
                    $width_ratio = $target_width / $source_width;
145
                    //print 'Width Ratio: ' . $width_ratio . '<br />';
146
                    $resized_width = $target_width;
147
                    $resized_height = $source_height * $width_ratio;
148
                    //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';
149
                    if($resized_height > $target_height) {
150
                        $height_ratio = $target_height / $resized_height;
151
                        $resized_height = $target_height;
152
                        //print 'Height Ratio: ' . $height_ratio . '<br />';
153
                        $resized_width = $resized_width * $height_ratio;
154
                        //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';
155
                    }
156
                }
157
                /*
158
                $width_ratio    = $target_width / $source_width;
159
                $height_ratio   = $target_height / $source_height;
160
                if($width_ratio > $height_ratio) {
161
                    $resized_width = $target_width;
162
                    $resized_height = $source_height * $width_ratio;
163
                } else {
164
                    $resized_height = $target_height;
165
                    $resized_width = $source_width * $height_ratio;
166
                }*/
167
 
168
                $resized_width = round($resized_width);
169
                $resized_height = round($resized_height);
170
 
171
                $offset_width = round(($target_width - $resized_width) / 2);
172
                $offset_height = round(($target_height - $resized_height) / 2);
173
 
174
 
175
                $new_image = imageCreateTrueColor($target_width, $target_height);
176
                imageAlphaBlending($new_image, False);
177
                imageSaveAlpha($new_image, True);
178
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
179
                imagefill($new_image, 0, 0, $transparent);
180
               // imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
181
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
182
 
183
 
184
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
185
                if(file_exists($target)) {
186
                    @unlink($target);
187
                }
188
 
189
 
190
                imagepng($new_image, $target);
191
            }
192
 
193
            unlink($source);
194
 
195
            return true;
196
 
197
        }
198
        catch (\Throwable $e)
199
        {
200
            error_log($e->getTraceAsString());
201
            return false;
202
        }
203
    }
204
 
205
    /**
206
     *
207
     * @param string $source
208
     * @param string $target_path
209
     * @param string $target_filename
210
     * @return boolean
211
     */
212
    public  static function uploadFile($source, $target_path, $target_filename)
213
    {
214
 
215
 
216
 
217
        try {
218
            $data = file_get_contents($source);
219
            $img = imagecreatefromstring($data);
220
 
221
            if(!file_exists($target_path)) {
222
                mkdir($target_path, 0755);
223
            }
224
 
225
            if($img) {
226
                list($source_width, $source_height) = getimagesize($source);
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
 
252
 
253
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
254
                if(file_exists($target)) {
255
                    @unlink($target);
256
                }
257
 
258
 
259
                imagepng($new_image, $target);
260
            }
261
 
262
            unlink($source);
263
 
264
            return true;
265
 
266
        }
267
        catch (\Throwable $e)
268
        {
269
            error_log($e->getTraceAsString());
270
            return false;
271
        }
272
    }
273
 
274
    /**
275
     *
276
     * @param string $path
277
     * @param string $filename
278
     * @return boolean
279
     */
280
    public static function delete($path, $filename)
281
    {
282
        try {
283
            if (is_dir($path)){
284
                if ($dh = opendir($path)) {
285
                    while (($file = readdir($dh)) !== false)
286
                    {
287
                        if($file == '.' || $file == '..') {
288
                            continue;
289
                        }
290
 
291
                        if($file == $filename) {
292
                            unlink($path . DIRECTORY_SEPARATOR . $file);
293
                        }
294
                    }
295
                    closedir($dh);
296
                }
297
            }
298
 
299
            return true;
300
 
301
        }
302
        catch (\Throwable $e)
303
        {
304
            error_log($e->getTraceAsString());
305
            return false;
306
        }
307
    }
308
 
309
}