Auto Image Rotation

Discussion forum for Extension Writers regarding Extension Development.
Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Auto Image Rotation

Post by Heavendenied81 »

In my Board with Version 3.1.10 i made some modifications for Auto image resize and rotation because many of my users upload pictures taken with a Smartphone Cam which have wrong Orientaion (wide instead of portrait and in exif date there is the right orientation).
As i noticed the resize is not needed anymore because it is done automaticly if i configure a max width and hight for pictures in ACP. Unfurtunatly the auto rotation is not done automaticly.
Can anyone help me how in can make this work in 3.2.0?
In 3.1.10 i used this:
viewtopic.php?f=72&t=2171923#p13241074

Thanks in advance.
User avatar
AmigoJack
Registered User
Posts: 6108
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack »

Adapting that code you now need to open /phpbb/files/filespec.php.

Find (at the end of the file, should be the last curly bracket):

Code: Select all

}
Before, add:

Code: Select all

	/*** BEGIN 2017-02-21
		https://www.phpbb.com/community/viewtopic.php?f=556&t=2410601 ***/
	function ImageOrient() {
		if( preg_match( '#^j(p([eg]|eg)|fif)$#i', $this-> extension ) ) {  // Only JPEG supported so far
			$oDest= imagecreatefromjpeg( $this-> destination_file );
			if( function_exists( 'exif_read_data' ) ) {
				$aExif= exif_read_data( $this-> destination_file );
			} else {
				// http://php.net/manual/de/function.exif-read-data.php#117355
				if( !preg_match( '#\x12\x01\x03\x00\x01\x00\x00\x00(.)\x00\x00\x00#', file_get_contents( $this-> destination_file ), $aMatch ) ) return;
    				$aExif= array( 'Orientation'=> ord( $aMatch[1] ) );
			}

			switch( $aExif['Orientation'] ) {
				case 2:  // Horizontal flip
				case 4: //  Vertical flip
				case 5:  // Vertical flip & 90° rotate clockwise
				case 7:  // Horizontal flip & 90° rotate counter clockwise
					$bFlip= true; 
				break;
				
				default:
					$bFlip= false;
				break;
			}

			switch( $aExif['Orientation'] ) {

				case 3:  // 180° rotate
				case 4: //  Vertical flip
					$iRotate= 180;
				break;

				case 5:  // Vertical flip & 90° rotate clockwise
				case 8:  // 90° rotate clockwise
					$iRotate= 90; 
				break;

				case 6:  // 90° rotate counter clockwise
				case 7:  // Horizontal flip & 90° rotate counter clockwise
					$iRotate= -90;
				break;

				default:
					$iRotate= 0;
				break;
			}

			if( $bFlip ) {
				if( !function_exists( 'imageflip' )
				 || !imageflip( $oImage, IMG_FLIP_HORIZONTAL )
				  ) {
					$this-> ImageFlip( $oDest );
				}
			}
			if( $iRotate ) $oDest= imagerotate( $oDest, $iRotate, 0 );

			imagejpeg( $oDest, $this-> destination_file, 100 );
		}
	}		 
  
	// Fallback for PHP below 5.5
	function ImageFlip( &$oImage, $iLeft= 0, $iTop= 0, $iWidth= 0, $iHeight= 0 ) {
		if( $iWidth< 1 ) $iWidth= imagesx( $oImage );
		if( $iHeight< 1 ) $iHeight= imagesy( $oImage );
   
		if( function_exists( 'imageistruecolor' )
		 && imageistruecolor( $oImage )
		  ) {
			$oNew= imagecreatetruecolor( 1, $iHeight );
		} else {
			$oNew = imagecreate( 1, $iHeight );
		}
  
		$iStripe= $iLeft+ $iWidth- 1;
  
		for( $iX= (int)floor( ($iWidth- 1)/ 2 ); $iX>= 0; $iX-- ) {
			// Backup right stripe.
			imagecopy( $oNew, $oImage, 0, 0, $iStripe- $iX, $iTop, 1, $iHeight );
  
			// Copy left stripe to the right.
			imagecopy( $oImage, $oImage, $iStripe- $iX, $iTop, $iLeft+ $iX, $iTop, 1, $iHeight );
  
			// Copy backuped right stripe to the left.
			imagecopy( $oImage, $oNew, $iLeft+ $iX,  $iTop, 0, 0, 1, $iHeight );
		}
  
		imagedestroy( $oNew );
	}
	/*** END 2017-02-21 ***/

