Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17015 | | 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
 
17018 efrain 14
 
17002 efrain 15
    /**
16
     *
17
     * @var array
18
     */
19
    private $config;
17018 efrain 20
 
21
     /**
22
     *
17002 efrain 23
     * @param array $config
24
     */
17018 efrain 25
    private function __construct($config)
1 www 26
    {
17018 efrain 27
        $this->config       = $config;
17002 efrain 28
    }
29
 
30
    /**
31
     *
32
     * @param array $config
33
     * @return \LeadersLinked\Library\Image
34
     */
35
    public static function getInstance($config)
36
    {
37
        if(self::$_instance == null) {
38
            self::$_instance = new Image($config);
39
        }
1 www 40
 
17002 efrain 41
        return self::$_instance;
42
    }
1 www 43
 
17018 efrain 44
 
17002 efrain 45
 
46
 
47
    /**
48
     *
1 www 49
     * @param string $source
50
     * @param string $target_filename
51
     * @param number $target_width
52
     * @param number $target_height
53
     * @param boolean $crop_to_dimensions
54
     * @return boolean
55
     */
17018 efrain 56
    public function uploadProcessChangeSize($source, $target_filename, $target_width, $target_height,  $crop_to_dimensions)
1 www 57
    {
17018 efrain 58
        $temp_filename = '';
1 www 59
 
60
 
61
        try {
17012 efrain 62
 
1 www 63
            $data = file_get_contents($source);
17002 efrain 64
            $img = imagecreatefromstring($data);
65
 
66
 
1 www 67
            if($img) {
68
                list($source_width, $source_height) = getimagesize($source);
69
 
70
                if($crop_to_dimensions) {
71
                    $width_ratio = $target_width / $source_width;
72
                    $height_ratio = $target_height / $source_height;
73
                    if($width_ratio > $height_ratio) {
74
                        $resized_width = $target_width;
75
                        $resized_height = $source_height * $width_ratio;
76
                    } else {
77
                        $resized_height = $target_height;
78
                        $resized_width = $source_width * $height_ratio;
79
                    }
80
                } else {
81
                    $width_ratio = $target_width / $source_width;
82
                    $resized_width = $target_width;
83
                    $resized_height = $source_height * $width_ratio;
84
                    if($resized_height > $target_height) {
85
                        $height_ratio = $target_height / $resized_height;
86
                        $resized_height = $target_height;
87
                        $resized_width = $resized_width * $height_ratio;
88
                    }
89
                }
17002 efrain 90
 
1 www 91
 
92
                $resized_width = round($resized_width);
93
                $resized_height = round($resized_height);
94
 
95
                $offset_width = round(($target_width - $resized_width) / 2);
96
                $offset_height = round(($target_height - $resized_height) / 2);
97
 
98
 
99
                $new_image = imageCreateTrueColor($target_width, $target_height);
100
                imageAlphaBlending($new_image, False);
101
                imageSaveAlpha($new_image, True);
102
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
103
                imagefill($new_image, 0, 0, $transparent);
104
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
105
 
106
 
17018 efrain 107
                $temp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $target_filename;
17002 efrain 108
                imagepng($new_image, $temp_filename);
17018 efrain 109
 
17002 efrain 110
 
111
 
112
            } else {
17018 efrain 113
                $temp_filename = '';
16798 efrain 114
            }
1 www 115
 
116
        }
117
        catch (\Throwable $e)
118
        {
17012 efrain 119
 
17018 efrain 120
            $temp_filename = '';
17012 efrain 121
 
1 www 122
        }
17018 efrain 123
 
124
        @unlink($source);
125
        return $temp_filename;
1 www 126
    }
127
 
128
    /**
129
     *
130
     * @param string $source
131
     * @param string $target_filename
132
     * @return boolean
133
     */
17018 efrain 134
    public function uploadProcessWithOutChangeSize($source, $target_filename)
1 www 135
    {
17018 efrain 136
        $temp_filename = '';
137
 
1 www 138
        try {
139
            $data = file_get_contents($source);
17015 efrain 140
 
1 www 141
 
17002 efrain 142
            $img = imagecreatefromstring($data);
143
 
1 www 144
            if($img) {
145
                list($source_width, $source_height) = getimagesize($source);
146
 
17002 efrain 147
                $target_width = $source_width;
148
                $target_height =  $source_height;
149
 
1 www 150
                $width_ratio    = $target_width / $source_width;
151
                $height_ratio   = $target_height / $source_height;
152
                if($width_ratio > $height_ratio) {
153
                    $resized_width = $target_width;
154
                    $resized_height = $source_height * $width_ratio;
155
                } else {
156
                    $resized_height = $target_height;
157
                    $resized_width = $source_width * $height_ratio;
158
                }
159
 
160
                $resized_width = round($resized_width);
161
                $resized_height = round($resized_height);
162
 
163
                $offset_width = round(($target_width - $resized_width) / 2);
164
                $offset_height = round(($target_height - $resized_height) / 2);
165
 
166
 
167
                $new_image = imageCreateTrueColor($target_width, $target_height);
168
                imageAlphaBlending($new_image, False);
169
                imageSaveAlpha($new_image, True);
170
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
171
                imagefill($new_image, 0, 0, $transparent);
172
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
173
 
174
 
17018 efrain 175
 
1 www 176
 
17018 efrain 177
                $temp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $target_filename;
17002 efrain 178
                imagepng($new_image, $temp_filename);
1 www 179
 
15077 efrain 180
 
181
 
17018 efrain 182
            } else {
183
                $temp_filename = '';
15077 efrain 184
            }
185
 
186
        }
187
        catch (\Throwable $e)
188
        {
17015 efrain 189
 
17018 efrain 190
            $temp_filename = '';
191
 
15077 efrain 192
        }
17018 efrain 193
 
194
        @unlink($source);
195
        return $temp_filename;
196
 
15077 efrain 197
    }
1 www 198
}