2016-09-19 15 views
5

Cố gắng sao chép hình ảnh từ máy chủ từ xa để sử dụng làm hình thu nhỏ trong trang web wordpress của tôi. Một số hình ảnh này bị hỏng sau khi sao chép.Hình ảnh bị hỏng sau khi sao chép

Dưới đây là mã của tôi:

$url = 'http://media.cultserv.ru/i/1000x1000/'.$event->subevents[0]->image; 
$timeout_seconds = 100; 
$temp_file = download_url($url, $timeout_seconds); 

if(!is_wp_error($temp_file)) { 
    $file = array(
    'name' => basename($url), 
    'type' => wp_check_filetype(basename($url), null), 
    'tmp_name' => $temp_file, 
    'error' => 0, 
    'size' => filesize($temp_file), 
); 
    $overrides = array(
    'test_form' => false, 
    'test_size' => true, 
    'test_upload' => true, 
); 
    $results = wp_handle_sideload($file, $overrides); 
    if(empty($results['error'])) { 
    $filename = $results['file']; 
    $local_url = $results['url']; 
    $type = $results['type']; 
    $attachment = array(
     'post_mime_type' => $results['type'], 
     'post_title' => preg_replace('/.[^.]+$/', '', basename($results['file'])), 
     'post_content' => '', 
     'post_status' => 'inherit', 
     'post_type' => 'attachment', 
     'post_parent' => $pID, 
    ); 
    $attachment_id = wp_insert_attachment($attachment, $filename); 
    if($attachment_id) { 
     set_post_thumbnail($pID, $attachment_id); 
    } 
    } 
} 

Dưới đây là một ảnh chụp màn hình cho thấy những gì tôi muốn nói (Left - ảnh gốc; Ngay - sao chép trên máy chủ của tôi):

screenshot

+0

Hãy thử sử dụng '$ attachData = wp_generate_attachment_metadata ($ attachment_id, $ filename);' và 'wp_update_attachment_metadata ($ attach_id, $ attachData); 'beofre gọi' set_post_thumbnail' và xem hình ảnh có được cải thiện hay không. Hãy chắc chắn rằng 'require_once (ABSPATH. 'Wp-admin/includes/image.php');' ở đâu đó trên tập lệnh. – fyrye

+0

Vấn đề là hình ảnh, có thể truy cập bằng url được lưu trữ trong $ local_url đã bị hỏng. Đó là trước khi tệp đính kèm được tạo. –

Trả lời

1

Tôi nghĩ rằng bạn download_url($url, $timeout_seconds) chức năng không hoạt động đúng (bạn không bắt được mạng/lỗi khác, đó là lý do tại sao bạn có hình ảnh bị hỏng), tôi cũng không nghĩ rằng tham số hết thời gian thực sự cần thiết để tải xuống url ...

Để sửa lỗi này nó tốt hơn để viết lại chức năng này vào một cái gì đó như thế này:

function download_url($url) 
{ 
    $saveto = 'temp.jpg'; // generate temp file 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
    $raw = curl_exec($ch); 
    if (curl_errno($ch)) { 
     curl_close($ch); 
     return false; 
     // you probably have a network problem here. 
     // you need to handle it, for example retry or skip and reqeue the image url 
    } 
    curl_close($ch); 
    if (file_exists($saveto)) { 
     unlink($saveto); 
    } 
    $fp = fopen($saveto, 'x'); 
    fwrite($fp, $raw); 
    fclose($fp); 
    return $saveto; 
} 
Các vấn đề liên quan