我开发了一个脚本,允许注册的前端用户创建自己的帖子,每个帖子都有一张特色图片。
虽然我已经完成了整个过程,但现在的问题是图像大小调整。
处理图像的脚本如下:
if(isset($files['rcp_user_logo']) && $files['rcp_user_logo']['error'] == 0)
{
$uploadDir = wp_upload_dir();
$updoadBase = $uploadDir['basedir'];
$uploadURL = $uploadDir['baseurl'];
$uploadLogos = $updoadBase . DIRECTORY_SEPARATOR . 'LfirmLogos' . DIRECTORY_SEPARATOR;
$uploadLogosURL = $uploadURL . '/LfirmLogos/';
$dir_exists = false; // Flag for directory existance
if(($dir_exists = is_dir($uploadLogos)) == false) // If the upload dir is not exists
{
$dir_exists = mkdir($uploadLogos, 0755); // Try to create the dir with 0755 permissions
// If the dir will be successfully created
// the $dir_exists will be set to true
// otherwise will be set to false
}
else
{
$dir_exists = true; // If upload dir exists then set
// the $dir_exists to true
}
if($dir_exists == true)
{
// Set the tmpFile to the temporary uploaded file path
$tmpFile = $files['rcp_user_logo']['tmp_name'];
// Set the new file to upload directory and name the file after the custom post ID
$newFile = $uploadLogos . $new_id . '-' . $files['rcp_user_logo']['name'];
// Set the new file URL to updaload directory URL and name the file after the custom post ID
$newFileURL = $uploadLogosURL . $new_id . '-' . $files['rcp_user_logo']['name'];
if(move_uploaded_file($tmpFile, $newFile)) // If file has been
{
$wp_filetype = wp_check_filetype($files['rcp_user_logo']['name']);
$attachment = array(
'guid' => $newFileURL,
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_title($files['rcp_user_logo']['name']),
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $newFileURL, $new_id);
if($attachment_id != 0 )
{
if(!function_exists('wp_generate_attachment_metadata'))
{
include( ABSPATH . 'wp-admin/includes/image.php' );
}
$attachment_data = wp_generate_attachment_metadata($attachment_id, $newFileURL);
$result = wp_update_attachment_metadata($attachment_id, $attachment_data);
$result = update_post_meta($new_id, '_thumbnail_id', $attachment_id);
}
}
}
}
注意:为了让我的徽标与默认的 WordPress 图像分开,我在 /wp-content/uploads/ 下创建了自己的目录,我不知道这是否是阻止图像调整大小的原因。
注意: $new_id 是以编程方式在预览代码中创建的帖子的 ID。
注意:我也使用了插件“AJAX Thumbnail Rebuild”,但对图像仍然没有影响。它也无法调整图像大小。
注意:管理区域上传的图像已正确调整大小。
关于如何解决这个问题有什么想法吗?
亲切的问候