温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用php怎么对图片进行缩放功能

发布时间:2020-12-25 15:35:58 来源:亿速云 阅读:111 作者:Leah 栏目:开发技术

使用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怎么对图片进行缩放功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI