致命错误未捕获错误:调用未定义的方法 WP_Error

如何解决致命错误未捕获错误:调用未定义的方法 WP_Error

错误信息

<b>Fatal error</b>:  Uncaught Error: Call to undefined method WP_Error::resize() in C:\Users\User\Documents\xampp\htdocs\WordPress1\wp-content\themes\elina\framework\php\PeTheme\PeThemeImage.php:144
Stack trace:
#0 C:\Users\User\Documents\xampp\htdocs\WordPress1\wp-content\themes\elina\framework\php\PeTheme\PeThemeImage.php(282): 
PeThemeImage-&gt;iresize('C:\\Users\\User\\D...',320,240,true)
#1 C:\Users\User\Documents\xampp\htdocs\WordPress1\wp-content\themes\elina\framework\php\PeTheme\PeThemeImage.php(364):
 PeThemeImage-&gt;aq_resize('http://localhos...',true,false)
#2 C:\Users\User\Documents\xampp\htdocs\WordPress1\wp-content\themes\elina\framework\php\PeTheme\PeThemeImage.php(369): PeThemeImage-&gt;resize('http://localhos...',true)

来自代码

class PeThemeImage { 
protected $basedir;
protected $baseurl;
protected $blanks = array();
protected $retina_enabled;
protected $lazyload_enabled;
protected $optkey;
public $generated;
protected $md5;
protected $image_sizes;

public function __construct() {
    $wp_upload = wp_upload_dir();
    $this->basedir = $wp_upload["basedir"];
    $this->baseurl = $wp_upload["baseurl"];
    $this->retina_enabled = peTheme()->options->get("retina") === "yes";
    $this->lazyload_enabled = peTheme()->options->get("lazyImages") === "yes";
    $this->optkey = "pe_theme_".PE_THEME_NAME."_thumbnails";
    //delete_option($this->optkey);
    $this->generated = get_option($this->optkey,array("index" => array()));
    $this->md5 = md5(serialize($this->generated));

    //peTheme()->thumbnail->clean();
}

public function &blank($w,$h) {
    $key = "{$w}x{$h}";
    if (isset($this->blanks[$key])) {
        return $this->blanks[$key];
    }

    $external = "/img/blank/$key.gif";

    if (file_exists(PE_THEME_PATH.$external)) {
        // a blank img exists with the requested size
        $this->blanks[$key] = PE_THEME_URL.$external;
    } else {
        // create blank img
        $image = imagecreate($w,$h);
        imagesavealpha($image,true);
        imagecolortransparent($image,imagecolorallocatealpha($image,0));
        ob_start();
        imagegif($image);
        $this->blanks[$key] = "data:image/gif;base64,".base64_encode(ob_get_clean());
        imagedestroy($image);
    }

    return $this->blanks[$key];
}

public function placeholder($w,$h) {
    return sprintf('<img src="%s" width="%s" height="%s" alt="" />',$this->blank($w,$h),$w,$h);
}


public function get_size($img) {
    return stripos($img,"http") === 0 ? false : getimagesize($img);
}

public function is_url($url) {
    return stripos($url,'http://') === 0 || strpos($url,'http://') === 0;
}


public function get_retina($url) {

    $ret = $this->retina_enabled;
    $lazy = $ret || $this->lazyload_enabled;
    $common = sprintf('src="%s" alt=""',$url);

    $img = str_replace(PE_THEME_URL,PE_THEME_PATH,$url);
    if ($this->is_url($img)) {
        $burl = $this->baseurl;
        $bdir = $this->basedir;
        $img = str_replace($burl,$bdir,$img);
        if ($this->is_url($img)) {
            // damn ... let's try adding/stripping www
            if (stripos("://www.",$url) > -1) {
                // www is there,try removing it
                $burl = str_replace("://www.","://",$burl);
            } else {
                // no www,try adding it
                $burl = str_replace("://","://www.",$burl);
            }
            $img = str_replace($burl,$img);
        }
    }

    if ($ret || $lazy) {
        
        if ($ret) {
            $ret = preg_replace("/\.(\w+)$/",'@2x.$1',$img);
            $ret = is_readable($ret) ? preg_replace("/\.(\w+)$/",$url) : false;
        }

        if ($ret || $lazy) {
            // try to get image size
            $size = $this->get_size($img);
            if ($size) {
                list($w,$h) = $size;
                $common = sprintf('src="%s" width="%s" height="%s" alt=""',$h);
            } else {
                $common = sprintf('src="%s" alt=""',$this->blank(1,1));
            }

            $lazy = sprintf(' data-original="%s"',$url);
            $ret = $ret ? sprintf(' data-original-hires="%s"',$ret) : "";
        }
    } else {
        $size = $this->get_size($img);

        if ($size) {
            list($w,$h) = $size;
            $common = sprintf('src="%s" width="%s" height="%s" alt=""',$url,$h);
        } 
    }

    return sprintf("<img %s%s%s>",$common,$lazy,$ret);
}

public function retina($img) {
    echo $this->get_retina($img);
}


public function iresize($file,$width,$height,$crop) { // compatibilty wrapper for 3.4,>= 3.5 use wp_get_image_editor
    if (function_exists("wp_get_image_editor")) {

        // check if custom cropping defined
        $size = "{$width}x{$height}";
        $thumb = str_replace($this->basedir,"",preg_replace("/(\.\w+)$/","-$size\\1",$file));
        if (!empty($this->generated["crop"][$thumb])) {
            $crop = $this->generated["crop"][$thumb];
            $url = str_replace($this->basedir,$this->baseurl,$file);
            $this->crop($url,$crop,$height);
            return $this->basedir.$thumb;
        }

        // WP 3.5
        $editor = wp_get_image_editor($file);
        $editor->resize($width,$crop);
        $dest_file = $editor->generate_filename();
        $editor->save($dest_file);
        return $dest_file;
    } else {
        $f = "image_resize"; // compatibilty with WP 3.4 and below
        return $f($file,$crop);
    }
}


public static function makeBWimage($file,$dest) {
    //$image = wp_load_image($file);
    $image = imagecreatefromstring( file_get_contents( $file ) );
    $res = imagefilter($image,IMG_FILTER_GRAYSCALE);
    $res = $res && imagejpeg($image,$dest);
    imagedestroy($image);
    return $res;
}

public function blackwhite_filter($meta) {
    if (!isset($meta) || !is_array($meta) || count($meta) == 0) {
        return $meta;
    }
    $base = dirname($this->basedir."/".$meta["file"]);
    $thumbs = array_keys(PeGlobal::$config["image-sizes"]);

    if (isset($meta["sizes"])) {
        $sizes =& $meta["sizes"];
    }

    foreach ($thumbs as $thumb) {
        if (substr($thumb,-3,3) == "-bw") {
            $normal = substr($thumb,strlen($thumb)-3);
            if (isset($sizes) && isset($sizes[$normal])) {
                $bwthumb = array_merge($sizes[$normal]);
            } else {
                $bwthumb = array("file" => wp_basename($meta["file"]),"width" => $meta["width"],"height" => $meta["height"]);
            }
            $file = "$base/{$bwthumb['file']}";
            $dest = preg_replace('/\.\w+$/',"-bw.jpg",$file);
            if (!isset($seen[$file])) {
                $seen[$file] = true;
                $this->makeBWimage($file,$dest);
            }
            $bwthumb["file"] = wp_basename($dest);
            $meta["sizes"][$thumb] = $bwthumb;
        }
    }

    return $meta;
}

/**
 * Title        : Aqua Resizer
 * Description  : Resizes WordPress images on the fly
 * Version  : 1.1.3
 * 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
 * @uses wp_upload_dir()
 *
 * @return str|array
 */
public function aq_resize( $url,$height = null,$crop = null,$single = true ) {
    
    //validate inputs
    if(!$url OR !$width ) return false;
    
    //define upload path & dir
    $upload_dir = $this->basedir;
    $upload_url = $this->baseurl;
    
    //check if $img_url is local
    //if(strpos( $url,home_url() ) === false) return false;
    if(strpos( $url,$upload_url ) === false) return false;
    
    //define path of image
    $rel_path = str_replace( $upload_url,'',$url);
    $img_path = $upload_dir . $rel_path;
    
    //check if img path exists,and is an image indeed
    if( !file_exists($img_path) OR !getimagesize($img_path) ) return false;
    
    //get image info
    $info = pathinfo($img_path);
    $ext = $info['extension'];
    list($orig_w,$orig_h) = getimagesize($img_path);
    
    //get image size after cropping
    $dims = image_resize_dimensions($orig_w,$orig_h,$crop);
    $dst_w = $dims[4];
    $dst_h = $dims[5];
    
    //use this to check if cropped image already exists,so we can return that instead
    $suffix = "{$dst_w}x{$dst_h}";

    $dst_rel_path = str_replace( '.'.$ext,$rel_path);
    $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";
    
    //if orig size is smaller
    if($width >= $orig_w) {
        
        if(!$dst_h) :
            //can't resize,so return original url
            $img_url = $url;
        $dst_w = $orig_w;
        $dst_h = $orig_h;
        
        else :
            //else check if cache exists
            if(file_exists($destfilename) && getimagesize($destfilename)) {
                $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
            } 
        //else resize and return the new resized image url
            else {
                $resized_img_path = $this->iresize( $img_path,$crop );
                $resized_rel_path = str_replace( $upload_dir,$resized_img_path);
                $img_url = $upload_url . $resized_rel_path;
                
            }
        
        endif;
        
    }
    //else check if cache exists
    elseif(file_exists($destfilename) && getimagesize($destfilename)) {
        $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
    } 
    //else,we resize the image and return the new resized image url
    else {
        $resized_img_path = $this->iresize( $img_path,$crop );
        $resized_rel_path = str_replace( $upload_dir,$resized_img_path);
        $img_url = $upload_url . $resized_rel_path;
    }
    

    //return the output
    if($single) {
        //str return
        $image = $img_url;
    } else {
        //array return
        $image = array (
                        0 => $img_url,1 => $dst_w,2 => $dst_h
                        );
    }
    
    $this->bwMode = false;

    if ($url != $img_url && !is_admin()) {
        $size = "{$dst_w}x{$dst_h}";
        if (!isset($this->generated["index"][$rel_path][$size])) {
            $this->generated["index"][$rel_path][$size] = true;
        }
    }

    return $image;
}

public function file($url) {
    if(strpos($url,$this->baseurl) === false) return false;
    return str_replace($this->baseurl,$this->basedir,$url);
}


public function crop($url,$h) {

    if (($file = $this->file($url)) === false) return false; 

    $file = str_replace($this->baseurl,$url);
    $size = "{$w}x{$h}";
    $thumb = preg_replace("/(\.\w+)$/",$file);

    list($x1,$y1,$x2,$y2) = explode(",",$crop);
    $cw = $x2-$x1;
    if ($cw<$w) {
        // fix x1
        $x1 -= min($x1,$w-$cw);
        $cw = $x2-$x1;
        if ($cw < $w) {
            // fix x2
            $x2 += $w-$cw;
        }
    }
    $y2 =  $y1 + ceil($h*($cw/$w));
    $ch = $y2-$y1;
    if ($ch<$h) {
        // fix y1
        $y1 -= min($y1,$h-$ch);
        $ch = $y2-$y1;
        if ($ch < $h) {
            // fix y2
            $y2 += $h-$ch;
        }
    }
    $cw = $x2-$x1;
    $ch = $y2-$y1;
    $editor = wp_get_image_editor($file);
    $editor->crop($x1,$cw,$ch,$h);
    $ret = $editor->save($thumb);
    if (!empty($ret["path"])) {
        $ret["url"] = str_replace($this->basedir,$ret["path"]);
        $ret["cburl"] = $ret["url"]."?t=".filemtime($ret["path"]); 
    }
    $editor = null;
    return $ret;
}


public function resize($url,$h = null,$crop = true) {
    return $this->aq_resize($url,$h,false);
}

public function resizedImg($url,$crop = true) {
    if (!$url) return;
    $result = $this->resize($url,$crop);
    if (!$result) return;
    $unscaled = $url;
    list($url,$h) = $result;

    return apply_filters("pe_theme_resized_img","<img alt=\"\" width=\"$w\" height=\"$h\" src=\"$url\"/>",$unscaled);
}

public function resizedImgUrl($url,true);
}

