对图片进行等比例缩小

该博客介绍了一个PHP函数,用于处理上传的图片并按比例缩放,确保最大宽度不超过600像素,最大高度不超过800像素。通过getimagesize获取图片信息,然后根据比例创建新的图像尺寸,使用imagecreatefromjpeg、imagecreatefromgif和imagecreatefrompng等函数处理不同类型的图片,并使用imagecopyresampled进行等比例缩放。最后将处理后的图片保存为新的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 上传文件
 * @return string
 */
public function uploadFile() {
    if($this->checkError() && $this->checkSize()
        && $this->checkExt() && $this->checkMime()
        && $this->checkTrueImg() && $this->checkHTTPPost()) {

        $this->checkUploadPath();

        $this->uniName = $this->fileInfo['name'];//图片名字
        $this->destination = $this->uploadPath . '/' . $this->uniName;
        if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)) {
            $im           =    $this->destination;
            $tofile       =    $this->uploadPath . '/'.'tmp'.'/'. $this->uniName ;
            $maxwidth     =    600;       //设置图片的最大宽度
            $maxheight    =    800;      //设置图片的最大高度
            $res = $this->resize($im,$tofile,$maxwidth,$maxheight,'');
            if($res) return $res;
            else $this->error = '014';
            $this->showError();
        } else {
            $this->error = '013';
            $this->showError();
        }
    } else {
        $this->showError();
    }
}

/**
 * 设置宽高  按比例缩放
 */
function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{

    list($width, $height, $type, $attr) = getimagesize($srcImage);
    if($width < $maxWidth  || $height < $maxHeight) return ;
    switch ($type) {
        case 1: $img = imagecreatefromgif($srcImage); break;
        case 2: $img = imagecreatefromjpeg($srcImage); break;
        case 3: $img = imagecreatefrompng($srcImage); break;
    }
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
    if($scale < 1) {
        $newWidth = floor($scale*$width);
        $newHeight = floor($scale*$height);
        $newImg = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
        $newName = "";
        $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);
        switch($type) {
            case 1: if(imagegif($newImg, "$toFile$newName.gif" ))
                return "$toFile$newName.gif"; break;
            case 2: if(imagejpeg($newImg, "$toFile$newName.jpg"))
                return "$toFile$newName.jpg"; break;
            //case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
            case 3: if(imagepng($newImg, "$toFile$newName.png"));
                return "$toFile$newName.png"; break;
            default: if(imagejpeg($newImg, "$toFile$newName.jpg"))
                return "$toFile$newName.jpg"; break;
        }
        imagedestroy($newImg);
    }
    imagedestroy($img);
    return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值