Auto Image Rotate for iPhone and iPad

AndyB

Well-known member
This hack will fix the problem when images uploaded from an iPhone or iPad end up in the wrong orientation (sideways).

You must have ImageMagick installed.

The code snippet below is for Xenforo v1.2.x.

library/Xenforo/Upload.php

Line #184

PHP:
protected function _checkImageState()
{
	
	// start hack	
	
	exec("/usr/bin/mogrify -auto-orient $this->_tempFile");
	
	// end hack
 
Last edited:
This hack will fix the problem when images uploaded from an iPhone or iPad end up in the wrong orientation (sideways).

You must have ImageMagick installed.

We have it installed, but I don't know if it is going to run without root or superuser permissions. At least in our server setup, I'm almost sure we can't run "system" commands. Will this throw an error somewhere if I try it out and it fails?

Pretty sad we have to do a sloppy workaround due to the failure of these devices to upload properly, though.
 
We have it installed, but I don't know if it is going to run without root or superuser permissions. At least in our server setup, I'm almost sure we can't run "system" commands. Will this throw an error somewhere if I try it out and it fails?

Pretty sad we have to do a sloppy workaround due to the failure of these devices to upload properly, though.

I suggest first taking a picture with your iPhone or iPad and verifying the attachment is sideways. Once you figure out which way holding the iPhone or iPad creates a sidways image you can add the code and duplicate the test. This will verify if the hack is working.
 
No apple products here (my choice) ;) . I am more concerned if the code will actually work on the server or if not, whether or not it will throw an error message. I may have a few minutes today to give it a try and be the guinea pig in all this. :D

Should be in core!

Should be, but there may be a reason that it can't. XF would make the assumption that the server has ImageMagick installed. One workaround might be to specifically state that IM is installed in the board options, or have XF detect it.
 
Thanks for the offer! Once I get around to doing this, I'll drop you a note. I'm working on a stack of assignments for one of my courses right now...
 
@AndyB I can not for the life of me to seem to get this to work. I tried modding the file two different times and the photo's still upload rotated. I have imagick installed
 
@AndyB I can not for the life of me to seem to get this to work. I tried modding the file two different times and the photo's still upload rotated. I have imagick installed

Please verify you have this setting enabled:

Admin CP -> Options -> Attachments

pic001.webp
 
Last edited:
This hack will fix the problem when images uploaded from an iPhone or iPad end up in the wrong orientation (sideways).

You must have ImageMagick installed.

The code snippet below is for Xenforo v1.2.x.

library/Xenforo/Upload.php

Line #184

PHP:
protected function _checkImageState()
{
  
    // start hack  
  
    exec("/usr/bin/mogrify -auto-orient $this->_tempFile");
  
    // end hack
is this for 1.2.3?

have it on line 195

Code:
$imageInfo = @getimagesize($this->_tempFile);
if (!$imageInfo)
{
if (in_array($this->_extension, array('gif', 'jpg', 'jpe', 'jpeg', 'png')))
{
$this->_errors['extension'] = new XenForo_Phrase('the_uploaded_file_was_not_an_image_as_expected');
}
return;
}
// start hack

exec("/usr/bin/mogrify -auto-orient $this->_tempFile");

// end hack
 
is this for 1.2.3?

have it on line 195

I don't have XenForo v1.2.3 downloaded yet, so can can't tell you the exact line. But put the code directly under the function.

PHP:
	/**
	 * Checks the state of the upload to determine if it's
	 * a valid image.
	 */
	protected function _checkImageState()
	{
		
		// start hack	
		
		exec("/usr/bin/mogrify -auto-orient $this->_tempFile");
		
		// end hack
 
is this for 1.2.3?

have it on line 195

Code:
$imageInfo = @getimagesize($this->_tempFile);
if (!$imageInfo)
{
if (in_array($this->_extension, array('gif', 'jpg', 'jpe', 'jpeg', 'png')))
{
$this->_errors['extension'] = new XenForo_Phrase('the_uploaded_file_was_not_an_image_as_expected');
}
return;
}
// start hack

exec("/usr/bin/mogrify -auto-orient $this->_tempFile");

// end hack
Just an FYI - but I had it as this code states and it worked, not sure why it changed its location from line 195 to your most recent post.
 
Glad to hear it's working for you, Tommy.

Yes either location for the code addition is fine, it's just easier to describe inserting it where I now show.
 
Last edited:
It's the second time I had to update this hack in the PHP file due to 2 Xenforo update releases. Which overwrite your code. Would be nice if it could be an add on. ;)
 
this should be core a long time ago! my forum is based on photo uploads and 7,000 visitors per day are using iphones/ipads

whats up K&M? :)
 
If you are having problems with this not working on your server, please make sure you have access to the following file:

PHP:
/usr/bin/mogrify
 

Similar threads

Back
Top Bottom