公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

这篇文章主要介绍了PHP生成缩略图实例讲解,文章列举了实例代码,有正好需要的同学可以借鉴下。


封装的方法函数:

<?php
/**
 * 生成缩略图
 * $imgSrc     图片源路径
 * $thumbWidth   缩略图宽度
 * $thumbHeight  缩略图高度
 * $thumbSrc    缩略图路径
 * $isCut     是否剪切图片
 */
function createThumbImg($imgSrc, $thumbWidth, $thumbHeight, $thumbSrc, $isCut = false) {
    //1.获取图片的类型
    $type = substr(strrchr($imgSrc, "."), 1);
    //2.初始化图象
    if ($type == "jpg" || $type == "jpeg") {
        //创建一块画布,并从JPEG文件或URL地址载入一副图像
        $sourceImg = imagecreatefromjpeg($imgSrc);
    }elseif ($type == "gif") {
        //创建一块画布,并从GIF文件或URL地址载入一副图像
        $sourceImg = imagecreatefromgif($imgSrc);
    }elseif ($type == "png") {
        //创建一块画布,并从PNG文件或URL地址载入一副图像
        $sourceImg = imagecreatefrompng($imgSrc);
    }elseif ($type == "wbmp") {
        //创建一块画布,并从WBMP文件或URL地址载入一副图像
        $sourceImg = imagecreatefromwbmp($imgSrc);
    }
    //取得图像宽度
    $width = imagesx($sourceImg);
    //取得图像高度
    $height = imagesy($sourceImg);
 
    //3.生成图象
    //缩略图的图象比例
    $scale = ($thumbWidth) / ($thumbHeight);
    //源图片的图象比例
    $ratio = ($width) / ($height);
    if (($isCut) == 1) {
        //高度优先
        if ($ratio >= $scale) {        
            //创建真彩图像资源(imagecreatetruecolor()函数使用GDLibrary创建新的真彩色图像)
            $newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
            //图像处理
            imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, (($height) * $scale), $height);
            //以JPEG格式将图像输出到浏览器或文件
            ImageJpeg($newimg, $thumbSrc);
        }
        //宽度优先
        if ($ratio < $scale) {       
            $newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
            imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, (($width) / $scale));
            ImageJpeg($newimg, $thumbSrc);
        }
    } else {
        if ($ratio >= $scale) {
            $newimg = imagecreatetruecolor($thumbWidth, ($thumbWidth) / $ratio);
            imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, ($thumbWidth) / $ratio, $width, $height);
            ImageJpeg($newimg, $thumbSrc);
        }
        if ($ratio < $scale) {
            $newimg = imagecreatetruecolor(($thumbHeight) * $ratio, $thumbHeight);
            imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, ($thumbHeight) * $ratio, $thumbHeight, $width, $height);
            ImageJpeg($newimg, $thumbSrc);
        }
    }
    //销毁图像
    ImageDestroy($sourceImg);
}
?>

调用示例:

<?php
    //图片源路径
    $imgSrc="D:/PHP/test/demo.jpg";
    //缩略图路径
    $thumbSrc="D:/PHP/test/thumb.jpg";
    createThumbImg($path,100,100,$thumbSrc);
?>



分享到:
标签:PHP生成缩略图 实例讲解
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定