2017-08-04 76 views
0
$filePath = wp_get_attachment_url($id); 
$tempfilename = explode("/", $filePath); 
$temppdffile = end($tempfilename); 
$cuurentPath = plugin_dir_path(__FILE__).$temppdffile; 
$im = new Imagick(); 
$im->setResolution(300, 300); 
$im->readImage($cuurentPath.'[0]'); 
$im->setImageFormat('jpg'); 

我有上面的代碼..我有成功生成的圖像.. $ im但我必須保存圖像..媒體在wordpress作爲特色圖像?任何想法..嘗試每一件事情如何在wordpress上將imagick資源上傳到媒體?

+0

「嘗試了一切」 - 讓我們瞭解您已經嘗試過了。 –

回答

0
$filePath = wp_get_attachment_url($uploaded); 

$tempfilename = explode("/", $filePath); 
$temppdffile = end($tempfilename); 
$thumb = basename($temppdffile, ".pdf"); 
$cuurentPath = get_attached_file($uploaded); 
echo "current path ",$cuurentPath."</br>"; 
$thumbDirectory = plugin_dir_path(__FILE__); 
$im = new Imagick(); 
$im->setResolution(300, 300); 
$im->readImage($cuurentPath.'[0]'); 
$im->setImageFormat('jpg'); 
$r = "featured".rand(5, 200000).'_fea.jpg'; 
echo $r; 
$newFileName = $_SERVER['DOCUMENT_ROOT'].'/wp-content/plugins/pdftohtml/pdfimage/'; 
$myfile = file_put_contents($newFileName.$r, $im , FILE_APPEND | LOCK_EX); 
//$myfile = $im->writeImage($newFileName.$r); 
//$myfile = $im->writeImageFile($newFileName.$r); 
//$fh = fopen($newFileName.$r, (file_exists($newFileName.$r)) ? 'a' : 'w'); 
//fwrite($fh, $im); 
//fclose($fh); 
//var_dump($myfile); 
//done image add 
$n = _uploadImageToMediaLibrary($post_lesson,plugins_url()."/pdftohtml/pdfimage/".$r); 
$s = media_sideload_image(plugins_url()."/pdftohtml/pdfimage/".$thumb,$post_lesson); 
$attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_lesson, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC')); 
if(sizeof($attachments) > 0){ 
           // set image as the post thumbnail 
           set_post_thumbnail($post_lesson, $attachments[0]->ID); 
          } 
echo "Story Successfully uploaded"; 


} 
function _uploadImageToMediaLibrary($postID, $url, $alt = "blabla") { 

    /* require_once("../sites/$this->_wpFolder/wp-load.php"); 
    require_once("../sites/$this->_wpFolder/wp-admin/includes/image.php"); 
    require_once("../sites/$this->_wpFolder/wp-admin/includes/file.php"); 
    require_once("../sites/$this->_wpFolder/wp-admin/includes/media.php"); */ 
    //echo $url; 

    $tmp = download_url($url); 
    $desc = $alt; 
    $file_array = array(); 

    // Set variables for storage 
    // fix file filename for query strings 
    preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches); 
    $file_array['name'] = basename($matches[0]); 
    $file_array['tmp_name'] = $tmp; 

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

值得慶幸的是我解決我自己..你知道的問題是..我想念目錄分隔符「/」在$ _ SERVER

相關問題