Find:

Code: Select all

				$this->filesystem->phpbb_chmod($this->destination_file, $chmod);
Before, add:

Code: Select all

				/*** BEGIN 2017-02-21
					https://www.phpbb.com/community/viewtopic.php?f=556&t=2410601 ***/
				$this-> ImageOrient();
				/*** END 2017-02-21 ***/

Untested.
Last edited by AmigoJack on Wed Feb 22, 2017 5:08 pm, edited 1 time in total.
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 »

Thanks for your Help.
But it does not work.
If i try to upload an Image now it says "HTTP Fehler"
User avatar
AmigoJack
Registered User
Posts: 6108
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack »

Can't reproduce. Attach said picture to your reply, so I can try it myself.
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 »

The failure appears every time i try to upload an file, regardles if it is an image or a pdf or anything else. If i delte the code you posted everything works fine again.
Find attached a picture of the failure as well as "my" Version of the filespec.php
Image
Attachments

[The extension php has been deactivated and can no longer be displayed.]

User avatar
AmigoJack
Registered User
Posts: 6108
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack »

Your version is still using the old code plus partly my comments around it - that's something I never tested. And I was expecting you to edit an unmodified version of said file, not to add my code to your old one.
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 »

Thank you very much fpr your help! I'm very bad in programming so i do not really understand where an why i have to change the code...
Sorry, i tested so much and now it seems have attached the wrong Version of the File.
I now used a completely unmodified file and modified it like you posted it.
Now it works if i upload a image with correct orientation. If i upload an file with wrong orientation i get a litte yellow triangle behind the upload (insted of the green hook). If i click on it i get the massage "Error parsing server response"
Attachments
2017-02-22.png

[The extension php has been deactivated and can no longer be displayed.]

User avatar
AmigoJack
Registered User
Posts: 6108
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack »

...which leads us back to the point of
AmigoJack wrote: Tue Feb 21, 2017 4:08 pmAttach said picture to your reply, so I can try it myself.
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 »

Ah sorry, i think i missunderstood which picture you mean and postetd one of the failure.
Her is one of this pictures with wrong orientation.
Attachments
20160826_130856334_iOS.jpg
User avatar
AmigoJack
Registered User
Posts: 6108
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack »

Found the bug: in my code find -1 and replace with 0 - worked for me.
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 »

Yes! Works perfect now.
Thank you very much!
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Auto Image Rotation

Post by 3Di »

This can be done as a standalone extension, no core edits.

Code: Select all

	/**
	* Event to modify uploaded file before submit to the post
	*
	* @event core.modify_uploaded_file
	* @var	array	filedata	Array containing uploaded file data
	* @var	bool	is_image	Flag indicating if the file is an image
	* @since 3.1.0-RC3
	*/
	$vars = array(
		'filedata',
		'is_image',
	);
./includes/functions_posting.php

I will maybe include this option also in my attachments extension, using some bit from here.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
mpoznjak
Registered User
Posts: 2
Joined: Tue Mar 21, 2017 6:20 pm

Re: Auto Image Rotation

Post by mpoznjak »

3Di wrote: Wed Mar 08, 2017 3:19 am This can be done as a standalone extension, no core edits.
I will maybe include this option also in my attachments extension, using some bit from here.
Any news on this one?
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Auto Image Rotation

Post by 3Di »

mpoznjak wrote: Sat Nov 11, 2017 7:57 pm
3Di wrote: Wed Mar 08, 2017 3:19 am This can be done as a standalone extension, no core edits.
I will maybe include this option also in my attachments extension, using some bit from here.
Any news on this one?
Yup, viewtopic.php?f=456&t=2477256
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
shortmort37
Registered User
Posts: 656
Joined: Sat Aug 26, 2006 8:40 pm
Location: Upper Darby, PA, USA
Name: Dan Morton
Contact:

Re: Auto Image Rotation

Post by shortmort37 »

Since the EXIR extension is no longer available, I'll be looking to incorporate this mod. A couple of questions, though:

1) Is the EXIF Orientation set = 1 to reflect a rotation or flip by the GD operations?

2) By the time the reorientation takes place on destination_file - has any corresponding thumbnail for the image file already been produced?

I assume that for efficiency's sake, phpBB produces the thumb before destination_file is first written; then it too will need reorientation based on the header (providing preserve_header = true).

Or am I missing something?
Post Reply

Return to “Extension Writers Discussion”