一键生成各种尺寸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 相关文章推荐
php实现的MySQL通用查询程序
Mar 11 PHP
php过滤危险html代码
Aug 18 PHP
php从右向左/从左向右截取字符串的实现方法
Nov 28 PHP
php数组中删除元素的实现代码
Jun 22 PHP
教你如何使用php session
Oct 28 PHP
php使用curl访问https示例分享
Jan 17 PHP
ThinkPHP中的系统常量和预定义常量集合
Jul 01 PHP
如何把php5.3版本升级到php5.4或者php5.5
Jul 31 PHP
PHP实现JS中escape与unescape的方法
Jul 11 PHP
深入理解PHP JSON数组与对象
Jul 19 PHP
PHP7.1新功能之Nullable Type用法分析
Sep 26 PHP
php中的异常和错误浅析
May 03 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
php基础知识:类与对象(2) 自动加载对象
2006/12/13 PHP
PHP+MYSQL 出现乱码的解决方法
2008/08/08 PHP
解析php中session的实现原理以及大网站应用应注意的问题
2013/06/17 PHP
php简单防盗链实现方法
2015/07/29 PHP
joomla实现注册用户添加新字段的方法
2016/05/05 PHP
Jquery post传递数组方法实现思路及代码
2013/04/28 Javascript
Query中click(),bind(),live(),delegate()的区别
2013/11/19 Javascript
JavaScript中对象property的读取和写入方法介绍
2014/12/30 Javascript
详解javascript函数的参数
2015/11/10 Javascript
JavaScript实现带播放列表的音乐播放器实例分享
2016/03/07 Javascript
Bootstrap学习笔记之css样式设计(2)
2016/06/07 Javascript
node+experss实现爬取电影天堂爬虫
2016/11/20 Javascript
基于Jquery Ajax type的4种类型(详解)
2017/08/02 jQuery
layui框架中layer父子页面交互的方法分析
2017/11/15 Javascript
微信小程序MUI导航栏透明渐变功能示例(通过改变opacity实现)
2019/01/24 Javascript
微信小程序如何播放腾讯视频的实现
2019/09/20 Javascript
使用Promise封装小程序wx.request的实现方法
2019/11/13 Javascript
谈谈node.js中的模块系统
2020/09/01 Javascript
JavaScript原生数组函数实例汇总
2020/10/14 Javascript
Python中input和raw_input的一点区别
2014/10/21 Python
使用PyInstaller将Python程序文件转换为可执行程序文件
2016/07/08 Python
Python Numpy 数组的初始化和基本操作
2018/03/13 Python
tensorflow实现简单的卷积神经网络
2018/05/24 Python
python实现对任意大小图片均匀切割的示例
2018/12/05 Python
Python如何向SQLServer存储二进制图片
2020/06/08 Python
Python 在局部变量域中执行代码
2020/08/07 Python
如何完美的建立一个python项目
2020/10/09 Python
从一次项目重构说起CSS3自定义变量在项目的使用方法
2021/03/01 HTML / CSS
Nice Kicks网上商店:ShopNiceKicks.com
2018/12/25 全球购物
心理健康教育制度
2014/01/27 职场文书
老总助理工作岗位职责
2014/02/06 职场文书
气象学专业个人求职信
2014/04/22 职场文书
关于责任的演讲稿
2014/05/20 职场文书
环保公益策划方案
2014/08/15 职场文书
2015年学生会干事工作总结
2015/04/09 职场文书
无罪辩护词范文
2015/05/21 职场文书