上传最小图像宽度和高度

时间:2009-12-21 21:38:38

标签: php jquery zend-framework uploadify

所以我使用Uplodify插件允许用户一次上传多张图片。问题是我需要为图像设置最小宽度和高度。假设150x150px是用户可以上传的最小图像。

如何在Uploadify插件中设置此限制?当用户尝试上传较小的图片时,我也想显示一些错误信息。

以下是用于上传图片的插件的PHP文件:

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

}

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

现在请记住,我知道如何检查PHP文件中的图像尺寸。但我不确定如何修改javascript,因此不会上传尺寸非常小的图片。

1 个答案:

答案 0 :(得分:4)

你可以在这一行获得图像的大小:

$size = getimagesize($pathToFullImage);

为什么不在这里添加条件来检查它是否至少是你想要的大小,如果没有返回错误。

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

您也可以设置上传的onError选项:http://www.uploadify.com/documentation/

编辑:此主题看起来对您有所帮助:http://www.uploadify.com/forum/viewtopic.php?f=5&t=14