Código de PHP - Trabajando con imagenes

sin imagen de perfil
Val: 393
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Trabajando con imagenesgráfica de visualizaciones


PHP

Publicado el 19 de Febrero del 2021 por Jefferson (11 códigos)
1.330 visualizaciones desde el 19 de Febrero del 2021
Trabajar con imágenes, en estos días es lo que vende "ya que todo entra por la vista".

Pero si trabajamos solo con move_uploaded_file empiezan nuestro problemas ya que esta instrucción solo mueve un archivo del punto A-B usando el mecanismo HTTP POST.

Si la imagen que queremos es muy grande (sin mencionar la que tomamos de nuestro teléfono que son exageradamente grandes y ahora parecen no acabar de pixelarlas mas) Cuando queremos pintarlas en nuestro src comienza la peregrinación para que cargue.

Esto trae como consecuencia que los clientes visitantes de la pagina se obstinan y abandonan ...

Con esta clase en php "que no es mia, de hecho hay tantas versiones en internet y en tantos idiomas que es difícil saber su autoría"

Lo que hice fue tomar funciones de una y otra y las agrupe en una sola clase, para que al final tengamos todo en uno.

* - Con ella reducimos el tamaño (redimensionar)
* - Creamos img Thumb (muy pequeñas para los perfiles)
* - Eliminamos el fondo negro de los png
* - Enderezamos las imágenes que generalmente las voltean los tlf.

Espero les sea de ayuda, a mi me sirvió de mucho.

Aparte les dejare una pequeña guía de como manipularla.

La subo a Google Drive, por lo pesada de las imágenes https://drive.google.com/file/d/1DIECxgU21ws_zjWkSUa8RBi4QizFpg8o/view?usp=sharing




Sin-titulo

1.0

Publicado el 19 de Febrero del 2021gráfica de visualizaciones de la versión: 1.0
1.331 visualizaciones desde el 19 de Febrero del 2021
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
class imagen
{
    private $imagen;
    private $tipo;
    private $ancho;
    private $alto;
 