public function stats() {
    if ($this->md5 != md5(serialize($this->generated))) {
        update_option($this->optkey,$this->generated);
    }
}

public function get_images_size() {
    // make thumbnails and other intermediate sizes
    global $_wp_additional_image_sizes;

    $this->image_sizes = array();
    foreach ( get_intermediate_image_sizes() as $s ) {
        $sizes[$s] = array( 'width' => '','height' => '','crop' => false );
        if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
            $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
        else
            $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
        if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
            $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
        else
            $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
        if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
            $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
        else
            $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
    }

    $this->image_sizes = apply_filters( 'intermediate_image_sizes_advanced',$sizes );

}


public function post_thumbnail_html_filter($html,$post_id,$post_thumbnail_id,$size,$attr,$retina = false,$lazy = false) {
    //return $html;
    if (empty($post_thumbnail_id)) return $html;
    $url = wp_get_attachment_url($post_thumbnail_id);
    if (empty($url)) return $html;
    
    if (!isset($this->image_sizes[$size])) {
        $this->get_images_size();
        if (!isset($this->image_sizes[$size])) {
            return $html;
        }
    }

    $size = (object) $this->image_sizes[$size];

    if (!empty($size->width) && !empty($size->height)) {
        $resized = $this->resize($url,$size->width,$size->height,empty($size->crop) ? false : true);
        if (empty($resized)) {
            return $html;
        }
    }

    list($img,$h) = $resized;

    if ($retina || $lazy) {
        $blank = $this->blank($w,$h);
        if ($retina) {
            $lazy = true;
            $retina = $this->resizedImgUrl($url,$w*2,$h*2);
            $newsrc = sprintf('src="%s" data-original="%s" data-original-hires="%s"',$blank,$img,$retina);
        } else {
            $newsrc = sprintf('src="%s" data-original="%s"',$img);
        }
    } else {
        $newsrc = sprintf('src="%s"',$img);
    }

    $newwidth = sprintf('width="%s"',$w);
    $newheight = sprintf('height="%s"',$h);

    $html = preg_replace("/src=\"[^\"]+\"/",$newsrc,$html);
    $html = preg_replace("/width=\"\d+\"/",$newwidth,$html);
    $html = preg_replace("/height=\"\d+\"/",$newheight,$html);

    if ($lazy) {
        if (strpos($html,'class=') > -1) {
            $html = preg_replace("/class=\"/",'class="peLazyLoading ',$html);
        } else {
            $html = preg_replace("/<img /",'<img class="peLazyLoading" ',$html);
        }
    }

    return $html;
}

