WordPress |上传文件并调整其大小

时间:2013-04-16 10:49:09

标签: wordpress wordpress-theming

我开发了一个脚本,允许注册的前端用户创建自己的帖子,每个帖子都有一个特色图片。

虽然我已完成整个过程,但现在的问题是图像调整大小。

操纵图像的脚本如下:

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 /下创建了我自己的目录,我不知道这是否是一个reasong防止图像大小调整。

注意: $ new_id是预览代码programmaticaly中创建的帖子的ID。

注意:我也使用了插件“AJAX Thumbnail Rebuild”,但对图像仍然没有影响。它也无法调整图像大小。

注意:管理区域上传的图片已正确调整大小。

对于如何解决这个问题有什么想法吗?

亲切的问候

1 个答案:

答案 0 :(得分:1)

我建议您使用php的GD图像,因为wordpress也使用它: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重建它是一个从wordpress媒体管理器重建缩略图的插件。