search
Homephp教程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);
}

}
?>



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools