2013-07-16 73 views
1

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

$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 } ?> 

在后上部代码提交,并通过上述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语句,但没有成功。

回答

1

试试这个:

焦距转换率在表单字段应该是

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

,并在你的函数:

//after $id = $_GET['id']; add following 
$cropfactor = $_POST['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; 
     } 

    }  
} 

希望它有帮助。

注意:代码未经测试。

+0

其中计数器是'$ count',是否正确? – Alex

+0

是的,正确的。但$柜台应该从0开始,而不是1。或者只是$ cropfactor [$ count-1] – Ula

+0

即时通讯对不起,不理解,为什么它应该从0开始,而不是1 ?.此外,我补充说,我正在通过中间的php文件处理函数,如上所示。 $ _POST是否会继承该功能。例如,如果我有:这种形式提交给一个图像处理程序,其中包含函数调用(但不包括在框架包含的另一个php文件中的函数逻辑本身) – Alex

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