如何编写php函数或设置参数来返回图像的无高度值?

前端之家收集整理的这篇文章主要介绍了如何编写php函数或设置参数来返回图像的无高度值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题来自这个 How to proportionally size images to fit dimensions of 200px x 150px thumbnail in css?,但是因为我觉得它不再是CSS相关的问题,我想我会创建一个新问题.我试图按比例将图像放入缩略图这里 http://giantmango.com/contest.我尝试将css img标签设置为下面,但所有图像的大小都是200px x 200px.没有另一条css线的高度为200px.我怀疑它是别的东西.
img {
max-height: 150px;
max-width: 200px;
}

调用函数以返回图像并认为它可能是这样.

<?PHP 
    $content = get_the_content('Concept');
    $content = apply_filters('the_content',$content);
    list($col_class,$grid_img) = adjust_grid_image(
        $content,$col_w,$gap_w,$max_col,$flg_img_forcelink,$flg_obj_fit
    );
?>
        <div <?PHP post_class('grid-item ' . $col_class); ?> id="post-<?PHP the_ID(); ?>">
            <?PHP if ($grid_img) echo '<div class="grid-image">' . $grid_img . '</div>'; ?>

这些是我设置的参数.

$col_w = 200; // width of grid column
$gap_w = 7;  // padding + margin-right (15+15+5)
$max_col = 5; // max column size (style div.x1 ~ xN)
$flg_img_forcelink = true;   // add/overwrite a link which links to a single post (permalink).
$flg_img_extract = true;    // in single post page,extract thumbnail link to an original image.
$flg_obj_fit = 'large-fit';  // none | small-fit | large-fit ... how to fit size of object tag.

这是运行adjust_grid_image的functions.PHP文件,但我不确定它在做什么.这是否将我的图像设置为200px x 200px?如果是这样,我必须更改哪个参数才能将200px硬编码为图像的高度,以便我可以在css中设置它?

/* 
 * return class name and image tag (resized w/h attributes) to fit a grid.
 */
