发布于 2014-12-17 10:55:11 | 124 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的PHP面向对象编程,程序狗速度看过来!

PHP开源脚本语言

PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。


本文是一个php图片缩放实现方法示例,感兴趣的同学参考下.

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>类型的控制。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务