网络编程 
首页 > 网络编程 > 浏览文章

php图片缩放实现方法

(编辑:jimmy 日期: 2024/10/13 浏览:3 次 )

php基础练习--图片缩放:
复制代码 代码如下:
<?php
    /**
    * image zoom.
    */
    function imageZoom($filename, $w, $h) {
        /* Arguments meaning */
        /* $filename: the source of the name */
        /* $w: you want get the image's width */
        /* $h: you want get the imgage's height */
        $arr = getimagesize($filename);
        $src_w = $arr[0];
        $src_h = $arr[1];
        $src_t = $arr[2];
        /*1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),
= TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,
= IFF,15 = WBMP,16 = XBM*/
        $src_m = $arr['mime'];

        $src_img = imagecreatefromjpeg($filename);

        if (($w / $src_w) >($h / $src_h)) {
            $bili = $h / $src_h;
        } else {
            $bili = $w / $src_h;
        }
        $dst_w = $src_w * $bili;
        $dst_h = $src_h * $bili;
        $dst_img = imagecreatetruecolor($dst_w, $dst_h);

        imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);

        header("content-type:{$src_m}");

        switch ($src_t) {
            case 1:
                $imgout = "imagegif";
                break;
            case 2:
                $imgout = "imagejpeg";
                break;
            case 3:
                $imgout = "imagepng";
                break;
            default:
                echo "The type was wrong!";
                break;
        }

        $dst_filename = "s_".$filename;
        $imgout($dst_img, $dst_filename);

        imagedestroy($dst_img);
    }
    $filename = 'gg.jpg';
    imageZoom($filename, 100, 200);

核心:<1>注意缩放比例如何得到,虽然这样得到的图片可能会与预想的有点差别,但是最起码保证了缩放比例。

   <2>类型的控制。

上一篇:php多文件上传实现代码
下一篇:php跨域cookie共享使用方法
一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。