Thinkphp自定义生成缩略图尺寸的方法

时间:2022-07-27
本文章向大家介绍Thinkphp自定义生成缩略图尺寸的方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Thinkphp自定义生成缩略图尺寸的方法,本实例中生成两张不同尺寸的图片:第一张是大图350*350,第二张 50*50的缩略图

Image类是Thinkphp系统自带的,可以研究下,这个缩略图类很强大

function getLogo($logo, $width, $height, $name) { 
   $fileArr = pathinfo($logo); 
   $dirname = $fileArr['dirname']; 
   $filename = $fileArr['filename']; 
   $extension = $fileArr['extension']; 
   $logo_rs = ""; 
   if ($width   0 && $height   0) { 
     $name_thumb = $dirname . "/" . $filename . "_" . $width . "_" . $height . "." . $extension; 
     if (!file_exists($name_thumb)) { 
       if (file_exists($logo)) { 
         $image = new ThinkImage(); 
         $image- open($logo); 
         $image- thumb($width, $height)- save($name_thumb); 
       } else { 
         $name_thumb = ""; 
       } 
     } 
     if ($name_thumb) { 
       $logo_rs = $name_thumb; 
     } 
   } else { 
     $logo_rs = $logo; 
   } 
   if ($logo_rs) { 
     if ($name) { 
       return "<img src='" . __APP__ . "/" . $logo_rs . "' alt='" . $name . "'/ "; 
     } else { 
       return __APP__ . "/" . $logo_rs; 
     } 
   } 
 }

模版自定义缩略图高度和宽度:生成350*350的缩略图,其它尺寸同理

<img alt="350*350" src="{$logo|getLogo=###,350,350}" /