Blueimp Jquery File Upload S3 & Resize

amazon-s3, blueimp, imagick, jquery, php

Solution

This is based on the assumption that all code in the OP's message was correct, and simply re-arranged it as requested.

Update: four upvotes (so far) seems to indicate the OP was correct not only regarding the code, but also regarding the magnitude of the issue. I do OSS as a matter of course, so by all means, let me know explicitly if this is of any interest to you so we can improve on this on github (any action is fine -- upvote the question, upvote the answer, post a comment, or any combination thereof).

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

Problem

I am using the Blueimp/jQuery-File-Uploader and the Amazon S3 plugin that is available for it and all is working out fine however I need to resize my images to be no more or less that 640px on the shortest side. my current code is ``` 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>"; } } ``` And I have written this bit of PHP however I am not sure as to how I can get the two working together. ``` $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"); ``` any help will be gratefully received, thanks

Original source