BlogHelper::getPostImg PHP Method

getPostImg() public method

記事中の画像を取得する
public getPostImg ( array $post, array $options = [] ) : string
$post array ブログ記事
$options array オプション(初期値 : array()) - `num` : 何枚目の画像か順番を指定(初期値 : 1) - `link` : 詳細ページへのリンクをつけるかどうか(初期値 : true) - `alt` : ALT属性(初期値 : ブログ記事のタイトル)
return string
    public function getPostImg($post, $options = array())
    {
        $this->setContent($post['BlogPost']['blog_content_id']);
        $options = array_merge($_options = array('num' => 1, 'link' => true, 'alt' => $post['BlogPost']['name']), $options);
        extract($options);
        unset($options['num']);
        unset($options['link']);
        $contents = $post['BlogPost']['content'] . $post['BlogPost']['detail'];
        $pattern = '/<img.*?src="([^"]+)"[^>]*>/is';
        if (!preg_match_all($pattern, $contents, $matches)) {
            return '';
        }
        if (isset($matches[1][$num - 1])) {
            $url = $matches[1][$num - 1];
            $url = preg_replace('/^' . preg_quote($this->base, '/') . '/', '', $url);
            $img = $this->BcBaser->getImg($url, $options);
            if ($link) {
                return $this->BcBaser->getLink($img, $this->request->params['Content']['url'] . 'archives/' . $post['BlogPost']['no']);
            } else {
                return $img;
            }
        } else {
            return '';
        }
    }