Jump to content

Read EXIF Orientation & Rotate Image


mcfc4heatons

Recommended Posts

Hi,

I want to create a function that reads the exif orientation data of an image and rotates it as required - needs to run before before the resize function here, I'm using GD library if that is relevant:

function resize_gd($sourceFileName, $folder, $destinationFileName, $newWidth, $newHeight, $keepProportion)
	{
		$newWidth = (int)$newWidth;
		$newHeight = (int)$newHeight;
		if (!$this->gdInfo >= 1 || !$this->checkGdFileType($sourceFileName)) {
			return false;
		}
		$img = &$this->getImg($sourceFileName);
		if ($this->hasError()) {
			return false;
		}
		$srcWidth = ImageSX($img);
		$srcHeight = ImageSY($img);

		if ( $keepProportion && ($newWidth != 0 && $srcWidth<$newWidth) && ($newHeight!=0 && $srcHeight<$newHeight) ) {
			if ($sourceFileName != $folder . $destinationFileName) {
				@copy($sourceFileName, $folder . $destinationFileName);
			}
			return true;
		}
		
		if ($keepProportion == true) {
			if ($newWidth != 0 && $newHeight != 0) {
				$ratioWidth = $srcWidth/$newWidth;
				$ratioHeight = $srcHeight/$newHeight;
				if ($ratioWidth < $ratioHeight) {
					$destWidth = $srcWidth/$ratioHeight;
					$destHeight = $newHeight;
				} else {
					$destWidth = $newWidth;
					$destHeight = $srcHeight/$ratioWidth;
				}
			} else {
				if ($newWidth != 0) {
					$ratioWidth = $srcWidth/$newWidth;
					$destWidth = $newWidth;
					$destHeight = $srcHeight/$ratioWidth;
				} else if ($newHeight != 0) {
					$ratioHeight = $srcHeight/$newHeight;
					$destHeight = $newHeight;
					$destWidth = $srcWidth/$ratioHeight;
				} else {
					$destWidth = $srcWidth;
					$destHeight = $srcHeight;
				}
			}
		} else {
			$destWidth = $newWidth; 
			$destHeight = $newHeight; 
		}
		$destWidth = round($destWidth);
		$destHeight = round($destHeight);
		if ($destWidth < 1) $destWidth = 1;
		if ($destHeight < 1) $destHeight = 1;
		
		$destImage = &$this->getImageCreate($destWidth, $destHeight);
Edited by mcfc4heatons
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.