首页 > php教程 > php手册 > 正文

php图片按比较生成缩略图片代码

WBOY
发布: 2016-06-13 09:48:43
原创
759 人浏览过

图片按在比较进行放大缩小,这得利用php教程 gd库的函数现实现,我们会利用到imagecreatetruecolor(),imagecopyresampled()来操作

function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
    if($dst_width         echo "params width or height error !";
        exit();
    }
    if(!file_exists($src_file)) {
        echo $src_file . " is not exists !";
        exit();
    }

    $type=exif_imagetype($src_file);
    $support_type=array(imagetype_jpeg , imagetype_png , imagetype_gif);

    if(!in_array($type, $support_type,true)) {
        echo "this type of image does not support! only support jpg , gif or png";
        exit();
    }

    switch($type) {
        case imagetype_jpeg :
            $src_img=imagecreatefromjpeg($src_file);
            break;
        case imagetype_png :
            $src_img=imagecreatefrompng($src_file);        
            break;
        case imagetype_gif :
            $src_img=imagecreatefromgif($src_file);
            break;
        default:
            echo "load image error!";
            exit();
    }
    $src_w=imagesx($src_img);
    $src_h=imagesy($src_img);
    $ratio_w=1.0 * $dst_width/$src_w;
    $ratio_h=1.0 * $dst_height/$src_h;
    if ($src_w         $x = ($dst_width-$src_w)/2;
        $y = ($dst_height-$src_h)/2;
        $new_img=imagecreatetruecolor($dst_width,$dst_height);
        imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
        switch($type) {
            case imagetype_jpeg :
                imagejpeg($new_img,$dst_file,100);
                break;
            case imagetype_png :
                imagepng($new_img,$dst_file);
                break;
            case imagetype_gif :
                imagegif($new_img,$dst_file);
                break;
            default:
                break;
        }
    } else {
        $dstwh = $dst_width/$dst_height;
        $srcwh = $src_w/$src_h;
        if ($ratio_w             $zoom_w = $dst_width;
            $zoom_h = $zoom_w*($src_h/$src_w);
        } else {
            $zoom_h = $dst_height;
            $zoom_w = $zoom_h*($src_w/$src_h);
        }

        $zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
        imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
        $new_img=imagecreatetruecolor($dst_width,$dst_height);
        $x = ($dst_width-$zoom_w)/2;
        $y = ($dst_height-$zoom_h)/2+1;
        imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
        switch($type) {
            case imagetype_jpeg :
                imagejpeg($new_img,$dst_file,100);
                break;
            case imagetype_png :
                imagepng($new_img,$dst_file);
                break;
            case imagetype_gif :
                imagegif($new_img,$dst_file);
                break;
            default:
                break;
        }
    }
}


总结,我们要生成比例生成小图就利用$dstwh = $dst_width/$dst_height;
        $srcwh = $src_w/$src_h;进行判断,然后再生成。

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!