您正在使用 IPV4 [18.209.209.28] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
[公益]保护绿色环境,构建和谐社会       悟空收录网      

【腾讯云】 爆款2核2G3M云服务器首年 61元,叠加红包再享折上折      
[公益] 地球是我家,绿化靠大家      
2024年 清明节 007
2024年 劳动节 034
2025年 元 旦 279
2025年 春 节 307
 
您现在的位置:首页 >> PHP >> 内容
本类新增
本类热门
PHP生成缩略图实例
内容摘要: ?php/** * 生成缩略图 * $imgSrc图片源路径 * $thumbWidth 缩略图宽度 * $thumbHeight 缩略图高度 * $thumbSrc缩略图路径 * $isCut是否剪切图片 */ function createThumbImg($imgSrc, $thumbWidth, $thumbHeight, $thumbSrc, $i......
<?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随机生成18位不重复的订单号

发布日期:2021/6/15
手机扫二维码直达本页
发布时间:12:34:56
点  击:3
录  入:壹家怡园
相关文章
Baidu
YiJiaCMS 7.3.8 build231228(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,Cloudflare提供加速防护
运行时间载入中.....