コード例 #1
0
ファイル: media.php プロジェクト: skylarkcob/hocwp-projects
function hocwp_crop_image($args = array())
{
    $attachment_id = hocwp_get_value_by_key($args, 'attachment_id');
    $url = hocwp_get_value_by_key($args, 'url');
    $base_url = '';
    if (!hocwp_id_number_valid($attachment_id) && !empty($url)) {
        $attachment_id = hocwp_get_media_id($url);
    }
    if (!hocwp_id_number_valid($attachment_id)) {
        if (empty($url)) {
            return new WP_Error('crop_image_size', __('Attachment ID is not valid.', 'hocwp-theme'));
        } else {
            $cropped = $url;
        }
    } else {
        $file_path = hocwp_get_media_file_path($attachment_id);
        $width = hocwp_get_value_by_key($args, 'width');
        $height = hocwp_get_value_by_key($args, 'height');
        $size = hocwp_get_image_sizes($attachment_id);
        $size = hocwp_sanitize_size($size);
        if (empty($width) && empty($height)) {
            $cropped = $file_path;
        } else {
            if (empty($width)) {
                $width = $size[0];
            }
            if (empty($height)) {
                $height = $size[1];
            }
            $x = apply_filters('hocwp_crop_image_x', 0, $args);
            $y = apply_filters('hocwp_crop_image_y', 0, $args);
            $x = hocwp_get_value_by_key($args, 'x', $x);
            $y = hocwp_get_value_by_key($args, 'y', $y);
            $dest_file = hocwp_get_value_by_key($args, 'dest_file', '');
            $path_info = pathinfo($file_path);
            if (empty($dest_file)) {
                $upload_dir = hocwp_get_upload_folder_details();
                $base_path = apply_filters('hocwp_custom_thumbnail_base_path', untrailingslashit($upload_dir['path']) . '/hocwp/thumbs/', $args);
                if (!file_exists($base_path)) {
                    wp_mkdir_p($base_path);
                }
                $base_url = apply_filters('hocwp_custom_thumbnail_base_url', untrailingslashit($upload_dir['url']) . '/hocwp/thumbs/', $args);
                $filename = $path_info['filename'];
                $dest_file = $base_path . str_replace($filename, $filename . '-' . $width . '-' . $height, basename($file_path));
            }
            $crop_args = array('source' => get_attached_file($attachment_id), 'dest' => $dest_file, 'width' => $width, 'height' => $height, 'x' => $x, 'y' => $y);
            $crop_args = wp_parse_args($args, $crop_args);
            if (file_exists($dest_file)) {
                $override = hocwp_get_value_by_key($args, 'override', false);
                if ($override) {
                    unlink($dest_file);
                    $cropped = hocwp_crop_image_helper($crop_args);
                } else {
                    $cropped = $dest_file;
                }
            } else {
                $cropped = hocwp_crop_image_helper($crop_args);
            }
        }
    }
    if (file_exists($cropped)) {
        $output = hocwp_get_value_by_key($args, 'output', 'url');
        if ('url' == $output) {
            $cropped = hocwp_media_path_to_url($attachment_id, $cropped, $base_url);
        }
    } else {
        $cropped = $url;
    }
    return apply_filters('hocwp_crop_image', $cropped, $args);
}
コード例 #2
0
function hocwp_execute_upload($args = array())
{
    $files = isset($args['files']) ? $args['files'] : array();
    unset($args['files']);
    $upload_path = isset($args['upload_path']) ? $args['upload_path'] : '';
    unset($args['upload_path']);
    $upload_url = isset($args['upload_url']) ? $args['upload_url'] : '';
    unset($args['upload_url']);
    if (empty($upload_path)) {
        $upload_dir = hocwp_get_upload_folder_details();
        $target_dir = untrailingslashit($upload_dir['path']) . '/hocwp';
        $upload_url = untrailingslashit($upload_dir['url']) . '/hocwp';
        if (!file_exists($target_dir)) {
            wp_mkdir_p($target_dir);
        }
        $upload_path = $target_dir;
    }
    $file_names = isset($files['name']) ? $files['name'] : array();
    $file_count = count($file_names);
    $list_files = array();
    for ($i = 0; $i < $file_count; $i++) {
        $name = isset($files['name'][$i]) ? $files['name'][$i] : '';
        $type = isset($files['type'][$i]) ? $files['type'][$i] : '';
        $tmp_name = isset($files['tmp_name'][$i]) ? $files['tmp_name'][$i] : '';
        $error = isset($files['error'][$i]) ? $files['error'][$i] : '';
        $size = isset($files['size'][$i]) ? $files['size'][$i] : '';
        $file_item = array('name' => $name, 'type' => $type, 'tmp_name' => $tmp_name, 'error' => $error, 'size' => $size);
        $list_files[] = $file_item;
    }
    $list_results = array();
    foreach ($list_files as $key => $file) {
        $file['path'] = $upload_path;
        $file = wp_parse_args($args, $file);
        $result = hocwp_upload($file);
        if ($result['success']) {
            $file_name = $file['name'];
            $file_path = untrailingslashit($upload_path) . '/' . hocwp_sanitize_file_name($file_name);
            $file_url = untrailingslashit($upload_url) . '/' . hocwp_sanitize_file_name(basename($result['name']));
            $attachment = array('guid' => $file_url);
            hocwp_insert_attachment($attachment, $file_path);
            $result['url'] = $file_url;
        }
        $list_results[] = $result;
    }
    return $list_results;
}