WordPress常规的图片是通过后台添加媒体上传到本地的。但是有些时候我们复制的图片到编辑器,希望直接发布的时候保存在本地,当然有类似的插件可以实现,这里有一个无插件的代码也可以实现的。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96  | 
						function save_preview_first_pic($post_id,$src) {             $wp_upload_dir = wp_upload_dir();             set_time_limit(0);             $ch = curl_init();             curl_setopt($ch, CURLOPT_HEADER, false);             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);             curl_setopt($ch, CURLOPT_MAXREDIRS,20);             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);             $ecp_options = $_SERVER['HTTP_HOST'];                 if (isset($src) && strpos($src, $ecp_options) === false) {                     $file_info = wp_check_filetype(basename($src), null);                     if ($file_info['ext'] == false) {                         date_default_timezone_set('PRC');                         $file_name = date('YmdHis-').dechex(mt_rand(100000, 999999)).'.tmp';                     } else {                         $file_name = dechex(mt_rand(100000, 999999)) . '-' . basename($src);                     }                     curl_setopt($ch, CURLOPT_URL, $src);                     $file_path = $wp_upload_dir['path'] . '/' . $file_name;                     $img = fopen($file_path, 'wb');                     curl_setopt($ch, CURLOPT_FILE, $img);                     $img_data  = curl_exec($ch);                     fclose($img);                     if (file_exists($file_path) && filesize($file_path) > 0) {                         $t   = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);                         $arr = explode('/', $t);                         if (pathinfo($file_path, PATHINFO_EXTENSION) == 'tmp') {                             $file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'tmp');                         } elseif (pathinfo($file_path, PATHINFO_EXTENSION) == 'webp') {                             $file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'webp');                         }                        $attachment = ecp_get_attachment_post(basename($file_path), $wp_upload_dir['url'] . '/' . basename($file_path));                        $attach_id = wp_insert_attachment($attachment, ltrim($wp_upload_dir['subdir'] . '/' . basename($file_path), '/'), $post_id);                         $attach_data = wp_generate_attachment_metadata($attach_id, $file_path);                         $ss = wp_update_attachment_metadata($attach_id, $attach_data);                     }                 }             curl_close($ch); } function ecp_handle_ext($file, $type, $file_dir, $file_name, $ext) {     switch ($ext) {         case 'tmp':             if (rename($file, str_replace('tmp', $type, $file))) {                 if ('webp' == $type) {                     return ecp_image_convert('webp', 'jpeg', $file_dir . '/' . str_replace('tmp', $type, $file_name));                 }                 return $file_dir . '/' . str_replace('tmp', $type, $file_name);             }         case 'webp':             if ('webp' == $type) {                 return ecp_image_convert('webp', 'jpeg', $file);             } else {                 if (rename($file, str_replace('webp', $type, $file))) {                     return $file_dir . '/' . str_replace('webp', $type, $file_name);                 }             }         default:             return $file;     } } //转换webp格式为jpg格式 function ecp_image_convert($from='webp', $to='jpeg', $image) {     $im = imagecreatefromwebp($image);     if (imagejpeg($im, str_replace('webp', 'jpeg', $image), 100)) {         try {             unlink($image);         } catch (Exception $e) {             $error_msg = sprintf('Error removing local file %s: %s', $image,                 $e->getMessage());             error_log($error_msg);         }     }     imagedestroy($im);     return str_replace('webp', 'jpeg', $image); } //构造上传附件的数据 function ecp_get_attachment_post($filename, $url) {     $file_info  = wp_check_filetype($filename, null);     return array(         'guid'           => $url,         'post_type'      => 'attachement',         'post_mime_type' => $file_info['type'],         'post_title'     => preg_replace('/\.[^.]+$/', '', $filename),         'post_content'   => '',         'post_status'    => 'inherit'     ); }  | 
					
这个可以实现发布的时候自动上传本地远程图片到本地。
	声明:本站发布的所有资源均来自于互联网,所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
如侵犯到您的权益,请及时通知我们,我们会及时处理。邮箱:505289534@qq.com
		




评论(0)