【问题标题】:Blueimp Jquery File Upload S3 & ResizeBlueimp Jquery 文件上传 S3 和调整大小
【发布时间】:2012-10-19 18:33:50
【问题描述】:

我正在使用 Blueimp/jQuery-File-Uploader 和可用的 Amazon S3 插件,一切正常,但是我需要将图像大小调整为最短边不超过 640 像素。

我当前的代码是

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

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

【问题讨论】:

  • 上面的所有代码都经过测试并且可以工作了吗?
  • 是的,这一切都很好

标签: php jquery amazon-s3 imagick blueimp


【解决方案1】:

这是基于假设 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);
}

【讨论】:

  • 看起来不错,但我收到一个错误我可以将我的文件发布到某个地方供您检查吗?
  • 我已经发布了代码@codepad.viper-7.com/NQQYwd 可以告诉我吗?
  • 是的,很抱歉,“,这可能不是最好的方法。
  • @JustinErswell:好的,我想我现在明白了。 :)
  • 酷豆在玩它我现在似乎遇到了这个错误 PHP 致命错误:未捕获的异常 'ImagickException' 并带有消息'无法打开图像`/tmp/resizer_sSs8jZ./tmp/phphnKgi9':@ /var/www/portal/imgup/awssdk.php 中的错误/blob.c/OpenBlob/2587':65\n堆栈跟踪:\n#0 /var/www/portal/imgup/awssdk.php(65):Imagick ->writeimage('/tmp/resizer_sS...')\n#1 /var/www/portal/imgup/awssdk.php(78): resize('/tmp/phphnKgi9')\n#2 /var/ www/portal/imgup/index.php(46): uploadFiles('cdmwl2012', 'FR24010/5093a01...', '59')\n#3 {main}\n 扔进 /var/www/portal/第 65 行的 imgup/awssdk.php
猜你喜欢
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 2018-06-27
  • 2013-06-01
  • 1970-01-01
  • 2012-05-09
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多