Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17012 | 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
 
17002 efrain 5
class Image
1 www 6
{
17002 efrain 7
 
1 www 8
    /**
17002 efrain 9
     *
10
     * @var \LeadersLinked\Library\Image
11
     */
12
    private static $_instance;
13
 
14
    /**
15
     *
16
     * @var \LeadersLinked\Library\Storage
17
     */
18
    private $storage;
19
 
20
    /**
1 www 21
     *
17002 efrain 22
     * @var array
23
     */
24
    private $config;
25
 
26
    /**
27
     *
28
     * @param array $config
29
     */
30
    private function __construct($config)
1 www 31
    {
17002 efrain 32
        $this->config = $config;
33
        $this->storage = Storage::getInstance($config);
34
    }
35
 
36
    /**
37
     *
38
     * @param array $config
39
     * @return \LeadersLinked\Library\Image
40
     */
41
    public static function getInstance($config)
42
    {
43
        if(self::$_instance == null) {
44
            self::$_instance = new Image($config);
45
        }
1 www 46
 
17002 efrain 47
        return self::$_instance;
48
    }
1 www 49
 
50
    /**
51
     *
17002 efrain 52
     * @return \LeadersLinked\Library\Storage
53
     */
54
    public function getStorage()
55
    {
56
        return $this->storage;
57
    }
58
 
59
 
60
 
61
    /**
62
     *
1 www 63
     * @param string $source
64
     * @param string $target_path
17002 efrain 65
     * @param string $target_code
1 www 66
     * @param string $target_filename
67
     * @param number $target_width
68
     * @param number $target_height
69
     * @param boolean $crop_to_dimensions
16798 efrain 70
     * @param boolean $unlink_source
1 www 71
     * @return boolean
72
     */
17002 efrain 73
    public function uploadImageChangeSize($source, $target_path, $target_code, $target_filename, $target_width, $target_height,  $crop_to_dimensions, $unlink_source)
1 www 74
    {
75
 
76
 
77
 
78
        try {
17012 efrain 79
 
1 www 80
            $data = file_get_contents($source);
17002 efrain 81
            $img = imagecreatefromstring($data);
82
 
83
 
1 www 84
            if($img) {
85
                list($source_width, $source_height) = getimagesize($source);
86
 
87
                if($crop_to_dimensions) {
88
                    $width_ratio = $target_width / $source_width;
89
                    $height_ratio = $target_height / $source_height;
90
                    if($width_ratio > $height_ratio) {
91
                        $resized_width = $target_width;
92
                        $resized_height = $source_height * $width_ratio;
93
                    } else {
94
                        $resized_height = $target_height;
95
                        $resized_width = $source_width * $height_ratio;
96
                    }
97
                } else {
98
                    $width_ratio = $target_width / $source_width;
99
                    $resized_width = $target_width;
100
                    $resized_height = $source_height * $width_ratio;
101
                    if($resized_height > $target_height) {
102
                        $height_ratio = $target_height / $resized_height;
103
                        $resized_height = $target_height;
104
                        $resized_width = $resized_width * $height_ratio;
105
                    }
106
                }
17002 efrain 107
 
1 www 108
 
109
                $resized_width = round($resized_width);
110
                $resized_height = round($resized_height);
111
 
112
                $offset_width = round(($target_width - $resized_width) / 2);
113
                $offset_height = round(($target_height - $resized_height) / 2);
114
 
115
 
116
                $new_image = imageCreateTrueColor($target_width, $target_height);
117
                imageAlphaBlending($new_image, False);
118
                imageSaveAlpha($new_image, True);
119
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
120
                imagefill($new_image, 0, 0, $transparent);
121
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
122
 
123
 
17002 efrain 124
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
125
                imagepng($new_image, $temp_filename);
126
 
127
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
128
                unlink($temp_filename);
129
 
130
 
17012 efrain 131
                if($unlink_source) {
132
                    @unlink($source);
133
                }
134
 
135
 
136
 
17002 efrain 137
                return $result;
138
            } else {
17012 efrain 139
 
140
 
141
                if($unlink_source) {
142
                    @unlink($source);
143
                }
144
 
1 www 145
 
17002 efrain 146
                return true;
16798 efrain 147
            }
1 www 148
 
149
        }
150
        catch (\Throwable $e)
151
        {
17012 efrain 152
 
153
            if($unlink_source) {
154
                @unlink($source);
155
            }
156
 
157
 
1 www 158
            error_log($e->getTraceAsString());
159
            return false;
160
        }
161
    }
162
 
163
    /**
164
     *
165
     * @param string $source
166
     * @param string $target_path
17002 efrain 167
     * @param string $target_code
1 www 168
     * @param string $target_filename
17002 efrain 169
     * @param boolean $unlink_source
1 www 170
     * @return boolean
171
     */
17002 efrain 172
    public function uploadImageRaw($source, $target_path, $target_code, $target_filename, $unlink_source)
1 www 173
    {
174
 
175
 
176
 
177
        try {
178
            $data = file_get_contents($source);
17015 efrain 179
 
1 www 180
 
17002 efrain 181
            $img = imagecreatefromstring($data);
182
 
1 www 183
            if($img) {
184
                list($source_width, $source_height) = getimagesize($source);
185
 
17002 efrain 186
                $target_width = $source_width;
187
                $target_height =  $source_height;
188
 
1 www 189
                $width_ratio    = $target_width / $source_width;
190
                $height_ratio   = $target_height / $source_height;
191
                if($width_ratio > $height_ratio) {
192
                    $resized_width = $target_width;
193
                    $resized_height = $source_height * $width_ratio;
194
                } else {
195
                    $resized_height = $target_height;
196
                    $resized_width = $source_width * $height_ratio;
197
                }
198
 
199
                $resized_width = round($resized_width);
200
                $resized_height = round($resized_height);
201
 
202
                $offset_width = round(($target_width - $resized_width) / 2);
203
                $offset_height = round(($target_height - $resized_height) / 2);
204
 
205
 
206
                $new_image = imageCreateTrueColor($target_width, $target_height);
207
                imageAlphaBlending($new_image, False);
208
                imageSaveAlpha($new_image, True);
209
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
210
                imagefill($new_image, 0, 0, $transparent);
211
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
212
 
213
 
214
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
215
                if(file_exists($target)) {
216
                    @unlink($target);
217
                }
218
 
17002 efrain 219
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
220
                imagepng($new_image, $temp_filename);
1 www 221
 
17002 efrain 222
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
223
                unlink($temp_filename);
15077 efrain 224
 
17015 efrain 225
                if($unlink_source) {
226
                    unlink($source);
227
                }
15077 efrain 228
 
17002 efrain 229
                return $result;
15077 efrain 230
            }
231
 
17015 efrain 232
            if($unlink_source) {
233
                unlink($source);
234
            }
15077 efrain 235
 
17015 efrain 236
 
15077 efrain 237
            return true;
238
 
239
        }
240
        catch (\Throwable $e)
241
        {
17015 efrain 242
            if($unlink_source) {
243
                unlink($source);
244
            }
245
 
15077 efrain 246
            error_log($e->getTraceAsString());
247
            return false;
248
        }
249
    }
1 www 250
}