27 2008

php按指定大小生成不变形缩略图的函数

Published by at 16:39 under PHP,Web技术

今天需要写一个用php生成缩略图的函数,在网上转了转,发现了一个好人写的一段代码,感觉很标准,就拿来给大家分享分享这个按指定大小生成不变形缩略图的函数。

<?php
function image_resize($f, $t, $tw, $th){
// 按指定大小生成缩略图,而且不变形,缩略图函数
// Cos.x 2007-9-5
        $temp = array(1=>’gif’, 2=>’jpeg’, 3=>’png’);

        list($fw, $fh, $tmp) = getimagesize($f);

        if(!$temp[$tmp]){
                return false;
        }
        $tmp = $temp[$tmp];
        $infunc = “imagecreatefrom$tmp”;
        $outfunc = “image$tmp”;

        $fimg = $infunc($f);

        if($fw/$tw > $fh/$th){
                $fw = $tw * ($fh/$th);
        }else{
                $fh = $th * ($fw/$tw);
        }

        $timg = imagecreatetruecolor($tw, $th);
        imagecopyresampled($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh);
        if($outfunc($timg, $t)){
                return true;
        }else{
                return false;
        }
}
?>

No responses yet

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.