Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save asifkhankadiwala/5012914a596f25137dce073a0c427dfa to your computer and use it in GitHub Desktop.

Select an option

Save asifkhankadiwala/5012914a596f25137dce073a0c427dfa to your computer and use it in GitHub Desktop.

Revisions

  1. @cowboymathu cowboymathu revised this gist Sep 8, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion correctImageOrientation.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,11 @@
    This funtion correct all the images' orientation in a given path or directory.
    Run: php -r "require 'image_rotate.php'; correctImageOrientation('test/test1/');"
    Run: php -r "require 'correctImageOrientation.php'; correctImageOrientation('test/');"
    or
    php -r "require 'correctImageOrientation.php'; correctImageOrientation('test/test1');"
    or
    php -r "require 'correctImageOrientation.php'; correctImageOrientation('test');"
    */
    function correctImageOrientation($directory) {
    $scanned_directory = array_diff(scandir($directory), array('..', '.'));
  2. @cowboymathu cowboymathu revised this gist Sep 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion correctImageOrientation.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php
    /*
    Correct image orientation v1..0
    Correct image orientation v1.0
    Author: Mathuvathanan Mounasamy
  3. @cowboymathu cowboymathu revised this gist Sep 8, 2014. 1 changed file with 48 additions and 44 deletions.
    92 changes: 48 additions & 44 deletions correctImageOrientation.php
    Original file line number Diff line number Diff line change
    @@ -17,55 +17,59 @@ function correctImageOrientation($directory) {
    print_r($scanned_directory);
    echo "</pre>\r\n";
    foreach ($scanned_directory as &$file) {

    $filen = explode(".", $file);
    $ext = end($filen);
    try {

    $exif = @exif_read_data($directory.$file);

    $orientation = $exif['Orientation'];

    if (isset($orientation) && $orientation != 1){
    switch ($orientation) {
    case 3:
    $deg = 180;
    break;
    case 6:
    $deg = 270;
    break;
    case 8:
    $deg = 90;
    break;
    }

    if ($deg) {

    if (is_dir($directory."/".$file)) {
    correctImageOrientation($directory."/".$file);
    } else {
    $filen = explode(".", $file);
    $ext = end($filen);
    try {

    // If png
    if ($ext == "png") {
    $img_new = imagecreatefrompng($directory.$file);
    $img_new = imagerotate($img_new, $deg, 0);

    // Save rotated image
    imagepng($img_new,$directory.$file);
    }else {
    $img_new = imagecreatefromjpeg($directory.$file);
    $img_new = imagerotate($img_new, $deg, 0);
    $exif = @exif_read_data($directory."/".$file);

    $orientation = $exif['Orientation'];

    if (isset($orientation) && $orientation != 1){
    switch ($orientation) {
    case 3:
    $deg = 180;
    break;
    case 6:
    $deg = 270;
    break;
    case 8:
    $deg = 90;
    break;
    }

    if ($deg) {

    // Save rotated image
    imagejpeg($img_new,$directory.$file,80);
    // If png
    if ($ext == "png") {
    $img_new = imagecreatefrompng($directory."/".$file);
    $img_new = imagerotate($img_new, $deg, 0);

    // Save rotated image
    imagepng($img_new,$directory.$file);
    }else {
    $img_new = imagecreatefromjpeg($directory."/".$file);
    $img_new = imagerotate($img_new, $deg, 0);

    // Save rotated image
    imagejpeg($img_new,$directory."/".$file,80);
    }
    }
    echo "<pre>";
    print_r("image changed: \r\n");
    print_r($directory."/".$file);
    echo "</pre>\r\n";
    }
    echo "<pre>";
    print_r("image affected: \r\n");
    print_r($directory.$file);
    echo "</pre>\r\n";

    } catch (Exception $e) {
    echo "error:";
    echo $e;
    echo "error";
    }

    } catch (Exception $e) {
    echo "error:";
    echo $e;
    echo "error";
    }
    }
    unset($file);
  4. @cowboymathu cowboymathu revised this gist Sep 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion correctImageOrientation.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function correctImageOrientation($directory) {
    print_r("scanned directory: \r\n");
    print_r($scanned_directory);
    echo "</pre>\r\n";
    foreach ($scanned_directory as &$file) {
    foreach ($scanned_directory as &$file) {

    $filen = explode(".", $file);
    $ext = end($filen);
  5. @cowboymathu cowboymathu renamed this gist Sep 8, 2014. 1 changed file with 0 additions and 0 deletions.
  6. @cowboymathu cowboymathu created this gist Sep 8, 2014.
    74 changes: 74 additions & 0 deletions correct_image_orientation.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    <?php
    /*
    Correct image orientation v1..0
    Author: Mathuvathanan Mounasamy
    Licensed under the MIT license
    This funtion correct all the images' orientation in a given path or directory.
    Run: php -r "require 'image_rotate.php'; correctImageOrientation('test/test1/');"
    */
    function correctImageOrientation($directory) {
    $scanned_directory = array_diff(scandir($directory), array('..', '.'));
    echo "<pre>";
    print_r("scanned directory: \r\n");
    print_r($scanned_directory);
    echo "</pre>\r\n";
    foreach ($scanned_directory as &$file) {

    $filen = explode(".", $file);
    $ext = end($filen);
    try {

    $exif = @exif_read_data($directory.$file);

    $orientation = $exif['Orientation'];

    if (isset($orientation) && $orientation != 1){
    switch ($orientation) {
    case 3:
    $deg = 180;
    break;
    case 6:
    $deg = 270;
    break;
    case 8:
    $deg = 90;
    break;
    }

    if ($deg) {

    // If png
    if ($ext == "png") {
    $img_new = imagecreatefrompng($directory.$file);
    $img_new = imagerotate($img_new, $deg, 0);

    // Save rotated image
    imagepng($img_new,$directory.$file);
    }else {
    $img_new = imagecreatefromjpeg($directory.$file);
    $img_new = imagerotate($img_new, $deg, 0);

    // Save rotated image
    imagejpeg($img_new,$directory.$file,80);
    }
    }
    echo "<pre>";
    print_r("image affected: \r\n");
    print_r($directory.$file);
    echo "</pre>\r\n";
    }

    } catch (Exception $e) {
    echo "error:";
    echo $e;
    echo "error";
    }
    }
    unset($file);
    }

    ?>