0

アップロードした画像を元の形式 (jpg、jpeg rpng など) に変換したいのですが、これらの画像のサイズを幅 150px、高さ 210px に変更する必要があります。コピー中にサイズを変更することはできますか、それとも変換する必要がありますか?

これは失敗しました:

    $uploaddir1 = "/home/myweb/public_html/temp/sfds454.png";
    $uploaddir2 = "/home/myweb/public_html/images/sfds454.png";

    $cmd = "/usr/bin/ffmpeg -i $uploaddir1 -vframes 1 -s 150x210 -r 1 -f mjpeg $uploaddir2";
    @exec($cmd);
4

2 に答える 2

3

ffmpegの代わりにgdを使用できます。画像を変換またはサイズ変更するには、次の例を参照してください: http://ryanfait.com/resources/php-image-resize/resize.txt

gdの PHPライブラリ:

http://php.net/manual/en/function.imagecopyresampled.php

そのページには、サイズ変更スクリプトのサンプルもいくつかあります。

于 2013-02-10T13:46:34.513 に答える
1

私は最近、この問題だけを解決する必要があり、この単純なキャッシュ ソリューションを実装しました。

<?php
function send($name, $ext) {
    $fp = fopen($name, 'rb');
    // send the right headers
    header("Content-Type: image/$ext");
    header("Content-Length: " . filesize($name));

    // dump the picture and stop the script
    fpassthru($fp);
    exit;
}

error_reporting(E_ALL);
ini_set('display_errors', 'On');

if (isset($_REQUEST['fp'])) {
    $ext = pathinfo($_REQUEST['fp'], PATHINFO_EXTENSION);

    $allowedExt = array('png', 'jpg', 'jpeg');
    if (!in_array($ext, $allowedExt)) {
        echo 'fail';
    }

    if (!isset($_REQUEST['w']) && !isset($_REQUEST['h'])) {
        send($_REQUEST['fp']);
    }
    else {
        $w = $_REQUEST['w'];
        $h = $_REQUEST['h'];

        //use height, width, modification time and path to generate a hash
        //that will become the file name
        $filePath = realpath($_REQUEST['fp']);
        $cachePath = md5($filePath.filemtime($filePath).$h.$w);
        if (!file_exists("tmp/$cachePath")) {
            exec("gm convert -quality 80% -colorspace RGB -resize " .$w .'x' . $h . " $filePath tmp/$cachePath");
        }
        send("tmp/$cachePath", $ext);

    }
}
?>

私が気づいたいくつかのこと:

  1. cuda 処理で変換をテストしていませんが、Graphicsmagick は imagemagick よりもはるかに高速に変換されます。
  2. 最終製品では、言語のネイティブ グラフィック ライブラリを使用して、このコードを ASP に再実装しました。これもはるかに高速でしたが、メモリ不足エラーが発生すると壊れてしまいました (私のワークステーションでは問題なく動作しましたが、4GB RAM サーバーでは動作しませんでした)。
于 2013-02-11T01:12:16.893 に答える