当前位置: 首页 > news >正文

php显示缩小的图片代码,php等比例缩放图片及剪切图片代码分享

php等比例缩放图片及剪切图片代码分享

来源:中文源码网    浏览: 次    日期:2018年9月2日

【下载文档:  php等比例缩放图片及剪切图片代码分享.txt 】

(友情提示:右键点上行txt文档名->目标另存为)

php等比例缩放图片及剪切图片代码分享 php等比例缩放图片及剪切图片代码分享

/**

* 图片缩放函数(可设置高度固定,宽度固定或者最大宽高,支持gif/jpg/png三种类型)

* Author : Specs

*

* @param string $source_path 源图片

* @param int $target_width 目标宽度

* @param int $target_height 目标高度

* @param string $fixed_orig 锁定宽高(可选参数 width、height或者空值)

* @return string

*/

function myImageResize($source_path, $target_width = 200, $target_height = 200, $fixed_orig = ''){

$source_info = getimagesize($source_path);

$source_width = $source_info[0];

$source_height = $source_info[1];

$source_mime = $source_info['mime'];

$ratio_orig = $source_width / $source_height;

if ($fixed_orig == 'width'){

//宽度固定

$target_height = $target_width / $ratio_orig;

}elseif ($fixed_orig == 'height'){

//高度固定

$target_width = $target_height * $ratio_orig;

}else{

//最大宽或最大高

if ($target_width / $target_height > $ratio_orig){

$target_width = $target_height * $ratio_orig;

}else{

$target_height = $target_width / $ratio_orig;

}

}

switch ($source_mime){

case 'image/gif':

$source_image = imagecreatefromgif($source_path);

break;

case 'image/jpeg':

$source_image = imagecreatefromjpeg($source_path);

break;

case 'image/png':

$source_image = imagecreatefrompng($source_path);

break;

default:

return false;

break;

}

$target_image = imagecreatetruecolor($target_width, $target_height);

imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height);

//header('Content-type: image/jpeg');

$imgArr = explode('.', $source_path);

$target_path = $imgArr[0] . '_new.' . $imgArr[1];

imagejpeg($target_image, $target_path, 100);

}

用法: myImageResize($filename, 200, 200); //最大宽高

myImageResize($filename, 200, 200, 'width'); //宽度固定

myImageResize($filename, 200, 200, 'height'); //高度固定

剪切图片为固定大小:

function imagecropper($source_path, $target_width, $target_height){

$source_info = getimagesize($source_path);

$source_width = $source_info[0];

$source_height = $source_info[1];

$source_mime = $source_info['mime'];

$source_ratio = $source_height / $source_width;

$target_ratio = $target_height / $target_width;

// 源图过高

if ($source_ratio > $target_ratio){

$cropped_width = $source_width;

$cropped_height = $source_width * $target_ratio;

$source_x = 0;

$source_y = ($source_height - $cropped_height) / 2;

}elseif ($source_ratio < $target_ratio){ // 源图过宽

$cropped_width = $source_height / $target_ratio;

$cropped_height = $source_height;

$source_x = ($source_width - $cropped_width) / 2;

$source_y = 0;

}else{ // 源图适中

$cropped_width = $source_width;

$cropped_height = $source_height;

$source_x = 0;

$source_y = 0;

}

switch ($source_mime){

case 'image/gif':

$source_image = imagecreatefromgif($source_path);

break;

case 'image/jpeg':

$source_image = imagecreatefromjpeg($source_path);

break;

case 'image/png':

$source_image = imagecreatefrompng($source_path);

break;

default:

return false;

break;

}

$target_image = imagecreatetruecolor($target_width, $target_height);

$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

// 裁剪

imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);

// 缩放

imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

$dotpos = strrpos($source_path, '.');

$imgName = substr($source_path, 0, $dotpos);

$suffix = substr($source_path, $dotpos);

$imgNew = $imgName . '_small' . $suffix;

imagejpeg($target_image, $imgNew, 100);

imagedestroy($source_image);

imagedestroy($target_image);

imagedestroy($cropped_image);

}

亲,试试微信扫码分享本页! *^_^*

相关文章:

  • php代码转换工具,推荐几款格式化工具以及代码转换工具
  • img标签 中的php图片大小,html中关于img标签以及改变图片尺寸的详解(图)
  • php csv 源码 下载,[PHP] 使用php生成下载csv文件
  • java 去掉 u00a0,UTF-8编码中的特殊空格之C2 A0 - NO-BREAK SPACE
  • java里函数式表达式,Java8 Lambda表达式 和 函数式编程 原理详解
  • 易语言与php加密AES,易语言调用openssl库实现aes cbc/cfb 256加密
  • linux安装php json拓展,Linux PHP增加JSON支持
  • php ajax实现图片预览,ajax怎么实现图片的预览上传以及查看缩略图
  • php ajax筛选,ajax商品筛选功能怎么进行判断
  • php curl_init 报错,如何解决curl_init php报错问题
  • php ?page,google chrome浏览器下载 PHP通用分页类pagephp[仿google分页]
  • php curl ssl错误,php curl常见错误:SSL错误、bool(false)
  • win2008系统php伪静态,ZBlog PHP在WIN2008 64位系统不能伪静态的解决方法
  • 常用PHP命令,PHP 常用命令行
  • python爬虫怎么自动下载图片,【图文详解】python爬虫实战——5分钟做个图片自动下载器...
  • JS 中的深拷贝与浅拷贝
  • @jsonView过滤属性
  • [iOS]Core Data浅析一 -- 启用Core Data
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • Mac转Windows的拯救指南
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • scrapy学习之路4(itemloder的使用)
  • tensorflow学习笔记3——MNIST应用篇
  • unity如何实现一个固定宽度的orthagraphic相机
  • Web设计流程优化:网页效果图设计新思路
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 讲清楚之javascript作用域
  • 利用jquery编写加法运算验证码
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 微信开源mars源码分析1—上层samples分析
  • 一道面试题引发的“血案”
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #laravel 通过手动安装依赖PHPExcel#
  • #控制台大学课堂点名问题_课堂随机点名
  • $forceUpdate()函数
  • (1)Android开发优化---------UI优化
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (C语言)二分查找 超详细
  • (八十八)VFL语言初步 - 实现布局
  • (定时器/计数器)中断系统(详解与使用)
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (利用IDEA+Maven)定制属于自己的jar包
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .net 按比例显示图片的缩略图
  • .NET 的程序集加载上下文
  • .netcore如何运行环境安装到Linux服务器
  • .net下的富文本编辑器FCKeditor的配置方法
  • .NET序列化 serializable,反序列化
  • .NET中使用Protobuffer 实现序列化和反序列化
  • .sdf和.msp文件读取
  • [17]JAVAEE-HTTP协议
  • [20190416]完善shared latch测试脚本2.txt