搜索
首页php教程php手册php进行图片裁剪及生成缩略图程序源代码

程序|源代码|缩略图

处理方法是:
1.当原图的宽或高任一比规定的尺寸小,只进行等比缩略处理,
2.当原图的宽与高都比规定尺寸大,先进行等比缩略处理,然后算出居中位置进行裁剪

以下是源代码:
/*
*        $o_photo 原图路径
*        $d_photo 处理后图片路径
*        $width   定义宽
*        $height  定义高
*        调用方法  cutphoto("test.jpg","temp.jpg",256,146);
*/

function cutphoto($o_photo,$d_photo,$width,$height){

$temp_img = imagecreatefromjpeg($o_photo);
$o_width  = imagesx($temp_img);                                //取得原图宽
$o_height = imagesy($temp_img);                                //取得原图高

//判断处理方法
if($width>$o_width || $height>$o_height){        //原图宽或高比规定的尺寸小,进行压缩

        $newwidth=$o_width;
        $newheight=$o_height;

        if($o_width>$width){
                $newwidth=$width;
                $newheight=$o_height*$width/$o_width;
        }

        if($newheight>$height){
                $newwidth=$newwidth*$height/$newheight;
                $newheight=$height;
        }

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


}else{                                                                                //原图宽与高都比规定尺寸大,进行压缩后裁剪

        if($o_height*$width/$o_width>$height){        //先确定width与规定相同,如果height比规定大,则ok
                $newwidth=$width;
                $newheight=$o_height*$width/$o_width;
                $x=0;
                $y=($newheight-$height)/2;
        }else{                                                                        //否则确定height与规定相同,width自适应
                $newwidth=$o_width*$height/$o_height;
                $newheight=$height;
                $x=($newwidth-$width)/2;
                $y=0;
        }

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

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

}
?>



声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境