一键生成各种尺寸Icon的php脚本(实例)


Posted in PHP onFebruary 08, 2017

实例如下:

<?php 
/** 
* @name thumb  缩略图函数 
* @param  sting  $img_name  图片路径 
* @param  int   $max_width 略图最大宽度 
* @param  int   $max_height 略图最大高度 
* @param  sting  $suffix 略图后缀(如"img_x.jpg"代表小图,"img_m.jpg"代表中图,"img_l.jpg"代表大图) 
* @return  void 
*/ 
function thumb($img_name,$max_width,$max_height,$path,$new_name) 
{ 
  $img_infos = getimagesize($img_name); 
  $img_height = $img_infos[0];//图片高 
  $img_width = $img_infos[1]; //图片宽 
  $img_extension = '';    //图片后缀名 
 
  switch($img_infos[2]) 
  { 
    case 1: 
      $img_extension = 'gif'; 
      break; 
    case 2: 
      $img_extension = 'jpeg'; 
      break; 
    case 3: 
      $img_extension = 'png'; 
      break; 
    default: 
      $img_extension = 'jpeg'; 
      break; 
  } 
 
  $new_img_size = array(); 
  $new_img_size['width'] = $max_width; 
  $new_img_size['height'] = $max_height; 
   
  $img_func = '';   //函数名称 
  $img_handle = '';  //图片句柄 
  $thum_handle = ''; //略图图片句柄 
 
  switch($img_extension) 
  { 
    case 'jpg': 
      $img_handle = imagecreatefromjpeg($img_name); 
      $img_func = 'imagejpeg'; 
      break; 
    case 'jpeg': 
      $img_handle = imagecreatefromjpeg($img_name); 
      $img_func = 'imagejpeg'; 
      break; 
    case 'png': 
      $img_handle = imagecreatefrompng($img_name); 
      imagesavealpha($img_handle, true); 
      $img_func = 'imagepng'; 
      break; 
    case 'gif': 
      $img_handle = imagecreatefromgif($img_name); 
      $img_func = 'imagegif'; 
      break; 
    default: 
      $img_handle = imagecreatefromjpeg($img_name); 
      $img_func = 'imagejpeg'; 
      break; 
  } 
 
  $quality = 100;//图片质量 
  if($img_func == 'imagepng') 
  { 
    $quality = 9; 
  }  
 
  $thum_handle = imagecreatetruecolor($new_img_size['height'],$new_img_size['width']); 
  imagealphablending($thum_handle,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色; 
  imagesavealpha($thum_handle,true);//这里很重要,意思是不要丢了$thumb图像的透明色; 
 
  if(function_exists('imagecopyresampled')) 
  { 
    imagecopyresampled($thum_handle,$img_handle, 0, 0, 0, 0,$new_img_size['height'],$new_img_size['width'],$img_height,$img_width); 
  }  
  else  
  { 
    imagecopyresized($thum_handle,$img_handle, 0, 0, 0, 0,$new_img_size['height'],$new_img_size['width'],$img_height,$img_width); 
  } 
 
  call_user_func_array($img_func,array($thum_handle,$path.'/'.$new_name,$quality)); 
  imagedestroy($thum_handle);//清除句柄 
  imagedestroy($img_handle);//清除句柄 
} 
 
 
$IconArray = array  
( 
  "IOS8" => array  
  ( 
    array("width" => 180,  "height"=> 180, "path"=>"ios8",   "name"=>"Icon-180.png"), 
    array("width" => 120,  "height"=> 120, "path"=>"ios8",   "name"=>"Icon-120.png"), 
    array("width" => 152,  "height"=> 152, "path"=>"ios8",   "name"=>"Icon-152.png"), 
    array("width" => 76,   "height"=> 76,    "path"=>"ios8",   "name"=>"Icon-76.png"),  
    array("width" => 144,  "height"=> 144, "path"=>"ios8",   "name"=>"Icon-144.png"), 
    array("width" => 72,   "height"=> 72,    "path"=>"ios8",   "name"=>"Icon-72.png"), 
    array("width" => 114,  "height"=> 114, "path"=>"ios8",   "name"=>"Icon-114.png"), 
    array("width" => 57,   "height"=> 57,    "path"=>"ios8",   "name"=>"Icon-57.png"),  
    array("width" => 100,  "height"=> 100, "path"=>"ios8",   "name"=>"Icon-100.png"), 
    array("width" => 50,   "height"=> 50,    "path"=>"ios8",   "name"=>"Icon-50.png"),  
    array("width" => 80,   "height"=> 80,    "path"=>"ios8",   "name"=>"Icon-80.png"), 
    array("width" => 40,   "height"=> 40,    "path"=>"ios8",   "name"=>"Icon-40.png"), 
    array("width" => 58,   "height"=> 58,    "path"=>"ios8",   "name"=>"Icon-58.png"), 
    array("width" => 29,   "height"=> 29,    "path"=>"ios8",   "name"=>"Icon-29.png"), 
  ), 
  "IOS" => array  
  ( 
    array("width" => 180,  "height"=> 180, "path"=>"ios",    "name"=>"Icon-60@3x.png"), 
    array("width" => 120,  "height"=> 120, "path"=>"ios",    "name"=>"Icon-60@2x.png"), 
    array("width" => 152,  "height"=> 152, "path"=>"ios",    "name"=>"Icon-76@2x.png"), 
    array("width" => 76,   "height"=> 76,    "path"=>"ios",    "name"=>"Icon-76.png"),  
    array("width" => 144,  "height"=> 144, "path"=>"ios",    "name"=>"Icon-72@2x.png"), 
    array("width" => 72,   "height"=> 72,    "path"=>"ios",    "name"=>"Icon-72.png"), 
    array("width" => 114,  "height"=> 114, "path"=>"ios",    "name"=>"Icon-57@2x.png"), 
    array("width" => 57,   "height"=> 57,    "path"=>"ios",    "name"=>"Icon-57.png"),  
    array("width" => 100,  "height"=> 100, "path"=>"ios",    "name"=>"Icon-50@2x.png"), 
    array("width" => 50,   "height"=> 50,    "path"=>"ios",    "name"=>"Icon-50.png"),  
    array("width" => 80,   "height"=> 80,    "path"=>"ios",    "name"=>"Icon-40@2x.png"), 
    array("width" => 40,   "height"=> 40,    "path"=>"ios",    "name"=>"Icon-40.png"), 
    array("width" => 58,   "height"=> 58,    "path"=>"ios",    "name"=>"Icon-29@2x.png"), 
    array("width" => 29,   "height"=> 29,    "path"=>"ios",    "name"=>"Icon-29.png"), 
  ) 
  , 
  "ANDROID" => array 
  ( 
    array("width" => 144,  "height" => 144,   "path" => "android/drawable",      "name"=>"icon.png"), 
    array("width" => 144,  "height" => 144,   "path" => "android/drawable-xhdpi", "name"=>"icon.png"), 
    array("width" => 72,   "height" => 72, "path" => "android/drawable-hdpi",    "name"=>"icon.png"), 
    array("width" => 48,   "height" => 48, "path" => "android/drawable-mdpi",    "name"=>"icon.png"), 
    array("width" => 32,   "height" => 32, "path" => "android/drawable-ldpi",    "name"=>"icon.png") 
  ) 
); 
 
 
foreach ($IconArray["IOS8"] as $key => $IconType)  
{ 
  thumb("icon.png",$IconType['width'],$IconType['height'],$IconType['path'],$IconType['name']); 
} 
 
foreach ($IconArray["IOS"] as $key => $IconType)  
{ 
  thumb("icon.png",$IconType['width'],$IconType['height'],$IconType['path'],$IconType['name']); 
} 
 
foreach ($IconArray["ANDROID"] as $key => $IconType)  
{ 
  thumb("icon.png",$IconType['width'],$IconType['height'],$IconType['path'],$IconType['name']); 
} 
 
?>

以上这篇一键生成各种尺寸Icon的php脚本(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
图书管理程序(一)
Oct 09 PHP
Windows中安装Apache2和PHP4权威指南
Nov 18 PHP
php 常用类整理
Dec 23 PHP
php curl基本操作详解
Jul 23 PHP
php中的curl使用入门教程和常见用法实例
Apr 10 PHP
PHP判断远程图片是否存在的几种方法
May 04 PHP
php两种无限分类方法实例
Apr 21 PHP
PHP实现HTTP断点续传的方法
Jun 17 PHP
PHP 微信支付类 demo
Nov 30 PHP
PHP封装的XML简单操作类完整实例
Nov 13 PHP
php封装db类连接sqlite3数据库的方法实例
Dec 19 PHP
Laravel 模型使用软删除-左连接查询-表起别名示例
Oct 24 PHP
php正则提取html图片(img)src地址与任意属性的方法
Feb 08 #PHP
PHP正则匹配反斜杠'\'和美元'$'的方法
Feb 08 #PHP
php利用嵌套数组拼接与解析json的方法
Feb 07 #PHP
php获取文件名称和扩展名的方法
Feb 07 #PHP
php 常用的系统函数
Feb 07 #PHP
thinkphp修改配置进入默认首页的方法
Feb 07 #PHP
Yii2学习笔记之汉化yii设置表单的描述(属性标签attributeLabels)
Feb 07 #PHP
You might like
CI框架中zip类应用示例
2014/06/17 PHP
PHP购物车类Cart.class.php定义与用法示例
2016/07/20 PHP
yii2.0框架实现上传excel文件后导入到数据库的方法示例
2020/04/13 PHP
语义化 H1 标签
2008/01/14 Javascript
javascript Prototype 对象扩展
2009/05/15 Javascript
用js做一个小游戏平台 (一)
2009/12/29 Javascript
jQuery 中使用JSON的实现代码
2011/12/01 Javascript
JavaScript高级程序设计 阅读笔记(十八) js跨平台的事件
2012/08/14 Javascript
javaScript让文本框内的最后一个文字的后面获得焦点实现代码
2013/01/06 Javascript
js实现页面转发功能示例代码
2013/08/05 Javascript
jquery $.each 和for怎么跳出循环终止本次循环
2013/09/27 Javascript
JS+CSS实现弹出全屏灰黑色透明遮罩效果的方法
2014/12/20 Javascript
理解Javascript文件动态加载
2016/01/29 Javascript
全面了解JavaScript的数据类型转换
2016/07/01 Javascript
详解vue2路由vue-router配置(懒加载)
2017/04/08 Javascript
React利用插件和不用插件实现双向绑定的方法详解
2017/07/03 Javascript
Extjs 中的 Treepanel 实现菜单级联选中效果及实例代码
2017/08/22 Javascript
vue-cli axios请求方式及跨域处理问题
2018/03/28 Javascript
layer.open的自适应及居中及子页面标题的修改方法
2019/09/05 Javascript
JavaScript ES6 Class类实现原理详解
2020/05/08 Javascript
使用Vue Composition API写出清晰、可扩展的表单实现
2020/06/10 Javascript
解决antd的Form组件setFieldsValue的警告问题
2020/10/29 Javascript
[02:32]DOTA2完美大师赛场馆静安体育中心观赛全攻略
2017/11/08 DOTA
[43:43]完美世界DOTA2联赛PWL S2 FTD.C vs Rebirth 第一场 11.22
2020/11/24 DOTA
python中关于时间和日期函数的常用计算总结(time和datatime)
2013/03/08 Python
windows下安装Python和pip终极图文教程
2017/03/05 Python
python 性能优化方法小结
2017/03/31 Python
python使用Tesseract库识别验证
2018/03/21 Python
Python设计模式之适配器模式原理与用法详解
2019/01/15 Python
python从zip中删除指定后缀文件(推荐)
2019/12/05 Python
基于CSS3实现的黑色个性导航菜单效果
2015/09/14 HTML / CSS
2014年廉洁自律承诺书
2014/05/26 职场文书
2014年庆祝国庆65周年演讲稿
2014/09/21 职场文书
经理助理岗位职责
2015/02/02 职场文书
Java 超详细讲解ThreadLocal类的使用
2022/04/07 Java/Android
JavaScript圣杯布局与双飞翼布局实现案例详解
2022/08/05 Javascript