Auto Image Rotation

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

Auto Image Rotation

Post by Heavendenied81 » Tue Feb 21, 2017 7:22 am

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: 4991
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack » Tue Feb 21, 2017 9:38 am

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 worst thing about censorship is ███████████

Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 » Tue Feb 21, 2017 10:26 am

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: 4991
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack » Tue Feb 21, 2017 4:08 pm

Can't reproduce. Attach said picture to your reply, so I can try it myself.
The worst thing about censorship is ███████████

Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 » Tue Feb 21, 2017 7:06 pm

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
filespec.php
(18.11 KiB) Downloaded 13 times

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

Re: Auto Image Rotation

Post by AmigoJack » Wed Feb 22, 2017 7:53 am

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 worst thing about censorship is ███████████

Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 » Wed Feb 22, 2017 9:17 am

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
filespec.php
(17.61 KiB) Downloaded 13 times

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

Re: Auto Image Rotation

Post by AmigoJack » Wed Feb 22, 2017 11:26 am

...which leads us back to the point of
AmigoJack wrote:
Tue Feb 21, 2017 4:08 pm
Attach said picture to your reply, so I can try it myself.
The worst thing about censorship is ███████████

Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 » Wed Feb 22, 2017 11:53 am

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: 4991
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Auto Image Rotation

Post by AmigoJack » Wed Feb 22, 2017 5:08 pm

Found the bug: in my code find -1 and replace with 0 - worked for me.
The worst thing about censorship is ███████████

Heavendenied81
Registered User
Posts: 11
Joined: Wed Sep 23, 2015 1:48 pm

Re: Auto Image Rotation

Post by Heavendenied81 » Thu Feb 23, 2017 8:32 am

Yes! Works perfect now.
Thank you very much!

User avatar
3Di
Registered User
Posts: 11864
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: Auto Image Rotation

Post by 3Di » Wed Mar 08, 2017 3:19 am

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.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
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 » 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?

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: Protopia and 9 guests