Having Trouble Changing Avatar Location on my Wordpress site

338 views Asked by At

So I am using a script made by someone else. Wordpress site.

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

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. 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.

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!

1

There are 1 answers

0
Mo Beigi On

Okay I found the answer!

In the function: tgt_resize_image

imagejpeg, imagepng and imagegif were saving the image file.

TEMPLATEPATH was pointing to the WP template location.

Using site_url() would not work as it would now be pointing to the web location (mysite.com).

I had to replace TEMPLATEPATH with $_SERVER['DOCUMENT_ROOT'] and it ended up working fine.

Good luck!