This should be an edit to the question, but my accounts are messing me up, so this is a new account and I can't edit questions.
Here's the code I've written (stolen).
<?php
// overkill? definitely
define('MIRROR_HORIZONTAL', 1);
define('MIRROR_VERTICAL', 2);
define('MIRROR_BOTH', 3);
define('CR', PHP_EOL); // carriage return
// memory: allow script to run for longer than usual time limit
set_time_limit(0);
ini_set('memory_limit', -1);
clearstatcache(); // there's no reason why this info should be cached anyway, but it does no harm to be sure
// plain and simple, baby; plain and simple
header('Content-Type: text/plain');
// find the brats
$c = array();
recursive_glob(getcwd(), $c);
function recursive_glob($path, &$c) {
$a = glob($path . '/*');
foreach ($a as $v) {
if (is_dir($v)) {
recursive_glob($v, $c);
} else {
$c[] = $v;
}
}
}
$u = count($c);
echo CR . 'Total number of files: ' . $u . CR . CR;
// uploaded from digital camera: a mixture of photos and videos (also, the recursive_glob() function above will find this file itself)
$i = 0; // count iterator: total images
$j = 0; // count iterator: total non-images
$k = 0; // count iterator: total images rotated
foreach ($c as $v) {
$ext = substr($v, -3);
if ($ext == 'jpg') { // I wanted to do this properly, by using finfo functions to check MIME types, but I couldn't get it to work and I don't actually need it
$i++;
echo clean_image_rotation($v, $k) . CR; // do the work
} else {
$j++;
echo $v . ' - not JPEG' . CR; // filter out the videos
}
}
echo CR . 'Image clean complete.' . CR . CR . 'Total files found: ' . $u . CR . 'Total JPEGs found: ' . $i . CR . 'Total images rotated: ' . $k . CR . CR . 'END';
function clean_image_rotation($file, &$k) {
// http://stackoverflow.com/questions/1718847/use-php-or-perl-to-properly-rotate-jpeg-images
// http://ie.php.net/manual/en/function.exif-read-data.php#76964
// http://bytes.com/topic/php/answers/641481-thumbnail-orientation
$exif = exif_read_data($file, 'IFD0');
if (!$exif) $exif = array();
$ort = isset($exif['IFD0']['Orientation']) ? $exif['IFD0']['Orientation'] : 1;
switch($ort) {
case 1:
$do = 'nothing';
break;
case 2:
$do = 'horizontal flip';
flipImage($file, MIRROR_HORIZONTAL);
$k++;
break;
case 3:
$do = '180 rotate left';
rotateImage($file, 180);
$k++;
break;
case 4:
$do = 'vertical flip';
flipImage($file, MIRROR_VERTICAL);
$k++;
break;
case 5:
$do = 'vertical flip + 90 rotate right';
flipImage($file, MIRROR_VERTICAL);
rotateImage($file, -90);
$k++;
break;
case 6:
$do = '90 rotate right';
rotateImage($file, -90);
$k++;
break;
case 7:
$do = 'horizontal flip + 90 rotate right';
flipImage($file, MIRROR_HORIZONTAL);
rotateImage($file, -90);
$k++;
break;
case 8:
$do = '90 rotate left';
rotateImage($file, 90);
$k++;
break;
}
return $file . ' - ' . $do;
}
function flipImage($src, $dest, $type) {
return;
// http://php.net/manual/en/function.imagecopy.php#42803
$imgsrc = imagecreatefromjpeg($src);
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);
$imgdest = imagecreatetruecolor($width, $height);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
switch ($type) {
case MIRROR_HORIZONTAL:
imagecopy($imgdest, $imgsrc, $width - $x - 1, $y, $x, $y, 1, 1);
break;
case MIRROR_VERTICAL:
imagecopy($imgdest, $imgsrc, $x, $height - $y - 1, $x, $y, 1, 1);
break;
case MIRROR_BOTH:
imagecopy($imgdest, $imgsrc, $width - $x - 1, $height - $y - 1, $x, $y, 1, 1);
break;
default:
return false;
}
}
}
imagejpeg($imgdest, $dest);
imagedestroy($imgsrc);
imagedestroy($imgdest);
return true;
}
function rotateImage($src, $degrees) {
return;
return imagerotate($src, $degrees, 0);
}
As you can see, the images won't actually rotate in this version (both rotation functions simply return. This was for a test run, which should dump to screen what the real code would actually do. The output is a long list of filenames, and then this:
Image clean complete.
Total files found: 2046
Total JPEGs found: 1982
Total images rotated: 0
END
That's not what I was expecting. I know for a fact that some of these images have a sideways orientation, and this script should find them and fix them. Any idea what I'm doing wrong?