4

Blueimp /jQuery-File-Uploaderと Amazon S3 プラグインを使用しており、すべて正常に機能していますが、画像のサイズを変更して、最短で 640px を超えないようにする必要があります。

私の現在のコードは

   global $s3;
    if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
        return "";
    }
    $upload = isset($_FILES['files']) ? $_FILES['files'] : null;
    $info = array();
    if ($upload && is_array($upload['tmp_name'])) {
        foreach($upload['tmp_name'] as $index => $value) {
            $fileTempName = $upload['tmp_name'][$index];

            $file_name = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index]);
            $extension=end(explode(".", $file_name));
            $rand = rand(1,100000000);
            $sha1 = sha1($rand);
            $md5 = md5($sha1);
            $filename = substr($md5, 0, 8);
            $fileName=$filename.".".$extension;    

            $fileName = $prefix.str_replace(" ", "_", $fileName);
            $response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
            if ($response->isOK()) {
                $info[] = getFileInfo($bucket, $fileName);
            } else {
                //     echo "<strong>Something went wrong while uploading your file... sorry.</strong>";

            }
        }

そして、私はこのPHPを少し書きましたが、どうすればこの2つを連携させることができるかわかりません。

$image = new Imagick('test2.jpg');
$imageprops = $image->getImageGeometry();

$w=$imageprops['width'];
$h=$imageprops['height'];
$edge = min($w,$h);
$ratio = $edge / 640;

$tWidth = ceil($w / $ratio);
$tHeight = ceil($h / $ratio);

if ($imageprops['width'] <= 640 && $imageprops['height'] <= 640) {
    // don't upscale
} else {
    $image->resizeImage($tWidth,$tHeight,imagick::FILTER_LANCZOS, 0.9, true);
}
$image->writeImage("test2-resized.jpg");

どんな助けでもありがたく受け取られます、ありがとう

4

1 に答える 1

5

これは、OP のメッセージ内のすべてのコードが正しいという前提に基づいており、単に要求どおりに再配置しただけです。

更新: 4 つの賛成票 (これまでのところ) は、OP がコードに関してだけでなく、問題の大きさに関しても正しかったことを示しているようです。私は当然のこととして OSS を行っているので、ぜひ、これがあなたにとって興味があるかどうかを明示的にお知らせください。コメント、またはそれらの任意の組み合わせ)。


function resize($imgName, $srcName)
{
    $image = new Imagick($imgName);
    $imageprops = $image->getImageGeometry();

    $w=$imageprops['width'];
    $h=$imageprops['height'];
    $edge = min($w,$h);
    $ratio = $edge / 640;

    $tWidth = ceil($w / $ratio);
    $tHeight = ceil($h / $ratio);

    if ($imageprops['width'] <= 640 && $imageprops['height'] <= 640) {
        return $imgName;
    } else {
        $image->resizeImage($tWidth,$tHeight,imagick::FILTER_LANCZOS, 0.9, true);
    }
    $extension=end(explode(".", $srcName));
    // Change "/tmp" if you're running this on Windows
    $tmpName=tempnam("/tmp", "resizer_").".".$extension;
    $image->writeImage($tmpName);
    return $tmpName
}

global $s3;
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
    return "";
}
$upload = isset($_FILES['files']) ? $_FILES['files'] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
    foreach($upload['tmp_name'] as $index => $value) {
        $file_name = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index]);
        $fileTempName = resize($upload['tmp_name'][$index], $file_name);
        $extension=end(explode(".", $file_name));
        $rand = rand(1,100000000);
        $sha1 = sha1($rand);
        $md5 = md5($sha1);
        $filename = substr($md5, 0, 8);
        $fileName=$filename.".".$extension;    

        $fileName = $prefix.str_replace(" ", "_", $fileName);
        $response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
        if ($response->isOK()) {
            $info[] = getFileInfo($bucket, $fileName);
        } else {
            //     `echo "<strong>Something went wrong while uploading your file... sorry.</strong>";`

        }
        unlink($fileTempName);
}
于 2012-11-02T10:14:23.877 に答える