PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通

QQ:420220301 微信/手机:150-3210-7690
当前位置:首页 > CMS教程 > Fastadmin

图片上传自动修改尺寸

管理员 2024-12-14
Fastadmin
7

applicationcommonlibraryUpload.php
最后加入如下函数:

public function resizeImage($im, $dest, $maxwidth, $maxheight) {        $img = getimagesize($im);        switch ($img[2]) {            case 1:                $im = @imagecreatefromgif($im);                break;            case 2:                $im = @imagecreatefromjpeg($im);                break;            case 3:                $im = @imagecreatefrompng($im);                break;        }        $pic_width = imagesx($im);        $pic_height = imagesy($im);        $resizewidth_tag = false;        $resizeheight_tag = false;        if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {            if ($maxwidth && $pic_width > $maxwidth) {                $widthratio = $maxwidth / $pic_width;                $resizewidth_tag = true;            }            if ($maxheight && $pic_height > $maxheight) {                $heightratio = $maxheight / $pic_height;                $resizeheight_tag = true;            }            if ($resizewidth_tag && $resizeheight_tag) {                if ($widthratio < $heightratio)                    $ratio = $widthratio;                else                    $ratio = $heightratio;            }            if ($resizewidth_tag && !$resizeheight_tag)                $ratio = $widthratio;            if ($resizeheight_tag && !$resizewidth_tag)                $ratio = $heightratio;            $newwidth = $pic_width * $ratio;            $newheight = $pic_height * $ratio;            if (function_exists("imagecopyresampled")) {                $newim = imagecreatetruecolor($newwidth, $newheight);                imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);            } else {                $newim = imagecreate($newwidth, $newheight);                imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);            }            imagejpeg($newim, $dest);            imagedestroy($newim);        } else {            imagejpeg($im, $dest);        }    }

328行处插入代码:

if($this->fileInfo['imagewidth']>1920||$this->fileInfo['imageheight']>1920){            $this->resizeImage(ROOT_PATH . '/public' . $uploadDir . $fileName, ROOT_PATH . '/public' . $uploadDir . $fileName,1000,1000);            $imgInfo = getimagesize(ROOT_PATH . '/public' . $uploadDir . $fileName);            if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {                throw new UploadException(__('Uploaded file is not a valid image'));            }            $this->fileInfo['imagewidth'] = isset($imgInfo[0]) ? $imgInfo[0] : 0;            $this->fileInfo['imageheight'] = isset($imgInfo[1]) ? $imgInfo[1] : 0;            $this->fileInfo['size']=filesize(ROOT_PATH . '/public' . $uploadDir . $fileName);        }

希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。

相关推荐

扫码关注

qrcode

QQ交谈

回顶部