禁止WordPress Justnews主题生成480X320尺寸的缩略图

今天有个站长好朋友来求助,他的网站使用Justnews的主题,他对主题各方面比较满意,就是很不喜欢它会一直主动裁切生成缩略图。

他说他折腾了很久,用了很常见的WP禁止缩略图的方法也无用。即便是删除了还会自动再生成,这让他很恼火,这才找到我帮忙。

 

我大概看了一下这个主题的结构,目前的解决方法是,先主题目录下这个文件:functions.php,接着找到这段代码,然后删除:

add_filter('wpcom_image_sizes', 'justnews_image_sizes', 20);
function justnews_image_sizes($image_sizes){
    global $options;
    if(!isset($options['thumb_default_width'])){
        $image_sizes['post-thumbnail'] = array(
            'width' => 480,
            'height' => 300
        );
    }
    return $image_sizes;
}

然后在这个位置加一段禁止代码:

// 完全禁止 WordPress 生成任何尺寸的缩略图
function disable_all_image_sizes( $sizes ) {
    return array(); // 返回空数组,阻止所有中间尺寸生成
}
add_filter( 'intermediate_image_sizes_advanced', 'disable_all_image_sizes', 999 );

// 移除所有已注册的图片尺寸
function remove_all_image_sizes() {
    // 移除默认尺寸
    remove_image_size( 'thumbnail' );
    remove_image_size( 'medium' );
    remove_image_size( 'medium_large' );
    remove_image_size( 'large' );
    
    // 移除主题或插件添加的自定义尺寸
    foreach ( get_intermediate_image_sizes() as $size ) {
        remove_image_size( $size );
    }
}
add_action( 'init', 'remove_all_image_sizes', 999 );

然后再找到主题的路径的这个文件:\themer\includes\aq-resizer.php,然后把以下改好的代码替换掉:

<?php
/**
 * Title         : Aqua Resizer
 * Description   : Resizes WordPress images on the fly
 * Version       : 1.2.1
 * Author        : Syamil MJ
 * Author URI    : http://aquagraphite.com
 * License       : WTFPL - http://sam.zoy.org/wtfpl/
 * Documentation : https://github.com/sy4mil/Aqua-Resizer/
 *
 * @param string  $url      - (required) must be uploaded using wp media uploader
 * @param int     $width    - (required)
 * @param int     $height   - (optional)
 * @param bool    $crop     - (optional) default to soft crop
 * @param bool    $single   - (optional) returns an array if false
 * @param bool    $upscale  - (optional) resizes smaller images
 * @uses  wp_upload_dir()
 * @uses  image_resize_dimensions()
 * @uses  wp_get_image_editor()
 *
 * @return str|array
 */
defined( 'ABSPATH' ) || exit;