    public function cargaImg($nombre)
    {
 
        $info = getimagesize($nombre);
 
        $this->ancho = $info[0];
        $this->alto = $info[1];
        $this->tipo = $info[2];
 
        switch ($this->tipo) {
 
            case IMAGETYPE_JPEG:
 
                $this->imagen = imagecreatefromjpeg($nombre);
 
                break;
 
            case IMAGETYPE_GIF:
 
                $this->imagen = imagecreatefromgif($nombre);
 
                break;
 
            case IMAGETYPE_PNG:
 
                $this->imagen = imagecreatefrompng($nombre);
 
                break;
 
            default:
 
        }
    }
    public function guardar($nombre, $quality = 100, $tipo = false)
    {
        $tipo = ($tipo) ? $tipo : $this->tipo;
 
        switch ($tipo) {
 
            case IMAGETYPE_JPEG:
 
                imagejpeg($this->imagen, $nombre . image_type_to_extension($tipo), $quality);
 
                break;
 
            case IMAGETYPE_GIF:
 
                imagegif($this->imagen, $nombre . image_type_to_extension($tipo));
 
                break;
 
            case IMAGETYPE_PNG:
 
                $pngquality = floor($quality / 100 * 9);
 
                imagepng($this->imagen, $nombre . image_type_to_extension($tipo), $pngquality);
 
                break;
 
            default:
 
        }
    }
    public function redimensionar($value, $prop)
    {
 
        $prop_value = ($prop == 'width') ? $this->ancho : $this->alto;
        $prop_versus = ($prop == 'width') ? $this->alto : $this->ancho;
 
        $pcent = $value / $prop_value;
        $value_versus = $prop_versus * $pcent;
 
        $imagen = ($prop == 'width') ? imagecreatetruecolor($value, $value_versus) : imagecreatetruecolor($value_versus, $value);
 
        if ($this->tipo == IMAGETYPE_GIF || $this->tipo == IMAGETYPE_PNG) $this->preparar_imagen($imagen);
 
        switch ($prop) {
 
            case 'width':
 
                imagecopyresampled($imagen, $this->imagen, 0, 0, 0, 0, $value, $value_versus, $this->ancho, $this->alto);
 
                break;
 
            default:
 
                imagecopyresampled($imagen, $this->imagen, 0, 0, 0, 0, $value_versus, $value, $this->ancho, $this->alto);
        }
 
        $this->ancho = imagesx($imagen);
        $this->alto = imagesy($imagen);
        $this->imagen = $imagen;
    }
    public function recortar($rec_ancho, $rec_alto, $pos = 'center')
    {
        $pcent = min($this->ancho / $rec_ancho, $this->alto / $rec_alto);
        $bigw = (int) ($pcent * $rec_ancho);
        $bigh = (int) ($pcent * $rec_alto);
 
        $imagen = imagecreatetruecolor($rec_ancho, $rec_alto);
 
        if ($this->tipo == IMAGETYPE_GIF || $this->tipo == IMAGETYPE_PNG) $this->preparar_imagen($imagen);
 
        switch ($pos) {
 
            case 'left':
 
                imagecopyresampled($imagen, $this->imagen, 0, 0, 0, abs(($this->alto - $bigh) / 2), $rec_ancho, $rec_alto, $bigw, $bigh);
 
                break;
 
            case 'right':
 
                imagecopyresampled($imagen, $this->imagen, 0, 0, $this->ancho - $bigw, abs(($this->alto - $bigh) / 2), $rec_ancho, $rec_alto, $bigw, $bigh);
 
                break;
 
            case 'top':
 
                imagecopyresampled($imagen, $this->imagen, 0, 0, abs(($this->ancho - $bigw) / 2), 0, $rec_ancho, $rec_alto, $bigw, $bigh);
 
                break;
 
            case 'bottom':
 
                imagecopyresampled($imagen, $this->imagen, 0, 0, abs(($this->ancho - $bigw) / 2), $this->alto - $bigh, $rec_ancho, $rec_alto, $bigw, $bigh);
 
                break;
 
            default:
 
                imagecopyresampled($imagen, $this->imagen, 0, 0, abs(($bigw - $this->ancho) / 2), abs(($bigh - $this->alto) / 2), $rec_ancho, $rec_alto, $bigw, $bigh);
        }
 
        $this->ancho = $rec_ancho;
        $this->alto = $rec_alto;
        $this->imagen = $imagen;
    }
    private function preparar_imagen($imagen)
    {
 
        switch ($this->tipo) {
 
            case IMAGETYPE_GIF:
 
                $background = imagecolorallocate($imagen, 0, 0, 0);
                imagecolortransparent($imagen, $background);
 
                break;
 
            case IMAGETYPE_PNG:
 
                imagealphablending($imagen, FALSE);
                imagesavealpha($imagen, TRUE);
 
                break;
        }
    }
    private static function reflejarImagen($imagenOriginal)
    {
        $anchura = imagesx($imagenOriginal);
 
        $altura = imagesy($imagenOriginal);
 
        $origenDeX = $anchura - 1;
 
        $origenDeY = 0;
 
        $anchura_original = -$anchura;
 
        $altura_original = $altura;
 
        $imagenDeDestino = imagecreatetruecolor($anchura, $altura);
 
        if (imagecopyresampled($imagenDeDestino, $imagenOriginal, 0, 0, $origenDeX, $origenDeY, $anchura, $altura, $anchura_original, $altura_original)) return $imagenDeDestino;
 
        return $imagenOriginal;
    }
    public function endereza($ficheroDeImagen)
    {
        $codificacionExif = exif_read_data($ficheroDeImagen);
 
        if ($codificacionExif && isset($codificacionExif['Orientation'])) {
 
            $orientacion = $codificacionExif['Orientation'];
 
            if ($orientacion != 1) {
 
                $imagenEnProceso = imagecreatefromjpeg($ficheroDeImagen);
 
                $reflejo = false;
 
                $grados = 0;
 
                switch ($orientacion) {
                    case 2:
 
                        $reflejo = true;
 
                        break;
 
                    case 3:
 
                        $grados = 180;
 
                        break;
 
                    case 4:
 
                        $grados = 180;
 
                        $reflejo = true;
 
                        break;
 
                    case 5:
 
                        $grados = 270;
 
                        $reflejo = true;
 
                        break;
 
                    case 6:
 
                        $grados = 270;
 
                        break;
 
                    case 7:
 
                        $grados = 90;
 
                        $reflejo = true;
 
                        break;
 
                    case 8:
 
                        $grados = 90;
 
                        break;
                }
 
                if ($grados) $imagenEnProceso = imagerotate($imagenEnProceso, $grados, 0);
 
                if ($reflejo) $imagenEnProceso = self::reflejarImagen($imagenEnProceso);
 
                imagejpeg($imagenEnProceso, $ficheroDeImagen);
            }
        }
 
        return true;
    }
    public function borra_file($file)
    {
        if (file_exists($file)) {
 
            unlink($file);
        }
    }
}

Si alguno de los archivos de descarga no funciona, comentanos aquí el error.




Comentarios sobre la versión: 1.0 (0)


No hay comentarios
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6896