2

サーバー上のフォルダーに、日付順に整理された多数の画像(500,000以上)があります。サブフォルダー(サム)上の各JPGファイルをコピーしてトリミングするPHPスクリプトを作成しましたが、PHPはマルチスレッドをサポートしていないため、非常に低速です。

進め方についてアドバイスをお願いします。Pythonはこれに適したオプションですか?良いツールはありますか、またはサイズ変更機能を改善するにはどうすればよいですか?

私のPHPコードもご覧ください

4

3 に答える 3

3

スレッドを直接使用する代わりに、スレッドをシミュレートして、問題なくPHPで実行できます。実際、PHPにはネイティブスレッドがありません(最終的にはライブラリを使用できますが、それはあなたの場合にはあまり役に立ちません)。

あなたのコードでは、呼び出す代わりに:

static::Crop($file,$destination,$tn_w = 300,$tn_h =200,$quality = 100,$wmsource = false);

やってみませんか:

$array = array($file, $destination, $tn_w = 300, $tn_h = 200, $quality = 100, $wmsource = 0);
$command = "/usr/bin/php crop.php";
foreach ($array as $arg)
{
  $command .= ' ' . escapeshellarg($arg);
}
exec("$command &"); // note the & which release your execution
usleep(100000);

そして、トリミング関数を内部crop.phpに配置し、次のように呼び出します。

list($exec, $file, $destination, $tn_w, $tn_h, $quality, $wmsource) = $argv;
static::Crop($file,$destination,$tn_w = 300,$tn_h =200,$quality = 100,$wmsource = false);

これでうまくいきます。

usleepを回避し、一度に実行するクロップの数を制御したい場合は、ファイルを使用してミューテックスをシミュレートすることもできます。これは実際にはあなた次第です。あなたは間違いなくPHPでそのような仕事をすることができます。

于 2012-11-05T22:10:12.937 に答える
0

ただし、phpcgiはマルチスレッドをサポートしています。なぜexec()を使わないのですか?または、シェルスクリプトを使用して変換php cgiを使用できますか?

于 2012-11-05T22:09:52.063 に答える
0

このクラスを使用する

<?php
class thumbnail_images {

// get
var $PathImgOld;
var $PathImgNew;
var $NewWidth;
var $NewHeight;

// tmp
var $mime;

function imagejpeg_new ($NewImg,$path_img) {
    if ($this->mime == 'image/jpeg' or $this->mime == 'image/pjpeg') imagejpeg($NewImg,$path_img);
    elseif ($this->mime == 'image/gif') imagegif($NewImg, $path_img);
    elseif ($this->mime == 'image/png') imagepng($NewImg, $path_img);
    else return(false);
    return(true);
}

function imagecreatefromjpeg_new($path_img) {
    if ($this->mime == 'image/jpeg' or $this->mime == 'image/pjpeg') $OldImg = imagecreatefromjpeg($path_img);
    elseif ($this->mime == 'image/gif') $OldImg = imagecreatefromgif($path_img);
    elseif ($this->mime == 'image/png') $OldImg = imagecreatefrompng($path_img);
    else return(false);
    return($OldImg);
}

function create_thumbnail_images() {
    $PathImgOld = $this->PathImgOld;
    $PathImgNew = $this->PathImgNew;
    $NewWidth = $this->NewWidth;
    $NewHeight = $this->NewHeight;

    $Oldsize = @getimagesize($PathImgOld);
    $this->mime = $Oldsize['mime'];
    $OldWidth = $Oldsize[0];
    $OldHeight = $Oldsize[1];

    if ($NewHeight == '' and $NewWidth != '') {
        $NewHeight = ceil(($OldHeight * $NewWidth) / $OldWidth);
    }
    elseif ($NewWidth == '' and $NewHeight != '') {
        $NewWidth = ceil(($OldWidth * $NewHeight) / $OldHeight);
    }
    elseif ($NewHeight == '' and $NewWidth == '') {
        return(false);
    }

    $OldHeight_castr = ceil(($OldWidth * $NewHeight) / $NewWidth);
    $castr_bottom = ($OldHeight - $OldHeight_castr) / 2;

    $OldWidth_castr = ceil(($OldHeight * $NewWidth) / $NewHeight);
    $castr_right = ($OldWidth - $OldWidth_castr) / 2;

    if ($castr_bottom>0) {
        $OldWidth_castr = $OldWidth;
        $castr_right = 0;
    }
    elseif ($castr_right>0) {
        $OldHeight_castr = $OldHeight;
        $castr_bottom = 0;
    }
    else {
        $OldWidth_castr = $OldWidth;
        $OldHeight_castr = $OldHeight;
        $castr_right = 0;
        $castr_bottom = 0;
    }

    $OldImg = $this->imagecreatefromjpeg_new($PathImgOld);
    if ($OldImg) {
        $NewImg_castr = imagecreatetruecolor($OldWidth_castr, $OldHeight_castr);
        if ($NewImg_castr) {
            imagecopyresampled($NewImg_castr, $OldImg, 0, 0, $castr_right, $castr_bottom, $OldWidth_castr, $OldHeight_castr, $OldWidth_castr, $OldHeight_castr);
            $NewImg = imagecreatetruecolor($NewWidth, $NewHeight);
            if ($NewImg) {
                imagecopyresampled($NewImg, $NewImg_castr, 0, 0, 0, 0, $NewWidth, $NewHeight, $OldWidth_castr, $OldHeight_castr);
                imagedestroy($NewImg_castr);
                imagedestroy($OldImg);
                if (!$this->imagejpeg_new($NewImg, $PathImgNew)) return (false);
                imagedestroy($NewImg);
            }
        }
    }
    else {
        return(false);
    }

    return(true);
}

}
?>

今すぐそれを使用してください

$width = $_REQUEST['img_width'];
$height = $_REQUEST['img_height'];
// example
$obj_img = new thumbnail_images();
$obj_img->PathImgOld = 'Old_image.jpg'; // Image for resize
$obj_img->PathImgNew = 'my_image_new_formSubURLkki.jpg'; // New Image Path
$obj_img->NewWidth = $width;
$obj_img->NewHeight = $height;
if (!$obj_img->create_thumbnail_images()) echo "error"; 
else {
    echo 'Image Maked andsave in directory';
}
于 2013-08-27T18:18:11.433 に答える