2013-01-08 27 views
0

所以我使用其他人制作的脚本。 Wordpress网站。遇到麻烦在我的Wordpress站点上更改头像位置

我想更改默认上传位置:

mysite.com/wp-content/themes/theme_name/images/avatar/avatarnamehere20.jpg 

要:

mysite.com/images/avatar/avatarnamehere20.jpg 

不管怎么说,这是第一个场景的工作代码:

代码处理上传

if(isset($_FILES['image'])) 
     $avatar = $_FILES['image']; 
$save_to = "/images/avatar"; 

    $logo_results = tgt_resize_image($avatar['tmp_name'] ,$save_to,150,150,$avatar['type'], get_the_author_meta('display_name', $user_ID)); 
     if ($logo_results === false) 
     { 
      global $errors; 
      $errors .= __("Error: We can't upload your avatar", 'ad')."<br>"; 
     } 
     else{ 
      $avatar = get_user_meta($user_ID,'tgt_image',true); 
      if(file_exists(TEMPLATEPATH . $avatar) && $avatar != '/images/avatar.png') 
    unlink(TEMPLATEPATH . $avatar); 
      $avatar = $logo_results; 
      update_user_meta($user_ID,'tgt_image',$avatar); 
     } 

功能tgt_resize_image

function tgt_resize_image($ipath, $tdir, $twidth, $theight, $image_type, $name_image){ 
try{ 
    $simg = ''; 
    //check image type 
    $type_arr = array('image/jpg', 'image/jpeg', 'image/pjpeg'); 
    if (in_array($image_type, $type_arr)) 
    { 
     $simg = imagecreatefromjpeg($ipath); 
    } 
    elseif($image_type == 'image/png'){ 
     $simg = imagecreatefrompng($ipath); 
    } 
    elseif($image_type == 'image/gif'){ 
     $simg = imagecreatefromgif($ipath); 
    } 
    else return false; 

    $currwidth = imagesx($simg); // Current Image Width 
    $currheight = imagesy($simg); 
    /* if ($twidth == 0) $twidth = $currwidth; 
    if ($theight == 0) $theight = $theight;*/ 

    $dimg = imageCreateTrueColor($twidth, $theight); // Make New Image For Thumbnail 

    $name_image .= rand(1, 100); 
    $name_image = sanitize_file_name($name_image); 


/*if($image_type == IMAGETYPE_GIF) { 
imagealphablending($dimg, false); 
imagesavealpha($dimg,true); 
$transparent = imagecolorallocatealpha($dimg, 255, 255, 255, 127); 
imagefilledrectangle($dimg, 0, 0, $twidth, $theight, $transparent); 
imagecolortransparent ($dimg, $transparent); 
} elseif($image_type == IMAGETYPE_PNG) { 
// These parameters are required for handling PNG files. 
imagealphablending($dimg, false); 
imagesavealpha($dimg,true); 
$transparent = imagecolorallocatealpha($dimg, 255, 255, 255, 127); 
imagefilledrectangle($dimg, 0, 0, $twidth, $theight, $transparent); 
}*/ 

    imagecopyresampled ($dimg, $simg, 0, 0, 0, 0, $twidth, $theight, $currwidth, $currheight); 
    //imagecopyresized($dimg, $simg, 0, 0, 0, 0, $twidth, $theight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) 
    // imagejpeg($dimg, TEMPLATEPATH . "$tdir/" . $name_image . '.jpg'); // Saving The Image 
    $link = $tdir . '/' . $name_image; 
    if (in_array($image_type, $type_arr)) 
    { 
     imagejpeg($dimg, TEMPLATEPATH . "$tdir/" . $name_image . '.jpg'); 
     $link .= '.jpg'; 
    } 
    elseif($image_type == 'image/png'){ 
     imagepng($dimg, TEMPLATEPATH . "$tdir/" . $name_image . '.png'); 
     $link .= '.png'; 
    } 
    elseif($image_type == 'image/gif'){ 
     imagegif($dimg, TEMPLATEPATH . "$tdir/" . $name_image . '.gif'); 
     $link .= '.gif'; 
    } 
} 
catch (exception $e){ 
    imagedestroy($simg); // Destroying The Temporary Image 
    imagedestroy($dimg); // Destroying The Other Temporary Image 
    return false; 
} 

imagedestroy($simg); // Destroying The Temporary Image 
imagedestroy($dimg); // Destroying The Other Temporary Image 
return $link; 

// wp_attachment_is_image() 
} 

我在改变代码的一些尝试。我设法让mysql feild正确更新,但文件根本不会上传。所以页面试图显示图像,但它不存在。

这是我得到:

if(isset($_FILES['image'])) 
     $avatar = $_FILES['image']; 
    //$save_to = "/images/avatar"; 
    $save_to = site_url('/') . 'images/avatar'; 

    $logo_results = tgt_resize_image($avatar['tmp_name'] ,$save_to,150,150,$avatar['type'], get_the_author_meta('display_name', $user_ID)); 
     if ($logo_results === false) 
     { 
      global $errors; 
      $errors .= __("Error: We can't upload your avatar", 'ad')."<br>"; 
     } 
     else{ 
      $avatar = get_user_meta($user_ID,'tgt_image',true); 
      if(file_exists(site_url('/') . $avatar) && $avatar != '/images/avatar.png') 
    unlink(site_url('/') . $avatar); 
      $avatar = $logo_results; 
      update_user_meta($user_ID,'tgt_image',$avatar); 
     } 

//wp_redirect(get_bloginfo('url').'/?author='.$current_user->ID); 
wp_redirect(get_author_posts_url($user_ID)); 
    exit; 

在tgt_resize_image功能,我改变了TEMPLATEPATH到SITE_URL( '/'),但没有奏效。

任何帮助表示赞赏!我会奖励那些帮助我的人,因为这很令人沮丧!

让我知道,如果你需要更多的信息或从任何地方的代码。

再次感谢!

回答

0

好吧,我找到了答案!

在功能:tgt_resize_image

imagejpeg,imagepng和imagegif被保存的图像文件。

TEMPLATEPATH指向WP模板位置。

使用site_url()将不起作用,因为它现在将指向web位置(mysite.com)。

我不得不使用$ _SERVER ['DOCUMENT_ROOT']替换TEMPLATEPATH,它结束工作正常。

祝你好运!