在我的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字段正确更新,但文件根本不会上传。因此,页面试图显示图像,但它不存在。

这是我得到的:

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('/'),但这不起作用。

任何帮助都是感激的!我会奖励那些帮助我的人,因为这是令人沮丧的!

如果你需要更多的信息或代码,请告诉我。

再次感谢!

好了,我找到答案了!

函数中:tgt_resize_image

imagejpeg, imagepng和imagegif正在保存图像文件。

TEMPLATEPATH指向WP模板位置。

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

我必须将TEMPLATEPATH替换为$_SERVER['DOCUMENT_ROOT'],它最终工作正常。

祝你好运!

最新更新