• Hi all,

    I’ve been struggling for the past 2 weeks trying to figure out how to do this. I run a community based site for a client where people upload images and the images are then posted to the blog. My client wants the iPhone pics their users upload to automatically be rotated (like facebook does, and other sites)

    I would like to hook into attachment function(?) and rotate. I would like to use something like this(http://rtcamp.com/tutorials/fixing-image-orientation-wordpress-uploads/) without downloading a plugin.

    I’ve tried rtMedia, and unfortunately it does not work at all(wont process or upload any pics, it appears broken), and other plugins that rotate images but nothing seems to work.

    Honestly don’t want a plugin with all the bloat anyway. I just need to hook into attachment_upload and run a script similar to what was linked above. I just don’t understand how. My problem is, not sure which hook to use (which one is before upload? also is there any conflict with wp exif processing?)

    Sorry for the long post Please share your thoughts.. Using wp 3.6.

    TL;DR: how to grab iPhone exif data and fix image orientation? preferably creating a hook in functions.php to do it. dont want bloated plugin, should be just a function, cant find a way to do it, help.

    Thanks,
    Matt

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Right, you need to intercept the file after it’s placed on the server but before WP extracts EXIF data and generates multiple sized images. I’m not sure of how the entire sequence works, but as a guess, try hooking into the ‘wp_handle_upload’ filter, where you are passed an array containing file path info. Apply your referenced script to this file and hopefully that’ll be all that’s needed.

    Thread Starter Matt McFarland

    (@matt-mcfarland)

    @bcworkz thanks for your reply it was pretty helpful. I did some digging about wp_handle_upload/ It looks like it is not something you can add a filter to (technically?) but there is a way to hook into it: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_handle_upload_prefilter

    add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

    So I cracked open functions.php and added this:

    add_filter('wp_handle_upload_prefilter', 'exifRotate' );
    
    function exifRotate( $file ){
    	$file = wp_handle_upload($file);
    	$file = exif($file);
    }
    
    function exif($file) {
            //This line reads the EXIF data and passes it into an array
            $exif = read_exif_data($file['file']);
    
            //We're only interested in the orientation
            $exif_orient = isset($exif['Orientation'])?$exif['Orientation']:0;
            $rotateImage = 0;
    
            //We convert the exif rotation to degrees for further use
            if (6 == $exif_orient) {
                $rotateImage = 90;
                $imageOrientation = 1;
            } elseif (3 == $exif_orient) {
                $rotateImage = 180;
                $imageOrientation = 1;
            } elseif (8 == $exif_orient) {
                $rotateImage = 270;
                $imageOrientation = 1;
            }
    
            //if the image is rotated
            if ($rotateImage) {
    
                //WordPress 3.5+ have started using Imagick, if it is available since there is a noticeable difference in quality
                //Why spoil beautiful images by rotating them with GD, if the user has Imagick
    
                if (class_exists('Imagick')) {
                    $imagick = new Imagick();
                    $imagick->readImage($file['file']);
                    $imagick->rotateImage(new ImagickPixel(), $rotateImage);
                    $imagick->setImageOrientation($imageOrientation);
                    $imagick->writeImage($file['file']);
                    $imagick->clear();
                    $imagick->destroy();
                } else {
    
                    //if no Imagick, fallback to GD
                    //GD needs negative degrees
                    $rotateImage = -$rotateImage;
    
                    switch ($file['type']) {
                        case 'image/jpeg':
                            $source = imagecreatefromjpeg($file['file']);
                            $rotate = imagerotate($source, $rotateImage, 0);
                            imagejpeg($rotate, $file['file']);
                            break;
                        case 'image/png':
                            $source = imagecreatefrompng($file['file']);
                            $rotate = imagerotate($source, $rotateImage, 0);
                            imagepng($rotate, $file['file']);
                            break;
                        case 'image/gif':
                            $source = imagecreatefromgif($file['file']);
                            $rotate = imagerotate($source, $rotateImage, 0);
                            imagegif($rotate, $file['file']);
                            break;
                        default:
                            break;
                    }
                }
            }
            // The image orientation is fixed, pass it back for further processing
            return $file;
        }

    The code above really just borrwed from rtmedia’s post that I mentioned in the first post above.

    I hope it works, I dont have an iphone to test but the website has decent traffic so should know by the end of this weekend.

    Moderator bcworkz

    (@bcworkz)

    Talk about trial by fire!

    It should be more efficient using ‘wp_handle_upload’ filter than the prefilter version. The filter call occurs at the very end of the wp_handle_upload() function definition just before the return, line 351 in v3.6 wp-admin/includes/file.php. You should not have to call wp_handle_upload() this way.

    But if your version is working without unreasonable delay, why try to “fix” it?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how to auto rotate iPhone image upon upload using functions.php?’ is closed to new replies.