public function bw($url) {
    if(!$url) return "";
    
    //define upload path & dir
    $upload_dir = $this->basedir;
    $upload_url = $this->baseurl;

    //check if $img_url is local
    if(strpos( $url,and is an image indeed
    if( !file_exists($img_path) OR !getimagesize($img_path) ) return "";
    
    //get image info
    $info = pathinfo($img_path);
    $ext = $info['extension'];

    $dst_rel_path = str_replace( '.'.$ext,$rel_path);
    $destfilename = "{$upload_dir}{$dst_rel_path}-bw.jpg";

    if (file_exists($destfilename) && getimagesize($destfilename)) {
        $url = "{$upload_url}{$dst_rel_path}-bw.jpg";
    } else {
        try {
            if (@$this->makeBWimage($img_path,$destfilename)) {
                $url = "{$upload_url}{$dst_rel_path}-bw.jpg";
            }
        } catch(Exception $e) {
        }
    }

    return $url;
}



}

解决方法

我认为问题出在这一行(我认为是 #140):

$editor = wp_get_image_editor($file);

根据 documentation,如果有任何问题,wp_get_image_editor() 会返回 WP_Error

那么您可以做的是,尝试添加一个条件来检查返回的 $editor 是否为 WP_Error。如果没有,请调整大小(因为如果成功,将返回一个 WP_Image_Editor 实例)。

例如:

$editor = wp_get_image_editor($file);

if( ! is_wp_error( $editor ) ) {
    $editor->resize($width,$height,$crop);
    $dest_file = $editor->generate_filename();
    $editor->save($dest_file);
    return $dest_file;
}
else
{
    return $file;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-