交换随机数的名称

时间:2015-04-09 19:52:23

标签: php

我有下面的代码,它是将图像保存到我的WordPress安装中的脚本的一部分。问题是脚本使用原始名称保存图像,例如“originalimage.jpg”。我想要的是保存的图像得到一个随机数,如“randomnumber.jpg”,我想知道如何做到这一点,以便如果它已经使用过一次,它可以再取一个已经取的随机数。到目前为止,我尝试了什么打破了代码。有人可以帮忙吗?

function wpr_save_all_images($content,$keyword,$insert) {
    $path = wp_upload_dir();
    $path = $path['baseurl'];
    $html = $content;
    if ( stripos( $html, '<img' ) !== false ) {

        $regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im';
        preg_match_all( $regex, $html, $matches );

        if ( is_array( $matches ) && ! empty( $matches ) ) {
            $new = array();
            $old = array();

            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . 'wp-admin/includes/media.php');
            require_once(ABSPATH . 'wp-admin/includes/image.php');

            foreach( $matches[2] as $img ) {

                if (  stripos( $img, "images/buynow-big.gif" ) !== false ) {
                    continue;
                }       

                $size = getimagesize($img);
                $width = $size[0];
                $height = $size[1];
                if (  $width < 2 || $height < 2 ) {
                    continue;
                }               

                if (  stripos( $img, $path ) !== false ) {
                    continue;
                }

                $tmp = download_url( $img );

                preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $img, $matches);

                $newfn = str_replace(array("%2B", "%52", "%20", "%5"), "B", basename($matches[0]));

                $oofnm = basename($matches[0]);

                if($newfn != $oofnm) {              

                    $newfn2 = str_replace(array(".jpg", ".png", ".gif"), "", $newfn);

                    $tmppath = pathinfo( $tmp );                                                        // extract path parts
                    $newpth = $tmppath['dirname'] . "/". $newfn2 . "." . $tmppath['extension'];   

                    rename($tmp, $newpth);                                                                 // renames temp file on server
                    $tmp = $newpth;                 
                }

                $file_array['name'] = $newfn;
                $file_array['tmp_name'] = $tmp;

                // If error storing temporarily, unlink
                if ( is_wp_error( $tmp ) ) {
                    @unlink($file_array['tmp_name']);
                    $file_array['tmp_name'] = '';
                    continue;
                }

                $id = media_handle_sideload( $file_array, $insert );

                if ( ! is_wp_error( $id ) ) {
                    $url  = wp_get_attachment_url( $id );
                    $thumb = wp_get_attachment_thumb_url( $id );
                    array_push( $new, $url );
                    array_push( $old, $img );
                }

                //echo "OLD: ". $img . "<br>WAWA: " . $wawawa . "<br>NEW: " . $url . " <br>";                                                       
            }

            if( !empty( $new ) ) {
                $content = str_ireplace( $old, $new, $html );
                $post_args = array( 'ID' => $insert, 'post_content' => $content, );
                if (!empty($content))
                    $post_id = wp_update_post( $post_args );
            }
        }
    } 
    return true;
}

0 个答案:

没有答案