简体   繁体   English

无法在我的Wordpress网站上更改头像位置

[英]Having Trouble Changing Avatar Location on my Wordpress site

So I am using a script made by someone else. 所以我正在使用别人制作的脚本。 Wordpress site. WordPress网站。

I want to change the default upload location: 我想更改默认的上传位置:

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

To: 至:

mysite.com/images/avatar/avatarnamehere20.jpg

Anyways, this is the working code for the first scenario: 无论如何,这是第一种情况的工作代码:

Code that processes upload 处理上传的代码

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);
        }

Function tgt_resize_image 函数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()
}

I had a few attempts at changing the code. 我曾尝试过更改代码。 I managed to get the mysql feild to update correctly but the file wouldn't upload at all. 我设法让mysql地域正确更新,但文件根本无法上传。 So the page tries to display the image but it does not exist. 因此,该页面尝试显示图像,但该图像不存在。

This is what I got: 这就是我得到的:

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;

In the tgt_resize_image function, I changed the TEMPLATEPATH to the site_url('/') but that did not work. 在tgt_resize_image函数中,我将TEMPLATEPATH更改为site_url('/'),但这没有用。

Any help is appreciated! 任何帮助表示赞赏! I will reward the person who does help me cause this is frustrating! 我会奖励确实帮助我的人,因为这令人沮丧!

Let me know if you need more info or code from anywhere. 让我知道您是否需要任何地方的更多信息或代码。

Thanks again! 再次感谢!

Okay I found the answer! 好吧,我找到了答案!

In the function: tgt_resize_image 在函数中:tgt_resize_image

imagejpeg, imagepng and imagegif were saving the image file. imagejpeg,imagepng和imagegif正在保存图像文件。

TEMPLATEPATH was pointing to the WP template location. TEMPLATEPATH指向WP模板位置。

Using site_url() would not work as it would now be pointing to the web location (mysite.com). 使用site_url()无效,因为它现在指向的是网站(mysite.com)。

I had to replace TEMPLATEPATH with $_SERVER['DOCUMENT_ROOT'] and it ended up working fine. 我不得不用$ _SERVER ['DOCUMENT_ROOT']替换TEMPLATEPATH,它最终运行正常。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM