Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created February 26, 2016 20:05
Show Gist options
  • Select an option

  • Save christianromney/dfdf61538edb5b01c71b to your computer and use it in GitHub Desktop.

Select an option

Save christianromney/dfdf61538edb5b01c71b to your computer and use it in GitHub Desktop.
<?php
class FakeImage {
function rotate($deg) {
echo "Rotating $deg degrees\n";
return $this;
}
function mirror(){
echo "Mirroring\n";
return $this;
}
function copy() {
echo "Copying\n";
return $this;
}
function saveToFile($path) {
echo "Saving image to $path\n";
return $this;
}
}
class WideImage_Operation_ExifOrient
{
// orientation constants
const MIRROR = 2;
const ROTATE_180 = 3;
const ROTATE_180_MIRROR = 4;
const ROTATE_90_MIRROR = 5;
const ROTATE_90 = 6;
const ROTATE_270_MIRROR = 7;
const ROTATE_270 = 8;
function execute($image, $orientation)
{
if (!isset($image)) throw new Exception('No image given');
if (!isset($orientation)) throw new Exception('No orientation given');
switch ($orientation) {
case self::MIRROR:
return $image->mirror();
case self::ROTATE_180:
return $image->rotate(180);
case self::ROTATE_180_MIRROR:
return $image->rotate(180)->mirror();
case self::ROTATE_90_MIRROR:
return $image->rotate(90)->mirror();
case self::ROTATE_90:
return $image->rotate(90);
case self::ROTATE_270_MIRROR:
return $image->rotate(-90)->mirror();
case self::ROTATE_270:
return $image->rotate(-90);
default:
return $image->copy();
}
}
}
// fake data for testing
$exif = array('Orientation' => 7);
$image = new FakeImage();
$imageWithText = "/tmp/image.jpg";
// use the class
$transformation = new WideImage_Operation_ExifOrient();
$transformation->execute($image, $exif['Orientation']);
$image->saveToFile($imageWithText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment