上传后获取上传附件图像的路径

时间:2011-03-18 作者:Dwayne Charrington

在您使用媒体上传屏幕在Wordpress中上传附件后,是否有一个钩子或过滤器可以在上传图像后运行,在那里我可以获得上传图像的路径,以便我可以分析它?

我正在构建一个插件,将分析上传后的图像,然后用它在图像中找到的平均颜色标记图像。唯一的问题是,我不知道我可以使用什么钩子,在图像上传后立即启动,然后我可以获得新上传文件的路径。

任何帮助都将不胜感激。

3 个回复
最合适的回答,由SO网友:Dwayne Charrington 整理而成

原来我在同事的帮助下解决了自己的问题。上载媒体后或编辑媒体时调用的两个筛选器为;\'“添加\\u附件”和“编辑\\u附件”。这是我正在使用的代码,然后检查附件是否是图像(示例中省略了代码)。

function analyse_attachment($attachment_ID)
{          
    $attachment = get_attached_file($attachment_ID); // Gets path to attachment
    update_post_meta($attachment_ID, "image_rgb", $the_colour);
}

add_action("add_attachment", \'analyse_attachment\');
add_action("edit_attachment", \'analyse_attachment\');
显然,我遗漏了一些与问题无关的东西。但在您上传或编辑附件后,会立即调用该代码。

SO网友:Bainternet

您可以使用两个过滤器:attachment_fields_to_save它得到两个参数($post,$attachment),所以:

add_filter(\'attachment_fields_to_save\',your_image_analyse_function);

function your_image_analyse_function($post, $attachment){
  $attachment[\'url\']
  //do your stuff
}
以及media_send_to_editor 它获取3个参数($html、$send\\u id、$attachment),并在单击“发送到”编辑器后激发,以便再次使用$attachment。

add_filter(\'media_send_to_editor\',your_image_analyse_function);

function your_image_analyse_function($html, $send_id, $attachment){
  $attachment[\'url\']
  //do your stuff
}

SO网友:Arvind07

HTML Markup:

<p>
  <label for="custom-upload">Upload New Image:</label>
  <input type="file" tabindex="3" name="custom-upload" id="custom-upload" />
</p>
<?php
  /*Retrieving the image*/
  $attachment = get_post_meta($postid, \'custom_image\');

  if($attachment[0]!=\'\')
  {
   echo wp_get_attachment_link($attachment[0], \'thumbnail\', false, false);
  }

?>

Uploading the image:

<?php
global $post; /*Global post object*/
$post_id = $post->ID; /*Geting current post id*/
$upload = $_FILES[\'upload\']; /*Receive the uploaded image from form*/
add_custom_image($post_id, $upload); /*Call image uploader function*/

function add_custom_image($post_id, $upload)
{
 $uploads = wp_upload_dir(); /*Get path of upload dir of wordpress*/

 if (is_writable($uploads[\'path\']))  /*Check if upload dir is writable*/
 {
  if ((!empty($upload[\'tmp_name\'])))  /*Check if uploaded image is not empty*/
  {
   if ($upload[\'tmp_name\'])   /*Check if image has been uploaded in temp directory*/
   {
    $file=handle_image_upload($upload); /*Call our custom function to ACTUALLY upload the image*/

    $attachment = array  /*Create attachment for our post*/
    (
      \'post_mime_type\' => $file[\'type\'],  /*Type of attachment*/
      \'post_parent\' => $post_id,  /*Post id*/
    );

    $aid = wp_insert_attachment($attachment, $file[\'file\'], $post_id);  /*Insert post attachment and return the attachment id*/
    $a = wp_generate_attachment_metadata($aid, $file[\'file\'] );  /*Generate metadata for new attacment*/
    $prev_img = get_post_meta($post_id, \'custom_image\');  /*Get previously uploaded image*/
    if(is_array($prev_img))
    {
     if($prev_img[0] != \'\')  /*If image exists*/
     {
      wp_delete_attachment($prev_img[0]);  /*Delete previous image*/
     }
    }
    update_post_meta($post_id, \'custom_image\', $aid);  /*Save the attachment id in meta data*/

    if ( !is_wp_error($aid) ) 
    {
     wp_update_attachment_metadata($aid, wp_generate_attachment_metadata($aid, $file[\'file\'] ) );  /*If there is no error, update the metadata of the newly uploaded image*/
    }
   }
  }
  else
  {
   echo \'Please upload the image.\';
  }
 }
}

function handle_image_upload($upload)
{
 global $post;

        if (file_is_displayable_image( $upload[\'tmp_name\'] )) /*Check if image*/
        {
            /*handle the uploaded file*/
            $overrides = array(\'test_form\' => false);
            $file=wp_handle_upload($upload, $overrides);
        }
 return $file;
}
?>
结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?