Resizing portrait photos

I have a script which resizes a bunch of images. It has worked fine for a few years now except on some portrait images which come out as landscape.

I am only now trying to fix it by checking the exif data and rotating the image if it is portrait. The image is rotated but the resulting file is still landscape. I attach an example.

Any ideas where Iā€™m going wrong? :slightly_smiling_face:

Here is my code:

function resizer($fileName, $maxWidth, $maxHeight, $fixedWidth, $fixedHeight, $oldDir, $newDir, $quality) {

  $file = $oldDir.'/'.$fileName;
  $fileDest = $newDir.'/'.$fileName;
  list($width, $height) = getimagesize($file);

  if ( $fixedWidth ) {
    $newWidth  = $fixedWidth;
    $newHeight = ($newWidth / $width) * $height;

  } elseif ( $fixedHeight ) {
    $newHeight = $fixedHeight;
    $newWidth  = ($newHeight / $height) * $width;

  } elseif ( $width < $height ) {            // image is portrait
    $newHeight = $maxHeight;
    $newWidth  = ($newHeight / $height) * $width;

  } elseif ( $width > $height ) {            // image is landscape
    $newWidth  = $maxWidth;
    $newHeight = ($newWidth / $width) * $height;

  } else {                                              // image is square
    $newWidth  = $maxHeight;
    $newHeight = $maxHeight;
  }

  $extn = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  $imageDest = imagecreatetruecolor($newWidth, $newHeight);

  // it's a jpeg
  if ( $extn == 'jpg' or $extn == 'jpeg' ) {
    $imageSrc = imagecreatefromjpeg($file);

    // rotate image if necessary
    $exif = exif_read_data($file);
    if (isset($exif['Orientation'])) {
      switch ($exif['Orientation']) {
        case 3:
          $imageSrc = imagerotate($imageSrc, 180, 0);
          break;
        case 6:
          $imageSrc = imagerotate($imageSrc, -90, 0);
          list($height, $width) = array($width, $height);
          list($newHeight, $newWidth) = array($newWidth, $newHeight);
          break;
        case 8:
          $imageSrc = imagerotate($imageSrc, 90, 0);
          list($height, $width) = array($width, $height);
          list($newHeight, $newWidth) = array($newWidth, $newHeight);
          break;
      }
    }

    if ( imagecopyresampled($imageDest, $imageSrc, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) ) {
      imagejpeg($imageDest, $fileDest, $quality);
      imagedestroy($imageSrc);
      imagedestroy($imageDest);
      return true;
    }
    return false;
  }

  // it's a png
  if ( $extn == 'png' ) {
    imagealphablending($imageDest, false);
    imagesavealpha($imageDest, true);
    $imageSrc = imagecreatefrompng($file);
    if ( imagecopyresampled($imageDest, $imageSrc, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) ) {
      imagepng($imageDest, $fileDest, ($quality / 10) - 1);
      imagedestroy($imageSrc);
      imagedestroy($imageDest);
      return true;
    }
    return false;
  }

}


  $maxWidth    = $_POST['maxWidth'];
  $maxHeight   = $_POST['maxHeight'];
  $fixedWidth  = $_POST['fixedWidth'];
  $fixedHeight = $_POST['fixedHeight'];
  $oldDir      = $_POST['oldDir'];
  $newDir      = $_POST['newDir'];
  $quality     = $_POST['quality'];

  // create destination directory if it doesn't exist
  if ( !file_exists($newDir) )
    mkdir($newDir);

  // check source directory exists, open it and get all files
  if ( !file_exists($oldDir) )
    die('Source directory does not exist.');
  $folder = opendir($oldDir);
  while ( $file = readdir($folder) ) {
    if ( $file[0] != '.' && $file[0] != '..' )
      $files[$file] = $file;
  }
  if ( ( $filess = @scandir($oldDir) ) && count($filess) <= 2 )
    die('Source directory is empty.');

  echo '<p>Settings: fixed width ', $fixedWidth, ', fixed height ', $fixedHeight, ', max width ', $maxWidth,
    ', max height ', $maxHeight, ', quality ', $quality, '%</p>', PHP_EOL;
  echo '<ul>', PHP_EOL;

  // process each file
  foreach ( $files as $key => $value ) {
    $ext = strtolower(pathinfo($key, PATHINFO_EXTENSION));
    if ( $ext == 'jpg' or $ext == 'jpeg' or $ext == 'png' ) {
      if ( resizer($key, $maxWidth, $maxHeight, $fixedWidth, $fixedHeight, $oldDir, $newDir, $quality) )
        echo '<li>Resized image: ', $key, '</li>', PHP_EOL;
      else
        echo '<li>** Failed to resize image: ', $key, ' **</li>', PHP_EOL;
    } else {
      echo '<li>** ', $key, ' is not a jpeg or png **</li>', PHP_EOL;
    }
  }
  echo '</ul>', PHP_EOL;

  closedir($folder);
  echo '<p>*** Finished ***</p>', PHP_EOL;

Edit. Oops, forgot the pic.

IMG_1233

Gotit!

All I needed was to move the imagecreatetruecolor till after I had recalculated $newWidth and $newHeight.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.