if(!class_exists('WPCOM_Resize')) {
    class WPCOM_Exception extends Exception {}

    class WPCOM_Resize
    {
        /**
         * The singleton instance
         */
        static private $instance = null;

        /**
         * Should an WPCOM_Exception be thrown on error?
         * If false (default), then the error will just be logged.
         */
        public $throwOnError = false;

        /**
         * No initialization allowed
         */
        private function __construct() {}

        /**
         * No cloning allowed
         */
        private function __clone() {}

        /**
         * For your custom default usage you may want to initialize an WPCOM_Resize object by yourself and then have own defaults
         */
        static public function getInstance() {
            if(self::$instance == null) {
                self::$instance = new self;
            }

            return self::$instance;
        }

        /**
         * Run, forest.
         */
        public function process( $url, $width = null, $height = null, $crop = null, $img_id = 0, $size = '', $single = true, $upscale = false ) {
            try {
                // Validate inputs.
                if (!$url)
                    throw new WPCOM_Exception('$url parameter is required');
                if (!$width)
                    throw new WPCOM_Exception('$width parameter is required');

                // Caipt'n, ready to hook.
                if ( true === $upscale ) add_filter( 'image_resize_dimensions', array($this, 'aq_upscale'), 10, 6 );

                // Define upload path & dir.
                $upload_info = wp_upload_dir();
                $upload_dir = $upload_info['basedir'];
                $upload_url = $upload_info['baseurl'];

                $http_prefix = "http://";
                $https_prefix = "https://";
                $relative_prefix = "//"; // The protocol-relative URL

                // 匹配本地绝对路径图片,并补充成完整地址
                if(preg_match('/^\/[^\/].*/i', $url) && preg_match('/(http:|https:)\/\/([^\/]+)/i', get_bloginfo('url'), $mc)){
                    if($mc && $mc[0]){
                        $url = $mc[0] . $url;
                    }
                }

                /* if the $url scheme differs from $upload_url scheme, make them match
                   if the schemes differe, images don't show up. */
                if(!strncmp($url,$https_prefix,strlen($https_prefix))){ //if url begins with https:// make $upload_url begin with https:// as well
                    $upload_url = str_replace($http_prefix,$https_prefix,$upload_url);
                }
                elseif(!strncmp($url,$http_prefix,strlen($http_prefix))){ //if url begins with http:// make $upload_url begin with http:// as well
                    $upload_url = str_replace($https_prefix,$http_prefix,$upload_url);
                }
                elseif(!strncmp($url,$relative_prefix,strlen($relative_prefix))){ //if url begins with // make $upload_url begin with // as well
                    $upload_url = str_replace(array( 0 => "$http_prefix", 1 => "$https_prefix"),$relative_prefix,$upload_url);
                }


                // Check if $img_url is local.
                if ( false === strpos( $url, $upload_url ) )
                    return false;

                // oss路径的图片使用oss图片服务返回
                if ( preg_match('/^oss:\/\//i', $upload_dir) || class_exists('WPOSS') ) {
                    $size = 'image/resize,m_fill,w_'.$width;
                    if($height) $size .= ',h_'.$height;
                    $size .= ',limit_0';
                    if ( $single ) {
                        $image = add_query_arg(array('x-oss-process' => $size), $url);
                    } else {
                        $image = array (
                            0 => add_query_arg(array('x-oss-process' => $size), $url),
                            1 => $width,
                            2 => $height
                        );
                    }
                    return $image;
                }else if( (function_exists('wpcos_unique_filename') || defined('TENCENT_WORDPRESS_COS_VERSION')) && !preg_match('/imageMogr2\//i', $upload_dir)) {
                    $size = 'imageMogr2/crop/'.$width.'x';
                    if($height) $size .= $height;
                    $size .= '/gravity/center';
                    if ( $single ) {
                        $image = add_query_arg(array($size => ''), $url);
                    } else {
                        $image = array (
                            0 => add_query_arg(array($size => ''), $url),
                            1 => $width,
                            2 => $height
                        );
                    }
                    return $image;
                }else if( class_exists('WPQiNiu') && !preg_match('/imageMogr2\//i', $upload_dir)) {
                    if($height) {
                        $size = 'imageMogr2/thumbnail/!'.$width.'x'.$height.'r|imageMogr2/gravity/center/crop/'.$width.'x'.$height;
                    }else{
                        $size = 'imageMogr2/thumbnail/'.$width.'x';
                    }
                    if ( $single ) {
                        $image = add_query_arg(array($size => ''), $url);
                    } else {
                        $image = array (
                            0 => add_query_arg(array($size => ''), $url),
                            1 => $width,
                            2 => $height
                        );
                    }
                    return $image;
                }else if(function_exists('wpupyun_unique_filename')){
                    if($height) {
                        $size = '!/both/'.$width.'x'.$height;
                    }else{
                        $size = '!/fw/'.$width;
                    }
                    if ( $single ) {
                        $image = $url . $size;
                    } else {
                        $image = array (
                            0 => $url . $size,
                            1 => $width,
                            2 => $height
                        );
                    }
                    return $image;
                }

                // Return the original image URL
                if ( $single ) {
                    $image = $url;
                } else {
                    list( $orig_w, $orig_h ) = getimagesize( str_replace( $upload_url, $upload_dir, $url ) );
                    $image = array (
                        0 => $url,
                        1 => $orig_w,
                        2 => $orig_h
                    );
                }

                // Okay, leave the ship.
                if ( true === $upscale ) remove_filter( 'image_resize_dimensions', array( $this, 'aq_upscale' ) );

                return $image;
            }
            catch (WPCOM_Exception $ex) {
                //error_log('WPCOM_Resize.process() error: ' . $ex->getMessage());

                if ($this->throwOnError) {
                    // Bubble up exception.
                    throw $ex;
                }
                else {
                    // Return false, so that this patch is backwards-compatible.
                    return false;
                }
            }
        }

        /**
         * Callback to overwrite WP computing of thumbnail measures
         */
        function aq_upscale( $default, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
            if ( ! $crop ) return null; // Let the wordpress default function handle this.

            // Here is the point we allow to use larger image size than the original one.
            $aspect_ratio = $orig_w / $orig_h;
            $new_w = $dest_w;
            $new_h = $dest_h;

            if ( ! $new_w ) {
                $new_w = intval( $new_h * $aspect_ratio );
            }

            if ( ! $new_h ) {
                $new_h = intval( $new_w / $aspect_ratio );
            }

            $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );

            $crop_w = round( $new_w / $size_ratio );
            $crop_h = round( $new_h / $size_ratio );

            $crop = apply_filters('wpcom_image_crop_position', $crop);
            if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
                $crop = array( 'center', 'center' );
            }

            list( $x, $y ) = $crop;

            if ( 'left' === $x ) {
                $s_x = 0;
            } elseif ( 'right' === $x ) {
                $s_x = $orig_w - $crop_w;
            } else {
                $s_x = floor( ( $orig_w - $crop_w ) / 2 );
            }

            if ( 'top' === $y ) {
                $s_y = 0;
            } elseif ( 'bottom' === $y ) {
                $s_y = $orig_h - $crop_h;
            } else {
                $s_y = floor( ( $orig_h - $crop_h ) / 2 );
            }

            return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
        }
    }
}


if(!function_exists('wpcom_resize')) {

    /**
     * This is just a tiny wrapper function for the class above so that there is no
     * need to change any code in your own WP themes. Usage is still the same :)
     */
    function wpcom_resize( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false ) {
        /* WPML Fix */
        if ( defined( 'ICL_SITEPRESS_VERSION' ) ){
            global $sitepress;
            $url = $sitepress->convert_url( $url, $sitepress->get_default_language() );
        }
        /* WPML Fix */

        $aq_resize = WPCOM_Resize::getInstance();
        return $aq_resize->process( $url, $width, $height, $crop, $single, $upscale );
    }
}

如此,便能基本上解决这个主题会为每张图片自动裁切480X320像素的缩略图了。

当然了,还有涉及到其他文件要修改,但是没必要,因为改多错多,能用就可以了。

 

题外话,其实有很多站长都很不喜欢图片被各种裁切。

在WP主题使用的过程中,在主题加入一个大图自适应尺寸的参数,这才是更受站长们喜欢的一种图片展示方式。

THE END
請多多支持
点赞0
相关推荐
  • 暂无相关文章
评论 抢沙发

    暂无评论内容