使用 class.upload 上传 php 图片

Posted

技术标签:

【中文标题】使用 class.upload 上传 php 图片【英文标题】:php image upload with class.upload 【发布时间】:2013-07-16 16:45:08 【问题描述】:

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

$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 处理

代码sn-p:

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 语句,但它不起作用。

【问题讨论】:

【参考方案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;
        

        

希望对你有帮助。

注意:代码未经测试。

【讨论】:

是的,正确的。但在你的情况下,$counter 应该从 0 而不是 1 开始。或者只是 $cropfactor[$count-1] 对不起,不明白,为什么要从0而不是1开始?。此外,我补充说,我正在通过一个中间 php 文件处理该函数,如上所示。 $_POST 是否会延续到函数中。例如,如果我有:这个表单提交给包含函数调用的图像处理程序(但不是函数逻辑本身,它位于框架包含的另一个 php 文件中) 它应该从 0 开始,因为在 html 表单中,您将输入 - 【参考方案2】:

您已经准备好大部分代码。首先是更改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");

【讨论】:

以上是关于使用 class.upload 上传 php 图片的主要内容,如果未能解决你的问题,请参考以下文章

php+ajax+flash 实现图片裁剪上传

H5 使用input标签上传图片给后台

input上传文件处理

H5压缩图片上传(FileReader +canvas)

隐藏iframe无刷新上传文件

vue---- ElementUI 实现上传Excel