php创建缩略图程序

  1. function Thumb_IM($thumbwidth, $thumbheight, $preview = 0) {
  2. global $thumbstatus, $imageimpath, $thumbquality;
  3. if($thumbstatus) {
  4. list($img_w, $img_h) = $this->attachinfo;
  5. $targetfile = !$preview ? ($thumbstatus == 1 || $thumbstatus == 3 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  6. if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
  7. if($thumbstatus != 3) {
  8. $exec_str = $imageimpath.'/convert -quality '.intval($thumbquality).' -geometry '.$thumbwidth.'x'.$thumbheight.' '.$this->targetfile.' '.$targetfile;
  9. @exec($exec_str, $output, $return);
  10. if(emptyempty($return) && emptyempty($output)) {
  11. $this->attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
  12. }
  13. } else {
  14. $imgratio = $img_w / $img_h;
  15. $thumbratio = $thumbwidth / $thumbheight;
  16. if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
  17. $cuty = $img_h;
  18. $cutx = $cuty * $thumbratio;
  19. } elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio < $thumbratio) {
  20. $cutx = $img_w;
  21. $cuty = $cutx / $thumbratio;
  22. }
  23. $exec_str = $imageimpath.'/convert -crop '.$cutx.'x'.$cuty.'+0+0 '.$this->targetfile.' '.$targetfile;
  24. @exec($exec_str, $output, $return);
  25. $exec_str = $imageimpath.'/convert -quality '.intval($thumbquality).' -geometry '.$thumbwidth.'x'.$thumbheight.' '.$targetfile.' '.$targetfile;
  26. @exec($exec_str, $output, $return);
  27. if(emptyempty($return) && emptyempty($output)) {
  28. $this->attach['thumb'] = $thumbstatus == 1 || $thumbstatus == 3 ? 1 : 0;
  29. }//开源代码phpfensi.com
  30. }
  31. }
  32. }
  33. }