仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2730|回复: 19
打印 上一主题 下一主题

[学习教程] PHP教程之php停止图片裁剪及生成缩略图法式源代码...

[复制链接]
简单生活 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:53:14 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
对于PHP的语法结构,刚开始真的很不习惯,真搞不懂为什么每个变量之前都要加个“$”符号,每个语句写完之后都必须加上“分号”来表示此句已经结束,还有,PHP对字母的大小写是敏感的,写的时候一定要注意大小写的区别。法式|源代码|缩略图   处置办法是:
1.当原图的宽或高任一比划定的尺寸小,只停止等比缩略处置,
2.当原图的宽与高都比划定尺寸大,先辈行等比缩略处置,然后算出居中地位停止裁剪

以下是源代码:
<?php
/*
*        &#36;o_photo 原图途径
*        &#36;d_photo 处置后图片途径
*        &#36;width   界说宽
*        &#36;height  界说高
*        挪用办法  cutphoto("test.jpg","temp.jpg",256,146);
*/

function cutphoto(&#36;o_photo,&#36;d_photo,&#36;width,&#36;height){

&#36;temp_img = imagecreatefromjpeg(&#36;o_photo);
&#36;o_width  = imagesx(&#36;temp_img);                                //获得原图宽
&#36;o_height = imagesy(&#36;temp_img);                                //获得原图高

//判别处置办法
if(&#36;width>&#36;o_width || &#36;height>&#36;o_height){        //原图宽或高比划定的尺寸小,停止紧缩

        &#36;newwidth=&#36;o_width;
        &#36;newheight=&#36;o_height;

        if(&#36;o_width>&#36;width){
                &#36;newwidth=&#36;width;
                &#36;newheight=&#36;o_height*&#36;width/&#36;o_width;
        }

        if(&#36;newheight>&#36;height){
                &#36;newwidth=&#36;newwidth*&#36;height/&#36;newheight;
                &#36;newheight=&#36;height;
        }

        //缩略图片
        &#36;new_img = imagecreatetruecolor(&#36;newwidth, &#36;newheight);  
        imagecopyresampled(&#36;new_img, &#36;temp_img, 0, 0, 0, 0, &#36;newwidth, &#36;newheight, &#36;o_width, &#36;o_height);  
        imagejpeg(&#36;new_img , &#36;d_photo);                 
        imagedestroy(&#36;new_img);


}else{                                                                                //原图宽与高都比划定尺寸大,停止紧缩后裁剪

        if(&#36;o_height*&#36;width/&#36;o_width>&#36;height){        //先肯定width与划定不异,假如height比划定大,则ok
                &#36;newwidth=&#36;width;
                &#36;newheight=&#36;o_height*&#36;width/&#36;o_width;
                &#36;x=0;
                &#36;y=(&#36;newheight-&#36;height)/2;
        }else{                                                                        //不然肯定height与划定不异,width自顺应
                &#36;newwidth=&#36;o_width*&#36;height/&#36;o_height;
                &#36;newheight=&#36;height;
                &#36;x=(&#36;newwidth-&#36;width)/2;
                &#36;y=0;
        }

        //缩略图片
        &#36;new_img = imagecreatetruecolor(&#36;newwidth, &#36;newheight);  
        imagecopyresampled(&#36;new_img, &#36;temp_img, 0, 0, 0, 0, &#36;newwidth, &#36;newheight, &#36;o_width, &#36;o_height);  
        imagejpeg(&#36;new_img , &#36;d_photo);                 
        imagedestroy(&#36;new_img);
         
        &#36;temp_img = imagecreatefromjpeg(&#36;d_photo);
        &#36;o_width  = imagesx(&#36;temp_img);                                //获得缩略图宽
        &#36;o_height = imagesy(&#36;temp_img);                                //获得缩略图高

        //裁剪图片
        &#36;new_imgx = imagecreatetruecolor(&#36;width,&#36;height);
        imagecopyresampled(&#36;new_imgx,&#36;temp_img,0,0,&#36;x,&#36;y,&#36;width,&#36;height,&#36;width,&#36;height);
        imagejpeg(&#36;new_imgx , &#36;d_photo);
        imagedestroy(&#36;new_imgx);
}

}
?>一些真正的强人总会搞出新玩意来丢给你,你不学就落后了,也印证了前人的经验,果然是学无止境啊!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-5 01:30

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表