Created
September 26, 2017 18:53
-
-
Save bssanchez/9e80f711907250fb51dde5e8ec3350d2 to your computer and use it in GitHub Desktop.
PHP function for fix rotate exif and delete exif information using GD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Función que crea un clon de la imagen que se pase como argumento | |
| * arreglándo la rotación y removiendo la información EXIF usando GD | |
| * | |
| * @param string $ruta ruta de la imagen | |
| */ | |
| function removeRotation($ruta) | |
| { | |
| // Obtener extension del archivo para obtener su ruta (Próxima lo cambio por filetype) | |
| $ext = strtolower(end(explode('.', $ruta))); | |
| // Generar imagen a partir de la imagen cargada en la ruta | |
| $imagen = null; | |
| switch($ext) { | |
| case "jpg": | |
| case "jpeg": | |
| $imagen = imagecreatefromjpeg($ruta); | |
| break; | |
| case "png": | |
| $imagen = imagecreatefrompng($ruta); | |
| break; | |
| } | |
| if (!is_null($imagen)) { | |
| // Obtener alto y ancho de la imagen para crear el clon con las mismas dimensiones | |
| $w = imagesx($imagen); | |
| $h = imagesy($imagen); | |
| // Obtener datos exif de la imagen actual | |
| $exif = exif_read_data($ruta); | |
| // Generar el clon de la imagen con las mismas dimensiones | |
| $clon = imagecreatetruecolor($w, $h); | |
| imagecopy($clon, $imagen, 0, 0, 0, 0, $w, $h); | |
| // Si tiene el exif de orientación, se gira la misma dependiendo del valor | |
| if (isset($exif['Orientation'])) { | |
| switch($exif['Orientation']) { | |
| case 3: | |
| $clon = imagerotate($clon, 180, 0); | |
| break; | |
| case 6: | |
| $clon = imagerotate($clon, -90, 0); | |
| break; | |
| case 8: | |
| $clon = imagerotate($clon, 90, 0); | |
| break; | |
| } | |
| } | |
| /* Exportar clon de imagen con los datos arreglados reemplazando la existente | |
| * a un 70% de calidad (ese valor se puede cambiar pero no quita mucha calidad | |
| * pero si reduce bastante el peso) | |
| */ | |
| switch($ext) { | |
| case "jpg": | |
| case "jpeg": | |
| imagejpeg($clon, $ruta, 70); | |
| break; | |
| case "png": | |
| imagepng($clon, $ruta, 70); | |
| break; | |
| } | |
| // Liberar memoria | |
| imagedestroy($imagen); | |
| imagedestroy($clon); | |
| } | |
| } | |
| if (php_sapi_name() == "cli") { | |
| // Ejemplo usando la terminal | |
| if ($argc == 1) { | |
| echo "\033[31mNo se especificó ningún archivo\033[0m\r\n"; | |
| echo "Ej: \033[32mphp \033[36m" . basename(__FILE__) . " \033[0mimagen1.jpg imagen2.png\r\n"; | |
| exit(2); | |
| } | |
| for($i = 1; $i < $argc; $i++) { | |
| if (file_exists($argv[$i])) { | |
| echo "\033[32m[-] Orientación y EXIF removidos " . $argv[$i] . "\033[0m\r\n"; | |
| } else { | |
| echo "\033[31m[X] No existe el fichero " . $argv[$i] . "\033[0m\r\n"; | |
| } | |
| } | |
| } else { | |
| // Ejemplo llamando la función | |
| removeRotation('example.jpg'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment