如何从服务侧面加载图像

时间:2014-05-11 作者:Christopher Carvache

我目前正在开发一个创建虚拟内容的WordPress插件。我还想让插件使用如下服务创建随机图像http://lorempixel.com. 然而,问题是,在访问服务时,不会生成实际的文件名。生成如下随机图像:

http://lorempixel.com/1200/700/

如何使用media_handle_sideload() 实现这一点的功能?

2 个回复
SO网友:Otto

简而言之,你不需要。像sideload这样的函数用于将图像导入WordPress本身,并使用所有正常的媒体库内容。对于外部服务上的图像,请使用普通img标记。

如果要从URL复制图像,请将其侧向加载。否则不会。

SO网友:Christopher Carvache

可以我已经解决了这个问题。。。

@奥托-看起来我所要做的就是用一点卷曲魔法来做这个。。。

以下代码是高级wordpress虚拟页面/帖子/图像/文本创建者的一部分

function render_page() {
    if ($_POST[\'delete\']) {
        $num_posts_deleted = 0;
        $num_images_deleted = 0;
        global $wpdb;

        $SQL = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = \'_wpl_dummy\'";
        $rows = $wpdb->get_results($SQL);

        foreach ($rows as $row) {
            $num_posts_deleted++;
            wp_delete_post( $row->post_id, true );
        }

        $SQL = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = \'_wpl_dummy_image\'";
        $rows = $wpdb->get_results($SQL);

        foreach ($rows as $row) {
            $num_images_deleted++;
            wp_delete_attachment( $row->post_id, true );
        }

    }
    if ($_POST[\'create\']) {
        $num_posts_created = 0;
        // Build the options array
        $options = array();

        if ($_POST[\'number_of_paragraphs\'])
            $options[] = $_POST[\'number_of_paragraphs\'];

        if ($_POST[\'paragraph_length\'])
            $options[] = $_POST[\'paragraph_length\'];

        if ($_POST[\'decorate\'])
            $options[] = \'decorate\';

        if ($_POST[\'links\'])
            $options[] = \'links\';

        if ($_POST[\'ul\'])
            $options[] = \'ul\';

        if ($_POST[\'ol\'])
            $options[] = \'ol\';

        if ($_POST[\'dl\'])
            $options[] = \'dl\';

        if ($_POST[\'bq\'])
            $options[] = \'bq\';

        if ($_POST[\'code\'])
            $options[] = \'code\';

        if ($_POST[\'headers\'])
            $options[] = \'headers\';

        if ($_POST[\'ac\'])
            $options[] = \'allcaps\';

        if ($_POST[\'pr\'])
            $options[] = \'prude\';

        if ($_POST[\'fi\'])
            $featured_image = 1;

        if ($_POST[\'fi_width\'])
            $featured_image_width = $_POST[\'fi_width\'];

        if ($_POST[\'fi_height\'])
            $featured_image_height = $_POST[\'fi_height\'];

        $options = implode(\'/\', $options);


        // Match up
        $args = array(
            \'public\'=>true
        );
        $post_types = get_post_types( $args, \'objects\', \'and\' );


        if ( $featured_image )
            require_once( ABSPATH . \'wp-admin/includes/image.php\' );

        foreach ( $post_types as $post_type ) {
            if ( $post_type->name != \'attachment\') {
                if ( $_POST[$post_type->name]) {
                    for ($i=1; $i <= $_POST[\'number_of_posts_to_create\']; $i++) {
                        // Get the random text
                        $curl = curl_init();
                        curl_setopt_array($curl, array(
                            CURLOPT_RETURNTRANSFER => 1,
                            CURLOPT_URL => \'http://loripsum.net/api/\' . $options
                        ));
                        $resp = curl_exec($curl);
                        curl_close($curl);

                        // Massage our data
                        preg_match("/<h1 ?.*>(.*)<\\/h1>/", $resp, $matches);
                        $title = $matches[1];
                        $body = str_replace($matches[0], \'\', $resp);

                        // Create the post
                        $post = array(
                            \'post_content\'  => $body,
                            \'post_title\'    => $title,
                            \'post_type\'     => $post_type->name,
                            \'post_status\'   => \'publish\'
                        );  
                        $post_id = wp_insert_post( $post, $wp_error );
                        update_post_meta($post_id, \'_wpl_dummy\', \'1\');


                        // Add and attach a featured image if necessary
                        if ( $featured_image ) {
                            $curl = curl_init();
                            curl_setopt_array($curl, array(
                                CURLOPT_RETURNTRANSFER => 1,
                                CURLOPT_URL => \'http://lorempixel.com/\' . $featured_image_width . \'/\' . $featured_image_height . \'/\'
                            ));
                            $img_raw = curl_exec($curl);
                            curl_close($curl);

                            $upload_dir = wp_upload_dir();

                            // place our file on the server in the appropriate directory
                            $file = $upload_dir[\'path\'] . \'/wpl_\' . time() . \'_dummy.jpg\';
                            file_put_contents ( $file , $img_raw );

                            // Check the type of tile. We\'ll use this as the \'post_mime_type\'.
                            $filetype = wp_check_filetype( basename( $file ), null );;

                            // Prepare an array of post data for the attachment.
                            $attachment = array(
                                \'guid\'           => $upload_dir[\'url\'] . \'/\' . basename( $file ), 
                                \'post_mime_type\' => $filetype[\'type\'],
                                \'post_title\'     => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $file ) ),
                                \'post_content\'   => \'\',
                                \'post_status\'    => \'inherit\'
                            );

                            // Insert the attachment.
                            $attach_id = wp_insert_attachment( $attachment, $file, $post_id );              

                            // Generate the metadata for the attachment, and update the database record.
                            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
                            wp_update_attachment_metadata( $attach_id, $attach_data );


                            // Update the featured image
                            update_post_meta($post_id, \'_thumbnail_id\', $attach_id);
                            update_post_meta($attach_id, \'_wpl_dummy_image\', \'1\');
                        }

                        $num_posts_created++;
                    }
                }
            }
        }


    }
}?>

显然更多,但这处理了我的目标。感谢您朝着正确的方向推动。

结束