≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

PHP 一定程度内缩放图像的简单示例

人气:656 时间:2018-09-17

这篇文章主要为大家详细介绍了PHP 一定程度内缩放图像的简单示例,具有一定的参考价值,可以用来参考一下。

这是比在网络上的例子做的更简洁的方式。我看到的每个例子,在涉及到输入时,好像都依赖于方向,水平的垂直的或者两者都有,这忽略了水平和垂直映射“长边”和“短边”,然后在最后重新映射变量到$h / $w,PHP 一定限度内缩放图像

/**
 * PHP 一定限度内缩放图像的简单示例
 *
 * @param 
 * @author 四海网 www.q1010.com
 **/
function scale_dimensions_within_limits($w,$h,$max_w,$max_h){
  // $w is the width of the current rectangle 
  // $h is the height of the current rectangle 
  // $max_w is the maximum width that an image can be sized 
  // $max_h is the maximum height that an image can be sized 
 
  // **** Here's where the magic is starts **** 
  // Switch the concept of horiz/vertical/square to long/short side 
  $short_side_len = ($w < $h ? $w : $h); 
  $long_side_len = ($w > $h ? $w : $h); 

  // Set a variable to the variable name of the output variable
  $ssvar = ($w > $h ? 'h':'w'); 
  $lsvar = ($w > $h ? 'w':'h'); 
  $maxLSvar = "max_".$lsvar; 
  $maxSSvar = "max_".$ssvar; 
 
  // Do the first pass on the long side
  $ratio = $$maxLSvar/$long_side_len; 
  $newSS = round($short_side_len * $ratio); 
  $newLS = round($long_side_len * $ratio); 
 
  // *** Note - the only coditional block!
  // If short side is still out of limit, limit the short side and adjust 
  if($newSS > $$maxSSvar){ 
    $ratio = $$maxSSvar/$newSS; 
    $newLS = round($ratio*$newLS); 
    $newSS = $$maxSSvar; 
  } 
 
  // **** Here's where the magic ends **** 
  // Re-couple the h/w (or w/h) with the long/shortside counterparts 
  // $$ means it's a variable variable (dynamic assignment) 
  $$ssvar = $newSS; 
  $$lsvar = $newLS;
 
  // Prep the return array 
  $dimensions['w'] = $w; // this is derived from either $ssvar or $lsvar 
  $dimensions['h'] = $h; return $dimensions; 
}


/***   来自四海网(www.q1010.com)   ***/

本文来自:http://www.q1010.com/173/135-0.html

注:关于PHP 一定程度内缩放图像的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:缩放图像

您可能感兴趣的文章

上一篇:php mysql数据库重复记录的删除方法
下一篇:PHP 字符串循环的实现方法
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。