蓝色魔方Jquery文件上传S3和调整大小

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 代码,但不确定如何将两者结合起来。

$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");

任何帮助都将不胜感激,谢谢。

以上的代码是否都经过测试并且运行正常? - Bogdan Stăncescu
是的,单独来看都没问题。 - Justin Erswell
1个回答

5
这是基于假设OP消息中的所有代码都是正确的,并按请求重新排列的。更新:四个赞(到目前为止)似乎表明OP不仅在代码方面正确,而且在问题的重要性方面也正确。作为常规实践,我会做OSS,请明确告诉我是否感兴趣,以便我们可以通过github改进它(任何行动都可以-点赞问题、点赞答案、发表评论或任何组合)。

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

看起来不错,但我遇到了一个错误,我可以把我的文件上传到某个地方让您检查吗? - Justin Erswell
我已经在http://codepad.viper-7.com/NQQYwd发布了代码,您能告诉我这是否可以吗? - Justin Erswell
确实关于错误,不过关于照片,我认为没问题。我会回去检查代码。 - Justin Erswell
太好了,我很高兴它对你有用。不幸的是,似乎没有任何优雅的解决方案将此代码集成到jQuery插件中,因为PHP后端似乎过于基础,无法支持这种可选图像处理。需要完全进行面向对象的重写才能使其有意义。 - Bogdan Stăncescu
这个插件的支持并不好,我一直在为此苦苦挣扎,现在又遇到了更多问题。你能否建议一个更好的方法来完成这个任务? - Justin Erswell
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接