0

我在 Ubuntu 上使用 F-Spot 旋转了一些照片(JPEG 文件),然后将它们通过 FTP 上传到我的网站。这似乎工作得很好。但是,如果这些图像在 Web 浏览器中打开,它们不会显示为已旋转。如果我将它们下载到 Windows Vista 机器并用那里的任何标准程序打开它们,它们也不会出现。我怀疑 F-Spot 通过修改 exif 数据或类似数据来旋转图像,而不是通过实际旋转图像。

所以我想要一个可以在我的 Web 服务器(即 PHP 或 Perl)上运行的小函数,它将接受文件路径数组,检查图像,并旋转需要旋转的那些,覆盖原始文件。

我知道一些 PHP 但不知道 Perl。


在寻找这个问题是否已经被问过的过程中,我遇到了一些想法。经过一些试验和错误后,我可能能够使用 glob()、exif_read_data() 和 imagerotate() 将某些东西组合在一起。我明天试试。但现在我要睡觉了。

4

3 回答 3

3

在 Perl 中,您可以使用Image::Magick模块旋转图像。还有一个PHP 界面和一个命令行界面(我认为)。如果您只是旋转几个图像,您可能最好使用命令行版本。

这是一个简单的 Perl 脚本,用于顺时针旋转图像(并保留文件的修改时间):

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;
}
于 2009-11-11T23:41:53.127 回答
3

直接从 PHP 网站复制: http: //us.php.net/manual/en/function.imagerotate.php

此示例将图像旋转 180 度 - 上下颠倒。

<?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);
?>

要将文件输出到新文件名,请使用前面的示例:

// Output
imagejpeg($rotate, "new-" . $filename);
?>
于 2009-11-11T23:45:46.850 回答
2

在 Perl 中,我认为你想要"exiftool -Orientation"。PHP 等效项似乎可以通过"exif_read_data"访问。

于 2009-11-16T15:16:23.943 回答