function adjust_grid_image($content,$flg_obj_fit) {
    global $post;

    $col_class_base = 'x';
    $col_class = $col_class_base . '1'; // default column-width class
    $arr_w = array();
    for ($i=0; $i<$max_col; $i++) {
        $arr_w[] = ($col_w * ($i+1)) + ($gap_w * $i);
    }

    $grid_img = '';
    $w = $h = 0;
    $matches1 = $matches2 = $matches3 = array();

    // search *first* img/object tag
    preg_match('/<(img|object)(?:[^>]+?)>/',$content,$matches1);

    if ($matches1[1] == 'img') {
        preg_match('/<img(?:.+?)src="(.+?)"(?:[^>]+?)>/',$matches2);
        $img_url = ($matches2[1]) ? $matches2[1] : '';
        if ($img_url) {
            // first,try to get attributes
            $matches_w = $matches_h = array();
            preg_match('/width="([0-9]+)"/',$matches2[0],$matches_w);
            preg_match('/height="([0-9]+)"/',$matches_h);
            if ($matches_w[1] and $matches_h[1]) {
                $w = $matches_w[1];
                $h = $matches_h[1];
            }
            else {
                // ... or get original size info.
                $upload_path = trim( get_option('upload_path') ); 
                $mark = substr(strrchr($upload_path,"/"),1); // default mark is 'uploads'
                preg_match("#$mark(/.+)$#",$img_url,$split_url);
 // split($mark,$img_url)
                if ($split_url[1] != null) {
                    $img_path = $upload_path . $split_url[1];
                    list($w,$h) = @getimagesize($img_path);
                }
            }
        }

        for ($i=0; $i<$max_col; $i++) { // set new width and col_class
            if ( ($i >= $max_col - 1) or ($w < $arr_w[$i+1]) ) {
                $nw = $arr_w[$i];
                $col_class = $col_class_base . ($i+1);
                break;
            }
        }
        $nh = (!$w or !$h) ? $nw : intval( ($h * $nw) / $w ); // set new height

        $grid_img = $matches2[0];
        // add width/height properties if nothing
        $flg_no_w = (strpos($grid_img_edit,'width=') === false);
        $flg_no_h = (strpos($grid_img_edit,'height=') === false);
        if ($flg_no_w or $flg_no_h) {
            $grid_img_close = (substr($grid_img,-2) == '/>') ? '/>' : '>';
            $grid_img_edit = substr( $grid_img,-(strlen($grid_img_close)) );
            $grid_img_edit .= ($flg_no_w) ? ' width="0"' : '';
            $grid_img_edit .= ($flg_no_h) ? ' height="0"' : '';
            $grid_img = $grid_img_edit . $grid_img_close;
        } 
        // replace new width/height properties
        $grid_img = preg_replace('/width="(\d+)"/','width="'. $nw .'"',$grid_img);
        $grid_img = preg_replace('/height="(\d+)"/','height="'. $nh .'"',$grid_img);

        // check image link
        //$chk_imglink = '/(<a(?:.+?)rel="(?:lightBox[^"]*?)"(?:[^>]*?)>)'. preg_quote($matches2[0],'/') .'/';
        $chk_imglink = '/(<a(?:.+?)href="(?:.+?\.(?:jpe?g|png|gif))"(?:[^>]*?)>)'. preg_quote($matches2[0],'/') .'/';
        if ($flg_img_forcelink) {
            $grid_img = '<a href="'. get_permalink() .'" title="' . esc_attr($post->post_title) . '">' . $grid_img . '</a>';
        }
        else if ( preg_match($chk_imglink,$matches3) ) {
            $grid_img = $matches3[1] . $grid_img . '</a>';
        }
    }
    else if ($matches1[1] == 'object') {
        preg_match('/<object(.+?)<\/object>/',$matches2);

        $matches_w = $matches_h = array();
        preg_match('/width="([0-9]+)"/',$matches_w);
        preg_match('/height="([0-9]+)"/',$matches_h);
        if ($matches_w[1] and $matches_h[1]) {
            $w = $matches_w[1];
            $h = $matches_h[1];
        }
        else {
            $flg_obj_fit = 'none';
        }

        //set col_class (and new width if in '*-fit' condition)
        if ($flg_obj_fit == 'small-fit') {
            for ($i=0; $i<$max_col; $i++) { 
                if ($i >= $max_col -1) {
                    $nw = $arr_w[$i];
                    $col_class = $col_class_base . ($i+1);
                    break;
                }
                else if ( $w < $arr_w[$i+1] ) {
                    $nw = $arr_w[$i];
                    $col_class = $col_class_base . ($i+1);
                    break;
                }
            }
        }
        else if ($flg_obj_fit == 'large-fit') {
            for ($i=$max_col -1; $i>=0; $i--) { 
                if ( $w > $arr_w[$i] ) {
                    if ($i >= $max_col -1) {
                        $nw = $arr_w[$i];
                        $col_class = $col_class_base . ($i+1);
                    }
                    else {
                        $nw = $arr_w[$i+1];
                        $col_class = $col_class_base . ($i+2);
                    }
                    break;
                }
                if ($i == 0) {
                    $nw = $arr_w[$i];
                    $col_class = $col_class_base . ($i+1);
                }
            }
        }
        else {
            for ($i=0; $i<$max_col; $i++) { 
                if ($i >= $max_col -1) {
                    $col_class = $col_class_base . ($i+1);
                    break;
                }
                else if ( $w < $arr_w[$i] ) {
                    $col_class = $col_class_base . ($i+1);
                    break;
                }
            }
        }
        $nh = (!$w or !$h) ? $nw : intval( ($h * $nw) / $w ); // set new height

        $grid_img = $matches2[0];
        if ($flg_obj_fit == 'small-fit' or $flg_obj_fit == 'large-fit') {
            // replace new width/height properties
            $grid_img = preg_replace('/width="(\d+)"/',$grid_img);
            $grid_img = preg_replace('/height="(\d+)"/',$grid_img);
        }
    }

    return array($col_class,$grid_img);
}

感谢您查看所有这些内容.

这是一些中间功能.代码中可能还有一些其他部分使用此函数的返回值并实际输出HTML.您应找到此部件并注释输出宽度和高度设置的部件.

但要注意,如果你的图像具有可变比例并且你重新缩放它们以保持这些比例(通过CSS),它们将不再适合网格.它们在页面上看起来比现在更加分散.

编辑现在您已经添加了更多详细信息,我认为应该足以删除以注释//添加宽度/高度属性开头的部分,如果没有

原文链接:https://www.f2er.com/php/138924.html

猜你在找的PHP相关文章