免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2166 | 回复: 0
打印 上一主题 下一主题

PHP批量生成iOS App Logo各种尺寸 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-16 14:06 |只看该作者 |倒序浏览
使用PHP GD,使用良好,一键剪裁各种尺寸,打包下载。经常换icon的懂的,美术给你一个1024的logo,你得ps出各种尺寸,于是有了这个东西。

展示地址:http://www.atool.org/ios_logo.php

核心代码如下
[PHP]代码
  1. <?php
  2. //http://www.codepearl.com

  3. class image {
  4.      
  5.     /**
  6.      * source image
  7.      *
  8.      * @var string|array
  9.      */
  10.     private $source;
  11.      
  12.     /**
  13.      * temporay image
  14.      *
  15.      * @var file
  16.      */
  17.     private $image;
  18.     private $ext;
  19.     /**
  20.      * erros
  21.      *
  22.      * @var array
  23.      */
  24.     private $error;
  25.      
  26.     /**
  27.      * construct
  28.      *
  29.      * @param string|array $source
  30.      */
  31.     public function __construct($source = NULL) {
  32.         if($source != NULL) {
  33.             $this->source($source);
  34.         }
  35.     }
  36.      
  37.     /**
  38.      * set the source image
  39.      *
  40.      * @param string|array $source
  41.      */
  42.     public function source($source) {
  43.         if(!is_array($source)) {
  44.             $this->source["name"] = $source;
  45.             $this->source["tmp_name"] = $source;
  46.             $type = NULL;
  47.             $ext = strtolower(end(explode(".",$source)));
  48.             switch($ext) {
  49.                 case "jpg"  :
  50.                 case "jpeg" : $type = "image/jpeg"; break;
  51.                 case "gif"  : $type = "image/gif"; break;
  52.                 case "png"  : $type = "image/png"; break;
  53.             }
  54.             $this->source["type"] = $type;
  55.         } else {
  56.             $this->source = $source;
  57.         }
  58.         $this->destination = $this->source["name"];
  59.     }
  60.      
  61.     /**
  62.      * resize the image
  63.      *
  64.      * @param int $width
  65.      * @param int $height
  66.      */
  67.     public function resize($width = NULL,$height = NULL) {
  68.         if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {
  69.             list($source_width,$source_height) = getimagesize($this->source["tmp_name"]);
  70.             if(($width == NULL) && ($height != NULL)) {
  71.                 $width = ($source_width * $height) / $source_height;
  72.             }
  73.             if(($width != NULL) && ($height == NULL)) {
  74.                 $height = ($source_height * $width) / $source_width;
  75.             }
  76.             if(($width == NULL) && ($height == NULL)) {
  77.                 $width = $source_width;
  78.                 $height = $source_height;
  79.             }
  80.             switch($this->source["type"]) {
  81.                 case "image/jpeg" : $created = imagecreatefromjpeg($this->source["tmp_name"]); break;
  82.                 case "image/gif"  : $created = imagecreatefromgif($this->source["tmp_name"]);  break;
  83.                 case "image/png"  : $created = imagecreatefrompng($this->source["tmp_name"]);  break;
  84.             }
  85.             $this->image = imagecreatetruecolor($width,$height);
  86.             imagecopyresampled($this->image,$created,0,0,0,0,$width,$height,$source_width,$source_height);
  87.         }
  88.     }
  89.      
  90.     /**
  91.      * add watermark on image
  92.      *
  93.      * @param string $mark
  94.      * @param int $opac
  95.      * @param int $x_pos
  96.      * @param int $y_pos
  97.      */
  98.     public function watermark($mark,$opac,$x_pos,$y_pos) {
  99.         if(file_exists($mark) && ($this->image != "")) {
  100.             $ext = strtolower(end(explode(".",$mark)));
  101.             switch($ext) {
  102.                 case "jpg"  :
  103.                 case "jpeg" : $watermark = imagecreatefromjpeg($mark); break;
  104.                 case "gif"  : $watermark = imagecreatefromgif($mark);  break;
  105.                 case "png"  : $watermark = imagecreatefrompng($mark);  break;
  106.             }
  107.             list($watermark_width,$watermark_height) = getimagesize($mark);
  108.             $source_width = imagesx($this->image);
  109.             $source_height = imagesy($this->image);
  110.             if($x_pos == "top") $pos  = "t"; else $pos  = "b";
  111.             if($y_pos == "left") $pos .= "l"; else $pos .= "r";
  112.             $dest_x = 0;
  113.             $dest_y = 0;
  114.             switch($pos) {
  115.                 case "tr" : $dest_x = $source_width - $watermark_width; break;
  116.                 case "bl" : $dest_y = $source_height - $watermark_height; break;
  117.                 case "br" : $dest_x = $source_width - $watermark_width; $dest_y = $source_height - $watermark_height; break;
  118.             }
  119.             imagecopymerge($this->image,$watermark,$dest_x,$dest_y,0,0,$watermark_width,$watermark_height,$opac);
  120.         }
  121.     }
  122.      
  123.     /**
  124.      * crop the image
  125.      *
  126.      * @param int $x
  127.      * @param int $y
  128.      * @param int $width
  129.      * @param int $height
  130.      */
  131.     public function crop($x,$y,$width,$height) {
  132.         if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"]) && ($width > 10) && ($height > 10)) {
  133.             switch($this->source["type"]) {
  134.                 case "image/jpeg" : $created = imagecreatefromjpeg($this->source["tmp_name"]); break;
  135.                 case "image/gif"  : $created = imagecreatefromgif($this->source["tmp_name"]);  break;
  136.                 case "image/png"  : $created = imagecreatefrompng($this->source["tmp_name"]);  break;
  137.             }           
  138.             $this->image = imagecreatetruecolor($width,$height);
  139.             imagecopy($this->image,$created,0,0,$x,$y,$width,$height);
  140.         }
  141.     }
  142.      
  143.     /**
  144.      * create final image file
  145.      *
  146.      * @param string $destination
  147.      * @param int $quality
  148.      */
  149.     public function create($destination,$quality = 100) {
  150.         if($this->image != "") {
  151.             $extension = substr($destination,-3,3);
  152.             switch($extension) {
  153.                 case "gif" :  
  154.                     imagegif($this->image,$destination,$quality);
  155.                     break;
  156.                 case "png" :
  157.                     $quality = ceil($quality/10) - 1;
  158.                     imagepng($this->image,$destination,$quality);
  159.                     break;
  160.                 default    :
  161.                     imagejpeg($this->image,$destination,$quality);
  162.                     break;
  163.             }
  164.         }
  165.     }
  166.      
  167.     /**
  168.      * check if extension is valid
  169.      *
  170.      */
  171.     public function validate_extension() {
  172.         if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {
  173.             $exts = array("image/jpeg", "image/pjpeg", "image/png", "image/x-png");
  174.             $ext = $this->source["type"];
  175.             $valid = 0;
  176.             $this->ext = '.not_found';
  177.             if ($ext == $exts[0] || $ext == $exts[1]) {
  178.                 $valid = 1;
  179.                 $this->ext = '.jpg';
  180.             }
  181.             // if ($ext == $exts[2]) {
  182.             //  $valid = 1;
  183.             //  $this->ext = '.gif';
  184.             // }
  185.             if ($ext == $exts[2] || $ext == $exts[3]) {
  186.                 $valid = 1;
  187.                 $this->ext = '.png';
  188.             }
  189.             if($valid != 1) {
  190.                 $this->error .= "extension";
  191.             }
  192.         } else {
  193.             $this->error .= "source";
  194.         }
  195.     }
  196.      
  197.     /**
  198.      * check if the size is correct
  199.      *
  200.      * @param int $max
  201.      */
  202.     public function validate_size($max) {
  203.         if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {
  204.             $max = $max * 1024;
  205.             if($this->source["size"] >= $max) {
  206.                 $this->error .= "size";
  207.             }
  208.         } else {
  209.             $this->error .= "source";
  210.         }
  211.     }
  212.      
  213.     /**
  214.      * check if the dimension is correct
  215.      *
  216.      * @param int $limit_width
  217.      * @param int $limit_height
  218.      */
  219.     public function validate_dimension($limit_width,$limit_height) {
  220.         if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {
  221.             list($source_width,$source_height) = getimagesize($this->source["tmp_name"]);
  222.             if(($source_width > $limit_width) || ($source_height > $limit_height)) {
  223.                 $this->error .= "dimension";
  224.             }
  225.         } else {
  226.             $this->error .= "source";
  227.         }
  228.     }
  229.      
  230.     /**
  231.      * get the found errors
  232.      *
  233.      */
  234.     public function error() {
  235.         $error = array();
  236.         if(stristr($this->error,"source")) $error[] = "找不到上传文件";
  237.         if(stristr($this->error,"dimension")) $error[] = "上传图片尺寸太大";
  238.         if(stristr($this->error,"extension")) $error[] = "不符合要求的格式";
  239.         if(stristr($this->error,"size")) $error[] = "图片文件太大";
  240.         return $error;
  241.     }

  242.     public function error_string() {
  243.         $error = "";
  244.         if(stristr($this->error,"source")) $error .= "找不到上传文件 / ";
  245.         if(stristr($this->error,"dimension")) $error .= "上传图片尺寸太大 / ";
  246.         if(stristr($this->error,"extension")) $error .= "不符合要求的格式 / ";
  247.         if(stristr($this->error,"size")) $error .= "图片文件太大 / ";
  248.          
  249.         if(eregi(" / $", $error)) {
  250.             $error = substr($error, 0, -3);
  251.         }
  252.         return $error;
  253.     }

  254.     public function ext() {
  255.         return $this->ext;
  256.     }
  257. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP