PHP等比例生成缩略图
2024-10-21 06:42:12
/**
* 生成缩略图
* $imgSrc 图片源路径
* $resize_width 图片宽度
* $resize_height 图片高度
* $dstimg 缩略图路径
* $isCut 是否剪切图片
*/
public function reSizeImg($imgSrc, $resize_width, $resize_height, $dstimg, $isCut = false) {
//图片的类型
$type = substr(strrchr($imgSrc, "."), 1);
//初始化图象
if ($type == "jpg" || $type == "jpeg") {
$im = imagecreatefromjpeg($imgSrc);
}
if ($type == "gif") {
$im = imagecreatefromgif($imgSrc);
}
if ($type == "png") {
$im = imagecreatefrompng($imgSrc);
} $width = imagesx($im);
$height = imagesy($im); //生成图象
//改变后的图象的比例
$resize_ratio = ($resize_width) / ($resize_height);
//实际图象的比例
$ratio = ($width) / ($height);
if (($isCut) == 1) {
if ($ratio >= $resize_ratio) {
//高度优先
$newimg = imagecreatetruecolor($resize_width, $resize_height);
imagecopyresampled($newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, (($height) * $resize_ratio), $height);
ImageJpeg($newimg, $dstimg);
}
if ($ratio < $resize_ratio) {
//宽度优先
$newimg = imagecreatetruecolor($resize_width, $resize_height);
imagecopyresampled($newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, $width, (($width) / $resize_ratio));
ImageJpeg($newimg, $dstimg);
}
} else {
if ($ratio >= $resize_ratio) {
$newimg = imagecreatetruecolor($resize_width, ($resize_width) / $ratio);
imagecopyresampled($newimg, $im, 0, 0, 0, 0, $resize_width, ($resize_width) / $ratio, $width, $height);
ImageJpeg($newimg, $dstimg);
}
if ($ratio < $resize_ratio) {
$newimg = imagecreatetruecolor(($resize_height) * $ratio, $resize_height);
imagecopyresampled($newimg, $im, 0, 0, 0, 0, ($resize_height) * $ratio, $resize_height, $width, $height);
ImageJpeg($newimg, $dstimg);
}
}
ImageDestroy($im);
}
最新文章
- Visual Studio 2015 和 Apache Cordova 跨平台开发入门(一)
- Jetty 的工作原理以及与 Tomcat 的比较
- 加入Tomcat插件到ECLIPSE中的方法
- Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法【转载】
- TJI读书笔记12-接口
- Unity3D引用dll打包发布的问题及解决
- 解决android的ListView嵌套在ScrollView中不能被滚动的问题
- HDU 4858 项目管理(邻接表 暴力模拟)
- POJ 3286 How many 0&#39;s?(数位DP)
- django访问静态文件
- Delphi各个版本和发展历史(转)
- 分享我们项目中基于EF事务机制的架构 【转载】
- vue2.0+elementUI构建单页面后台管理平台
- LogBack学习
- 项目管理之 SVN 管理软件 CornerStone for Mac
- 从一个word文件中读取所有的表格和标题(2)
- Java课程设计报告——购物车
- Linux framebuffer deferred io机制
- Docke--Dockerfile指令介绍
- leetcode感想