登録済みのフロント エンド ユーザーが独自の投稿を作成できるようにするスクリプトを開発しました。投稿ごとに注目の画像が表示されます。
全体的なプロセスは完了しましたが、問題は画像のサイズ変更です。
画像を操作するスクリプトは次のとおりです。
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」も使用しましたが、画像には影響しません。画像のサイズも変更できません。
注 :管理エリアからアップロードされた画像は、適切にサイズ変更されます。
その問題を解決する方法について何か考えはありますか?
敬具