Gravity Forms – Mobile image rotation problem hack

Recently I’ve been developing an application that uses Gravity Forms to allow visitors to add content. Just as I had everything running smoothly, Gravity Forms uploading decided to be a pain in the bum and photo’s taken on Android (and probably iPhone) mobile portrait mode were rotated the wrong way. I tried a few different plugins but they wouldn’t work with Gravity Forms because Gravity Forms doesn’t hook into WordPress uploading.. so in the end I had to add the fix code to Gravity Forms (and I couldn’t add that as a GF plugin because GF doesn’t have a hook there..) anyway, here is the wee hack I used to fix the problem and unfortunately I’ll have to add it to every plugin update until they themselves fix it… thanks to the developer of Fix Image Rotation for the rotation code.

Edit file: gravityforms/forms_model.php and add the green highlighted code.

[cc lang=”php” first_line=”3075″ line_numbers=”on” width=”100%”] // use image exif/iptc data for title and caption defaults if possible if ( $image_meta = @wp_read_image_metadata( $file ) ) { include_once( ABSPATH . ‘wp-admin/includes/image-edit.php’ ); $rotator = false; $flipper = false; switch ($image_meta[‘orientation’]) { case 1: break; case 2: $flipper = array(false, true); break; case 3: $orientation = -180; $rotator = true; break; case 4: $flipper = array(true, false); break; case 5: $orientation = -90; $rotator = true; $flipper = array(false, true); break; case 6: $orientation = -90; $rotator = true; break; case 7: $orientation = -270; $rotator = true; $flipper = array(false, true); break; case 8: case 9: $orientation = -270; $rotator = true; break; default: $orientation = 0; $rotator = true; break; } $editor = wp_get_image_editor($file); if (!is_wp_error($editor)) { if ($rotator === true) { $editor->rotate($orientation); } if ($flipper !== false) { $editor->flip($flipper[0], $flipper[1]); } $editor->save($file); } if ( trim( $image_meta[‘title’] ) && ! is_numeric( sanitize_title( $image_meta[‘title’] ) ) ) { $title = $image_meta[‘title’]; } if ( trim( $image_meta[‘caption’] ) ) { $content = $image_meta[‘caption’]; } } [/cc]

Leave a Reply

Your email address will not be published. Required fields are marked *