Question

I used F-Spot on Ubuntu to rotate some photos (JPEG files) before I FTPed them up to my website. This seemed to work just fine. However, if those images are opened in a web browser, they do not show as rotated. Nor do they if I download them to a Windows Vista machine and open them with any standard program there. I suspect that F-Spot rotates images by modifying the exif data or similar, not by actually rotating the images.

So I want a little function which will run on my web server (i.e., PHP or Perl) which will accept an array of file paths, examine the images, and rotate those which need to be rotated, overwriting the original file.

I know some PHP but no Perl.


In the course of searching to see whether this question had already been asked, I came across some ideas. I might be able, after some trial and error, to knock something together using glob(), exif_read_data(), and imagerotate(). I'll try tomorrow. But now I'm going to bed.

Was it helpful?

Solution

In Perl, I think you want "exiftool -Orientation". The PHP equivalent seems to be accessible through "exif_read_data".

OTHER TIPS

Copying this directly from the PHP website: http://us.php.net/manual/en/function.imagerotate.php

This example rotates an image 180 degrees - upside down.

<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;

// Content type
header('Content-type: image/jpeg');

// Load
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

// Output
imagejpeg($rotate);
?>

To output the file to a new filename, using previous example:

// Output
imagejpeg($rotate, "new-" . $filename);
?>

In Perl you can rotate images using the Image::Magick module. There's a PHP interface too, and a command-line interface (I think). If you're just rotating a few images you're probably best off with the command line version.

Here's a simple Perl script to rotate images clockwise (and preserves the files' modification time):

use strict;
use warnings;
use Image::Magick;

die "no filename specified!\n" if not @ARGV;

foreach my $filename (@ARGV)
{
    print "Processing: $filename\n";

    # Get the file's last modified time for restoring later
    my $mtime = (stat $filename)[9];

    my $image = Image::Magick->new;
    my $result = $image->Read($filename);
    warn "$result" if $result;
    $result = $image->Rotate(degrees => 90.0);
    warn "$result" if $result;
    $result = $image->Write($filename);
    warn "$result" if $result;

    # Restore the mtime
    utime time, $mtime, $filename;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top