WordPress 可用的缩略图函数

2022-12-17

记录一个 WordPress 获取文章中的图片作为缩略图,并缓存的函数,方便以后应用。

  1. <?php
  2. if( !defined( 'THEME_THUMBNAIL_PATH' ) ) define( 'THEME_THUMBNAIL_PATH', '/cache/theme-thumbnail' ); //存储目录
  3. function biji_build_empty_index( $path ){ //生成空白首页
  4.     $index = $path . '/index.php';
  5.     if( is_file( $index ) ) return;
  6.     wp_mkdir_p( $path );
  7.     file_put_contents( $index, "<?php\n// Silence is golden.\n" );
  8. }
  9. function biji_crop_thumbnail( $url, $width, $height = null ){ //裁剪图片
  10.     $width = (int) $width;
  11.     $height = empty( $height ) ? $width : (int) $height;
  12.     $hash = md5( $url );
  13.     $file_path = constant( 'WP_CONTENT_DIR' ) . constant( 'THEME_THUMBNAIL_PATH' ) . "/$hash-$width-$height.jpg";
  14.     $file_url = content_url( constant( 'THEME_THUMBNAIL_PATH' ) . "/$hash-$width-$height.jpg" );
  15.     if( is_file( $file_path ) ) return $file_url;
  16.     $editor = wp_get_image_editor( $url );
  17.     if( is_wp_error( $editor ) ) return $url;
  18.     $size = $editor->get_size();
  19.     $dims = image_resize_dimensions( $size['width'], $size['height'], $width, $height, true );
  20.     //if( !$dims ) return $url;
  21.     $cmp = min( $size['width'] / $width, $size['height'] / $height );
  22.     if( is_wp_error( $editor->crop( $dims[2], $dims[3], $width * $cmp, $height * $cmp, $width, $height ) ) ) return $url;
  23.     biji_build_empty_index( constant( 'WP_CONTENT_DIR' ) . constant( 'THEME_THUMBNAIL_PATH' ) );
  24.     return is_wp_error( $editor->save( $file_path, 'image/jpg' ) ) ? $url : $file_url;
  25. }
  26. //缩略图获取post_thumbnail
  27. function post_thumbnail($width = 275,$height = 170 )
  28. {
  29.     global $post;
  30.     //如果有特色图片则取特色图片
  31.     if( has_post_thumbnail( $post->ID ) ){
  32.         $thumbnail_ID = get_post_thumbnail_id( $post->ID );
  33.         $thumbnailsrc = wp_get_attachment_image_src( $thumbnail_ID, 'full' );
  34.         return biji_crop_thumbnail($thumbnailsrc[0],$width,$height);
  35.     } else {
  36.         $content = $post->post_content;
  37.         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)? >/sim', $content, $strResult, PREG_PATTERN_ORDER);
  38.         if(count($strResult[1]) > 0) return biji_crop_thumbnail($strResult[1][0],$width,$height);
  39.         else{
  40.             return false;
  41.         }
  42.     }
  43. }
  44. ?>

    在调用时,可以这样调用:

    1. <?php if (post_thumbnail(110, 110)){ ?>
    2.   <img src="<?php echo post_thumbnail(110, 110); ?>" srcset="<?php echo post_thumbnail(220, 220); ?> 2x"/>
    3. <?php }else{
    4.    echo '<i class="icon-file-text2"></i>';
    5.   }?>
    
    
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明 1、本站所发布的全部内容源于互联网搬运,(包括源代码、软件、学习资料等)本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的23个小时之内,从您的电脑或手机中彻底删除上述内容。
2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容!敬请谅解! 侵删请致信E-mail:messi0808@qq.com
3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为www.xmy7.com
4、如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

小蚂蚁资源网 cms教程 WordPress 可用的缩略图函数 https://www.xmy7.com/zh/cms/25505.html

相关文章