用于functions.php中的循环问题

时间:2019-08-16 16:52:10

标签: php wordpress

我会跳到这个问题。 我有一个模糊图片的功能,并且里面有这个循环:

for ($x=1; $x <=40; $x++){
    imagefilter($pic, IMG_FILTER_GAUSSIAN_BLUR, 999);
}

它在我的其他模板中可以正常工作,但是当我尝试保存functions.php文件时,在此行中出现错误:“超过了20秒的最大执行时间” 我试图使用.htaccess来超过进程超时,但这没有用。 我真的很困惑,因为此功能可以在其他模板中使用,并且完全没有问题。 全功能代码:

<?php
add_theme_support('post-thumbnails');
require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
register_nav_menus(array(
    'primary' => __('Primary Menu', 'wordpress'),
    'postmenu' => __('postm')
));
function wpdocs_my_search_form($form)
{
    $form = '<form role="search" method="get" id="searchform" class="form-inline my-2 my-lg-0" action="' . home_url('/') . '" >
    <input id="sv" class="form-control mr-sm-2 d-none " dir="rtl" type="search" aria-label="Search" value="' . get_search_query() . '" name="s" />
    <button type="button" style="background-color: transparent; border: 0px"><i id="sche" style="cursor: pointer" class="fa fa-search"></i></button>
    </form>';

    return $form;
}
function check_img($image)
{
    if ($image == '') {
        return get_home_url() . '/defpic.jpg';
    } else {
        return $image;
    }
}

function image_web($image, $width, $height, $name)
{
    $info = getimagesize($image);
    $name .= '.jpg';
    //$mimetype = $info['mime']; 
    $mimetype   = image_type_to_mime_type($info[2]);
    $allowTypes = array(
        'image/jpeg',
        'image/png',
        'image/gif'
    );
    if (in_array($mimetype, $allowTypes)) {
        switch ($mimetype) {
            case 'image/jpeg':
                $image = imagecreatefromjpeg($image);
                break;
            case 'image/gif':
                $image = imagecreatefromgif($image);
                break;
            case 'image/png':
                $image = imagecreatefrompng($image);
                break;
            default:
                die('Invalid image type.');
        }
    } else {
        echo 'Is not a image';
    }
    $image2 = $image; 
    $wor    = imagesx($image); 
    $hor    = imagesy($image); 
    if ($hor >= $wor) {
        if ($wor >= $width) {  dimensions
            $hcenter = ($hor / 2) - ($height / 2);
            $back    = imagecreatetruecolor(round($width), round($height));
            imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $wor, $hor, $wor, $hor);
        } else {
            $hcenter = ($hor / 2) - ($height / 2); 
            $hnu     = ($hor * $width) / $wor;
            $back    = imagecreatetruecolor(round($width), round($height));
            imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
        }
    } else {
        $ratio = $wor / $hor;
        if ($ratio > 1.7777777) {
            $wnu     = ($wor * $height) / $hor;
            $wcenter = ($wnu / 2) - ($width / 2);
            $back    = imagecreatetruecolor(round($width), round($height));
            imagecopyresampled($back, $image, -$wcenter, 0, 0, 0, $wnu, $height, $wor, $hor);
        } else {
            $hnu     = ($wor * $height) / $hor;
            $hcenter = ($hnu / 2) - ($height / 2);
            $back    = imagecreatetruecolor(round($width), round($height));
            imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
        }
    }

    //The Error tells there's a problem at this line
    for ($x = 1; $x <= 40; $x++) {
        imagefilter($back, IMG_FILTER_GAUSSIAN_BLUR, 999);
    }

    imagefilter($back, IMG_FILTER_SMOOTH, 99);
    imagefilter($back, IMG_FILTER_BRIGHTNESS, 10);
    $src_w = imagesx($image2);
    $src_h = imagesy($image2);

    $x_ratio = $width / $src_w;
    $y_ratio = $height / $src_h;
    if (($src_w <= $width) && ($src_h <= $height)) {
        $new_w = $src_w;
        $new_h = $src_h;
    } elseif (($x_ratio * $src_h) < $height) {
        $new_h = ceil($x_ratio * $src_h);
        $new_w = $width;
    } else {
        $new_w = ceil($y_ratio * $src_w);
        $new_h = $height;
    }
    $front = imagecreatetruecolor(round($new_w), round($new_h));
    imagecopyresampled($front, $image2, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
    if ($new_h >= $new_w) { 
        $wctr = ($new_w / 2) - ($width / 2);
        imagecopymerge($back, $front, -$wctr, 0, 0, 0, $new_w, $new_h, 100);
    } elseif ($new_w >= $new_h) {
        $hctr = ($new_h / 2) - ($height / 2);
        $wctr = ($new_w / 2) - ($width / 2);
        imagecopymerge($back, $front, -$wctr, -$hctr, 0, 0, $new_w, $new_h, 100);
    } else { 
        $hctr = ($new_h / 2) - ($height / 2);
        imagecopymerge($back, $front, 0, -$hctr, 0, 0, $new_w, $new_h, 100);
    }

    imagejpeg($back, $name, 90);
    imagedestroy($back);
    imagedestroy($front);
}
add_filter('get_search_form', 'wpdocs_my_search_form');
?>

0 个答案:

没有答案