当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP实现批量生成App各种尺寸Logo

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-17 11:03:25 浏览: 评论:0 

这篇文章主要介绍了PHP实现批量生成App各种尺寸Logo的方法和示例的核心代码,非常的简单实用,这里推荐给小伙伴们,有需要的可以参考下。

使用PHP GD,使用良好,一键剪裁各种尺寸,打包下载。经常换icon的懂的,美工给你一个1024的logo,你得ps出各种尺寸,于是有了这个东西。

核心代码:

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

以上就是本文所述的全部内容了,希望大家能够喜欢。

Tags: PHP批量生成App

分享到: