001 | function ecp_save_post($post_id, $post) { |
003 | if ($post->post_status == 'publish' ) { |
004 | $p = '/<img.*[\s]src=[\"|\'](.*)[\"|\'].*>/iU' ; |
005 | $num = preg_match_all($p, $post->post_content, $matches); |
007 | $wp_upload_dir = wp_upload_dir(); |
010 | curl_setopt($ch, CURLOPT_HEADER, false ); |
011 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); |
012 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false ); |
013 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); |
014 | curl_setopt($ch, CURLOPT_MAXREDIRS,20); |
015 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); |
017 | $ecp_options = $_SERVER[ 'HTTP_HOST' ]; |
018 | foreach ($matches[1] as $src) { |
019 | if (isset($src) && strpos($src, $ecp_options) === false ) { |
020 | $file_info = wp_check_filetype( basename ($src), null); |
021 | if ($file_info[ 'ext' ] == false ) { |
022 | date_default_timezone_set( 'PRC' ); |
023 | $file_name = date ( 'YmdHis-' ).dechex(mt_rand(100000, 999999)). '.tmp' ; |
025 | $file_name = dechex(mt_rand(100000, 999999)) . '-' . basename ($src); |
027 | curl_setopt($ch, CURLOPT_URL, $src); |
028 | $file_path = $wp_upload_dir[ 'path' ] . '/' . $file_name; |
029 | $img = fopen($file_path, 'wb' ); |
030 | curl_setopt($ch, CURLOPT_FILE, $img); |
031 | $img_data = curl_exec($ch); |
034 | if (file_exists($file_path) && filesize($file_path) > 0) { |
035 | $t = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
036 | $arr = explode( '/' , $t); |
037 | if (pathinfo($file_path, PATHINFO_EXTENSION) == 'tmp' ) { |
038 | $file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir[ 'path' ], $file_name, 'tmp' ); |
039 | } elseif (pathinfo($file_path, PATHINFO_EXTENSION) == 'webp' ) { |
040 | $file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir[ 'path' ], $file_name, 'webp' ); |
042 | $post->post_content = str_replace($src, $wp_upload_dir[ 'url' ] . '/' . basename ($file_path), $post->post_content); |
043 | $attachment = ecp_get_attachment_post( basename ($file_path), $wp_upload_dir[ 'url' ] . '/' . basename ($file_path)); |
044 | $attach_id = wp_insert_attachment($attachment, ltrim($wp_upload_dir[ 'subdir' ] . '/' . basename ($file_path), '/' ), 0); |
045 | $attach_data = wp_generate_attachment_metadata($attach_id, $file_path); |
046 | $ss = wp_update_attachment_metadata($attach_id, $attach_data); |
051 | $wpdb->update( $wpdb->posts, array( 'post_content' => $post->post_content), array( 'ID' => $post->ID)); |
056 | function ecp_handle_ext($ file , $ type , $file_dir, $file_name, $ext) { |
059 | if (rename($ file , str_replace( 'tmp' , $ type , $ file ))) { |
060 | if ( 'webp' == $ type ) { |
061 | return ecp_image_convert( 'webp' , 'jpeg' , $file_dir . '/' . str_replace( 'tmp' , $ type , $file_name)); |
063 | return $file_dir . '/' . str_replace( 'tmp' , $ type , $file_name); |
066 | if ( 'webp' == $ type ) { |
067 | return ecp_image_convert( 'webp' , 'jpeg' , $ file ); |
069 | if (rename($ file , str_replace( 'webp' , $ type , $ file ))) { |
070 | return $file_dir . '/' . str_replace( 'webp' , $ type , $file_name); |
078 | function ecp_image_convert($from= 'webp' , $to= 'jpeg' , $image) { |
079 | $im = imagecreatefromwebp($image); |
080 | if (imagejpeg($im, str_replace( 'webp' , 'jpeg' , $image), 100)) { |
083 | } catch (Exception $e) { |
084 | $error_msg = sprintf( 'Error removing local file %s: %s' , $image, |
086 | error_log($error_msg); |
091 | return str_replace( 'webp' , 'jpeg' , $image); |
094 | function ecp_get_attachment_post($filename, $url) { |
095 | $file_info = wp_check_filetype($filename, null); |
098 | 'post_type' => 'attachement' , |
099 | 'post_mime_type' => $file_info[ 'type' ], |
100 | 'post_title' => preg_replace( '/\.[^.]+$/' , '' , $filename), |
101 | 'post_content' => '' , |
102 | 'post_status' => 'inherit' |
105 | add_action( 'save_post' , 'ecp_save_post' , 120, 2); |
|
发表评论
评论列表(0条)