2

登録済みのフロント エンド ユーザーが独自の投稿を作成できるようにするスクリプトを開発しました。投稿ごとに注目の画像が表示されます。

全体的なプロセスは完了しましたが、問題は画像のサイズ変更です。

画像を操作するスクリプトは次のとおりです。

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」も使用しましたが、画像には影響しません。画像のサイズも変更できません。

注 :管理エリアからアップロードされた画像は、適切にサイズ変更されます。

その問題を解決する方法について何か考えはありますか?

敬具

4

1 に答える 1

1

ワードプレスでも使用されているため、php の GD 画像を使用することをお勧めします: http://php.net/manual/en/function.imagecopyresized.php

以下は、画像のサイズをパーセンテージ (例では 0.5) に変更する、そのページから取得したコード スニペットです。

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

注: AJAX Thumbnail Rebuild は、wordpress メディア マネージャーからサムネイルを再構築するプラグインです。

于 2013-04-16T10:59:25.113 に答える