Uploadify Minimum Image Width And Height

2019-06-08 05:59发布

问题:

So I am using the Uplodify plugin to allow users to upload multiple images at once. The problem is I need to set a minimum width and height for images. Let's say 150x150px is the smallest image users can upload.

How can I set this limitation in the Uploadify plugin? When user tries to upload smaller picture, I would like to display some error message as well.

Here is the PHP file that is called bu the plugin to upload images:

<?php

define('BASE_PATH', substr(dirname(dirname(__FILE__)), 0, -22));

// set the include path
set_include_path(BASE_PATH
                 . '/../library'
                 . PATH_SEPARATOR
                 . BASE_PATH
                 . '/library'
                 . PATH_SEPARATOR
                 . get_include_path());

// autoload classes from the library
function __autoload($class) {
    include str_replace('_', '/', $class) . '.php';
}

$configuration = new Zend_Config_Ini(BASE_PATH
                                     . '/application'
                                     . '/configs/application.ini',
                                     'development');
$dbAdapter = Zend_Db::factory($configuration->database);
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);

function _getTable($table)
{
    include BASE_PATH
    . '/application/modules/default/models/'
    . $table
    . '.php';
    return new $table();
}

$albums = _getTable('Albums');
$media = _getTable('Media');

if (false === empty($_FILES)) {

    $tempFile = $_FILES['Filedata']['tmp_name'];
    $extension = end(explode('.', $_FILES['Filedata']['name']));

    // insert temporary row into the database
    $data = array();
    $data['type'] = 'photo';
    $data['type2'] = 'public';
    $data['status'] = 'temporary';
    $data['user_id'] = $_REQUEST['user_id'];
    $paths = $media->add($data, $extension, $dbAdapter);

    // save the photo
    move_uploaded_file($tempFile,
                       BASE_PATH . '/public/' . $paths[0]);

    // create a thumbnail
    include BASE_PATH . '/library/My/PHPThumbnailer/ThumbLib.inc.php';
    $thumb = PhpThumbFactory::create(BASE_PATH . '/public/' . $paths[0]);
    $thumb->adaptiveResize(85, 85);
    $thumb->save(BASE_PATH . '/public/' . $paths[1]);

    // add watermark to the bottom right corner
    $pathToFullImage = BASE_PATH . '/public/' . $paths[0];
    $size = getimagesize($pathToFullImage);
    switch ($extension) {
        case 'gif':
            $im = imagecreatefromgif($pathToFullImage);
            break;
        case 'jpg':
            $im = imagecreatefromjpeg($pathToFullImage);
            break;
        case 'png':
            $im = imagecreatefrompng($pathToFullImage);
            break;
    }
    if (false !== $im) {
        $white = imagecolorallocate($im, 255, 255, 255);
        $font = BASE_PATH . '/public/fonts/arial.ttf';
        imagefttext($im,
                    13, // font size
                    0, // angle
                    $size[0] - 132, // x axis (top left is [0, 0])
                    $size[1] - 13, // y axis
                    $white,
                    $font,
                    'HunnyHive.com');
        switch ($extension) {
            case 'gif':
                imagegif($im, $pathToFullImage);
                break;
            case 'jpg':
                imagejpeg($im, $pathToFullImage, 100);
                break;
            case 'png':
                imagepng($im, $pathToFullImage, 0);
                break;
        }
        imagedestroy($im);
    }

    echo "1";

}

And here's the javascript:

$(document).ready(function() {    
    $('#photo').uploadify({
        'uploader'       : '/flash-uploader/scripts/uploadify.swf',
        'script'         : '/flash-uploader/scripts/upload-public-photo.php',
        'cancelImg'      : '/flash-uploader/cancel.png',
        'scriptData'     : {'user_id' : 'USER_ID'},
        'queueID'        : 'fileQueue',
        'auto'           : true,
        'multi'          : true,
        'sizeLimit'      : 2097152,
        'fileExt'        : '*.jpg;*.jpeg;*.gif;*.png',
        'wmode'          : 'transparent',
        'onComplete'     : function() {
            $.get('/my-account/temporary-public-photos', function(data) {
                $('#temporaryPhotos').html(data);
            });
        }
    });
    $('#upload_public_photo').hover(function() {
        var titles = '{';
        $('.title').each(function() {
            var title = $(this).val();
            if ('Title...' != title) {
                var id = $(this).attr('name');
                id = id.substr(5);
                title = jQuery.trim(title);
                if (titles.length > 1) {
                    titles += ',';
                }
                titles += '"' + id + '"' + ':"' + title + '"';
            }
        });
        titles += '}';
        $('#titles').val(titles);
    });
});

Now bear in mind that I know how to check images dimensions in the PHP file. But I'm not sure how to modify the javascript so it won't upload images with very small dimensions.

回答1:

You get the size of the image on this line:

$size = getimagesize($pathToFullImage);

Why not add a conditional here to check if it is at least the size you want, and if not return an error.

if($size[0] > 150 || $size[1] > 150) {
  return $someError;
}

Also it looks like there is an onError option for Uploadify which you could set: http://www.uploadify.com/documentation/

EDIT: This thread looks like it could be of some help to you: http://www.uploadify.com/forum/viewtopic.php?f=5&t=14