1

我正在使用来自 verot的带有class.upload的boostrap 文件上传。我所有的代码都可以正常工作,但是我想添加一个额外的功能,即有一个复选框来确定脚本如何裁剪图像。

$handle->image_ratio_crop = true; // basically zooms in on the middle (default)
$handle->image_ratio_fill = true; // preserves ratio and adds white bars where needed

如果用户检查图像旁边的 image_ratio_fill,那么我希望使用它而不是脚本中的默认值。目前我在页面上有 4 张图片上传可用。以下脚本遍历每个文件并添加执行必要的裁剪 .etc。

PHP:

function upImageSpec($table, $m_x, $m_y, $t_x, $t_y)
{
    $id = $_GET['id'];
    $img_db_name = str_replace('s', '', $table); // this will replace the s with nothing so that it is user_image_1, project_image_1 for the db insertion
    // unchanging variables
    $ext = 'jpg';
    $upload_path = FRONTEND_IMAGE_UPLOAD_PATH . slash_item($table) . slash_item($id); // will not work with /images/
    // end unchanging variables
    $files = array();
    foreach ($_FILES['userfile'] as $k => $l) {
        foreach ($l as $i => $v) {
            if (!array_key_exists($i, $files))
                $files[$i] = array();
            $files[$i][$k] = $v;
        }
    }
    $counter = 1;
    foreach ($files as $file) {
        // foreach variables
        $main_name = 'm_' . $id;
        $thumb_name = 't_' . $id;
        $count = $counter++;
        // end foreach variables
        $handle = new upload($file);
        if ($handle->uploaded) {
            // save uploaded image $m_x, $m_y
            $mi = sprintf("%s_%d", $main_name, $count);
            $full_src = REL_FRONTEND_IMAGE_UPLOAD_PATH . slash_item($table) . slash_item($id) . $mi . '.' . $ext;
            $handle->file_new_name_body = $mi;
            $handle->image_convert = $ext;
            $handle->allowed = array(
                'image/*'
            );
            $handle->file_max_size = MAX_IMAGE_FILE_SIZE;
            $handle->jpeg_quality = 95;
            $handle->image_resize = true;
            //$handle->image_ratio_crop = true;
            $handle->image_ratio_fill = true;
            $handle->image_x = $m_x;
            $handle->image_y = $m_y;
            $handle->file_overwrite = true;
            $handle->auto_create_dir = true;
            $handle->process($upload_path);
            if ($handle->processed) {
                Nemesis::update($table, "last_modified = NOW(), last_modified_by = '{$_SESSION[user_id]}', {$img_db_name}_image_{$count} = '{$full_src}'", "id = '{$id}'");
            } else {
                $msg = new Messages();
                $msg->add('e', $handle->error);
            }
            // thumbnail part, same as above, just w/ diff dimensions
        }
        unset($handle);
    }
}

HTML:

    <?php if ($totalRows_projects > 0) { ?>
    <?php $msg = new Messages(); echo $msg->display(); ?>
    <h2>Project Images<?php if (!empty($row_projects['project_name'])) { echo ': ' . $row_projects['project_name']; }?></h2>
    <form action="framework/helpers/image_handler.php?type=upload&id=<?php echo $_GET['id']; ?>" method="post" enctype="multipart/form-data">
       <?php $counter = 1; while ($row = $resultImages->fetch_assoc()) { $count = $counter++; ?>
       <div class="fileupload fileupload-new" data-provides="fileupload">
          <input type="hidden">
          <div class="fileupload-new thumbnail" style="width: 104px; height: 76px;"><span id="img<?php echo $count; ?>"><img src="<?php getThumb($row_projects["project_image_$count"]); ?>"></span></div>
          <div class="fileupload-preview fileupload-exists thumbnail" style="width: 104px; height: 76px; line-height: 50px; "></div>
          <span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" name="userfile[]" id="file<?php echo $count; ?>" class="search" multiple></span>
          <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
          <?php if (is_file(ROOT . $row_projects["project_image_$count"])) { ?><a href="javascript:void(0);" onclick="$.get('framework/helpers/image_remove.php',{ cmd: 'deleteImage', image: '<?php echo $row_projects["project_image_$count"]; ?>' } ,function(data){ $('#img<?php echo $count; ?>').html(data); });" class="fileupload-controls btn btn-danger" title="Deletion is permanent!">Remove Existing</a><?php } ?>
          <select name="cropfactor" class="select-ms">
             <option value="1">Ratio Crop</option>
             <option value="2">Ratio Fill</option>
          </select>
       </div>
       <?php } ?>
       <br><input name="submit" type="submit" id="submit" value="Add Project Image" class="btn btn-success"><a id="back" href="projects.php" class="btn btn-space" title="No changes will be made">Skip to Projects</a><?php if (!empty($row_projects['youtube_link'])) { ?><a id="yt_image" href="framework/helpers/image_handler.php?type=youtube&id=<?php echo $id; ?>&link=<?php echo $row_projects['youtube_link']; ?>" class="btn btn-space" title="Add Youtube still as a main image">Get YouTube Thumb</a><?php } ?>
       <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $MAX_IMAGE_FILE_SIZE ?>">
    </form>
    <?php } ?>

在 post 上面的代码提交并通过上面的函数通过 image_handler.php 处理

代码片段:

case 'upload':
    if (checkIDExists()) {
        upImageSpec('projects', 458, 332, 104, 76);
        redirect('', true);
    } else {
        $msg = new Messages();
        $msg->add('e', QUERY_ID_ERROR);
        redirect('', true);
    }
    break;

一个用户将如何使用该cropfactor字段,以便如果更改为比例填充默认值(比例裁剪)被关闭,并且image_ratio_fill使用。假设页面上的其他图像可能希望保留为默认值。我尝试了一个简单的 if else 语句,但它不起作用。

4

2 回答 2

1

您已经准备好大部分代码。首先是更改Select名称,使其可以与给定的图像相关联(Select每个图像一个,每个名称一个Select)。您可以依靠您使用的相同方法轻松做到这一点,即:

<select name="cropfactor<?php echo $count; ?>" class="select-ms">
    <option value="1">Ratio Crop</option>
    <option value="2">Ratio Fill</option>
</select>

选定的值将与 一起提交Form,然后通过 检索$_POST[$cropName],其中$cropName是给定的名称Select

您的 PHP 代码将变为:

$cropName = "cropfactor" . $count;
$handle->image_ratio_crop = ($_POST[$cropName] == "1");
$handle->image_ratio_fill = ($_POST[$cropName] == "2");
于 2013-07-23T15:12:47.860 回答
1

尝试这个:

表单字段中的 CropFactor 应该是

<select name="cropfactor[]" class="select-ms">
..
</select>

并在您的功能中:

//after $id = $_GET['id']; add following
$cropfactor = $_POST['cropfactor'];

并为您的每个文件检查 $cropfactor 的每个值

foreach ($files as $file) {
    // ... all other codes

    if ($handle->uploaded) {
        //...everything else        

        if($cropfactor[$counter] == 1) {
           $handle->image_ratio_crop = true;
        } else {
           $handle->image_ratio_fill = true;
        }

    }    
}

希望能帮助到你。

注意:代码未经测试。

于 2013-07-23T02:50:54.377 回答