Skip to content

Instantly share code, notes, and snippets.

@djekl
Last active December 11, 2015 09:39
Show Gist options
  • Select an option

  • Save djekl/4581650 to your computer and use it in GitHub Desktop.

Select an option

Save djekl/4581650 to your computer and use it in GitHub Desktop.
Simple image rotation class
<?php
// in this example we will use a remote file
$file = 'https://raw.github.com/recurser/exif-orientation-examples/master/Landscape_6.jpg';
// adjust orientation and save a local copy (or overrite the origional)
// AdjustImgOrientation::adjust($file, TRUE);
// adjust orientation and display the image
AdjustImgOrientation::adjust($file, FALSE);
class AdjustImgOrientation
{
public function __construct($full_filename = FALSE)
{
if($full_filename !== FALSE)
self::adjust($full_filename);
}
public static function adjust($full_filename = FALSE, $save = TRUE)
{
if(!$full_filename)
return FALSE;
$exif = exif_read_data($full_filename);
if($exif && isset($exif['Orientation']))
{
$orientation = $exif['Orientation'];
if($orientation !== 1)
{
// loop through the image formats so that we read the image correctly
switch ($exif['FileType'])
{
case 1:
$img = imagecreatefromgif($full_filename);
break;
case 2:
$img = imagecreatefromjpeg($full_filename);
break;
case 3:
$img = imagecreatefrompng($full_filename);
break;
case 6:
case 15:
$img = imagecreatefromwbmp($full_filename);
break;
default:
throw new Exception("File Type not supported", 1);
return FALSE;
break;
}
// define default options
$mirror = FALSE;
$deg = 0;
// loop through the orientations and set the options
switch ($orientation)
{
case 2:
$mirror = TRUE;
break;
case 3:
$deg = 180;
break;
case 4:
$deg = 180;
$mirror = TRUE;
break;
case 5:
$deg = 270;
$mirror = TRUE;
break;
case 6:
$deg = 270;
break;
case 7:
$deg = 90;
$mirror = TRUE;
break;
case 8:
$deg = 90;
break;
}
if($deg)
$img = imagerotate($img, $deg, 0);
if($mirror)
$img = self::mirrorImage($img);
// save or overwite the origional image
if($save)
{
// check if its a remote image that we are loading
if(stripos($full_filename, 'http') !== FALSE)
{
$file_uri = explode('/', $full_filename);
$full_filename = $file_uri[count($file_uri) - 1];
}
}
else
{
$full_filename = NULL;
}
// just display the image
switch ($exif['FileType'])
{
case 1:
@header("Content-Type: Image/Gif");
imagegif($img, $full_filename);
break;
case 2:
@header("Content-Type: Image/Jpeg");
imagejpeg($img, $full_filename, 100);
break;
case 3:
@header("Content-Type: Image/Png");
imagepng($img, $full_filename, 100);
break;
case 6:
case 15:
@header("Content-Type: Image/Wbmp");
imagewbmp($img, $full_filename);
break;
}
// cleanup
imagedestroy($img);
}
}
return $full_filename;
}
private static function mirrorImage($imgsrc)
{
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);
$src_x = $width - 1;
$src_y = 0;
$src_width =- $width;
$src_height = $height;
$imgdest = imagecreateTRUEcolor($width, $height);
if(imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height))
{
return $imgdest;
}
return $imgsrc;
}
}
// eof.
@djekl

djekl commented Jan 21, 2013

Copy link
Copy Markdown
Author

Gist updated to change "Self::" to "self::" as it threw and issue when testing

@djekl

djekl commented Jan 21, 2013

Copy link
Copy Markdown
Author

Standardized my FALSE, TRUE and NULL to be all uppercase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment