Blueimp Jquery文件上传S3&调整

时间:2012-10-30 12:09:04

标签: php jquery amazon-s3 imagick blueimp

我正在使用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");

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:5)

这是基于OP消息中的所有代码都是正确的假设,并根据要求重新安排它。

更新:四个upvotes(到目前为止)似乎表明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);
}