所以我使用的是别人制作的脚本。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('/') 但这不起作用。
任何帮助表示赞赏!我会奖励帮助我的人,因为这很令人沮丧!
如果您需要来自任何地方的更多信息或代码,请告诉我。
再次感谢!