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


Posted in PHP onMarch 19, 2015

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

核心代码

<?php

class image {

    /**

     * source image

     *

     * @var string|array

     */

    private $source;

    /**

     * temporay image

     *

     * @var file

     */

    private $image;

    private $ext;

    /**

     * erros

     *

     * @var array

     */

    private $error;

    /**

     * construct

     *

     * @param string|array $source

     */

    public function __construct($source = NULL) {

        if($source != NULL) {

            $this->source($source);

        }

    }

    /**

     * set the source image

     *

     * @param string|array $source

     */

    public function source($source) { 

        if(!is_array($source)) {

            $this->source["name"] = $source;

            $this->source["tmp_name"] = $source;

            $type = NULL;

            $ext = strtolower(end(explode(".",$source)));

            switch($ext) {

                case "jpg"  : 

                case "jpeg" : $type = "image/jpeg"; break;

                case "gif"  : $type = "image/gif"; break;

                case "png"  : $type = "image/png"; break;

            }

            $this->source["type"] = $type;

        } else {

            $this->source = $source;

        }

        $this->destination = $this->source["name"];

    }

    /**

     * resize the image

     *

     * @param int $width

     * @param int $height

     */

    public function resize($width = NULL,$height = NULL) {

        if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {

            list($source_width,$source_height) = getimagesize($this->source["tmp_name"]);

            if(($width == NULL) && ($height != NULL)) {

                $width = ($source_width * $height) / $source_height;

            }

            if(($width != NULL) && ($height == NULL)) {

                $height = ($source_height * $width) / $source_width;

            }

            if(($width == NULL) && ($height == NULL)) {

                $width = $source_width;

                $height = $source_height;

            }

            switch($this->source["type"]) {

                case "image/jpeg" : $created = imagecreatefromjpeg($this->source["tmp_name"]); break;

                case "image/gif"  : $created = imagecreatefromgif($this->source["tmp_name"]);  break;

                case "image/png"  : $created = imagecreatefrompng($this->source["tmp_name"]);  break;

            }

            $this->image = imagecreatetruecolor($width,$height);

            imagecopyresampled($this->image,$created,0,0,0,0,$width,$height,$source_width,$source_height);

        }

    }

    /**

     * add watermark on image

     *

     * @param string $mark

     * @param int $opac

     * @param int $x_pos

     * @param int $y_pos

     */

    public function watermark($mark,$opac,$x_pos,$y_pos) {

        if(file_exists($mark) && ($this->image != "")) {

            $ext = strtolower(end(explode(".",$mark)));

            switch($ext) {

                case "jpg"  : 

                case "jpeg" : $watermark = imagecreatefromjpeg($mark); break;

                case "gif"  : $watermark = imagecreatefromgif($mark);  break;

                case "png"  : $watermark = imagecreatefrompng($mark);  break;

            }

            list($watermark_width,$watermark_height) = getimagesize($mark);

            $source_width = imagesx($this->image);

            $source_height = imagesy($this->image);

            if($x_pos == "top") $pos  = "t"; else $pos  = "b";

            if($y_pos == "left") $pos .= "l"; else $pos .= "r";

            $dest_x = 0; 

            $dest_y = 0; 

            switch($pos) {

                case "tr" : $dest_x = $source_width - $watermark_width; break;

                case "bl" : $dest_y = $source_height - $watermark_height; break;

                case "br" : $dest_x = $source_width - $watermark_width; $dest_y = $source_height - $watermark_height; break;

            }

            imagecopymerge($this->image,$watermark,$dest_x,$dest_y,0,0,$watermark_width,$watermark_height,$opac);

        }

    }

    /**

     * crop the image

     *

     * @param int $x

     * @param int $y

     * @param int $width

     * @param int $height

     */

    public function crop($x,$y,$width,$height) {

        if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"]) && ($width > 10) && ($height > 10)) {

            switch($this->source["type"]) {

                case "image/jpeg" : $created = imagecreatefromjpeg($this->source["tmp_name"]); break;

                case "image/gif"  : $created = imagecreatefromgif($this->source["tmp_name"]);  break;

                case "image/png"  : $created = imagecreatefrompng($this->source["tmp_name"]);  break;

            }           

            $this->image = imagecreatetruecolor($width,$height);

            imagecopy($this->image,$created,0,0,$x,$y,$width,$height);

        }

    }

    /**

     * create final image file 

     *

     * @param string $destination

     * @param int $quality

     */

    public function create($destination,$quality = 100) {

        if($this->image != "") {

            $extension = substr($destination,-3,3);

            switch($extension) {

                case "gif" :  

                    imagegif($this->image,$destination,$quality); 

                    break;

                case "png" :

                    $quality = ceil($quality/10) - 1;

                    imagepng($this->image,$destination,$quality); 

                    break;

                default    : 

                    imagejpeg($this->image,$destination,$quality); 

                    break;

            }

        }

    }

    /**

     * check if extension is valid

     *

     */

    public function validate_extension() {

        if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {

            $exts = array("image/jpeg", "image/pjpeg", "image/png", "image/x-png");

            $ext = $this->source["type"];

            $valid = 0;

            $this->ext = '.not_found';

            if ($ext == $exts[0] || $ext == $exts[1]) {

                $valid = 1;

                $this->ext = '.jpg';

            }

            // if ($ext == $exts[2]) {

            //  $valid = 1;

            //  $this->ext = '.gif';

            // }

            if ($ext == $exts[2] || $ext == $exts[3]) {

                $valid = 1;

                $this->ext = '.png';

            }

            if($valid != 1) {

                $this->error .= "extension";

            }

        } else {

            $this->error .= "source";

        }

    }

    /**

     * check if the size is correct

     *

     * @param int $max

     */

    public function validate_size($max) {

        if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {

            $max = $max * 1024;

            if($this->source["size"] >= $max) {

                $this->error .= "size";

            }

        } else {

            $this->error .= "source";

        }

    }

    /**

     * check if the dimension is correct

     *

     * @param int $limit_width

     * @param int $limit_height

     */

    public function validate_dimension($limit_width,$limit_height) {

        if(isset($this->source["tmp_name"]) && file_exists($this->source["tmp_name"])) {

            list($source_width,$source_height) = getimagesize($this->source["tmp_name"]);

            if(($source_width > $limit_width) || ($source_height > $limit_height)) {

                $this->error .= "dimension";

            }

        } else {

            $this->error .= "source";

        }

    }

    /**

     * get the found errors

     *

     */

    public function error() {

        $error = array();

        if(stristr($this->error,"source")) $error[] = "找不到上传文件";

        if(stristr($this->error,"dimension")) $error[] = "上传图片尺寸太大";

        if(stristr($this->error,"extension")) $error[] = "不符合要求的格式";

        if(stristr($this->error,"size")) $error[] = "图片文件太大";

        return $error;

    }

    public function error_string() {

        $error = "";

        if(stristr($this->error,"source")) $error .= "找不到上传文件 / ";

        if(stristr($this->error,"dimension")) $error .= "上传图片尺寸太大 / ";

        if(stristr($this->error,"extension")) $error .= "不符合要求的格式 / ";

        if(stristr($this->error,"size")) $error .= "图片文件太大 / ";

        if(eregi(" / $", $error)) {

            $error = substr($error, 0, -3);

        }

        return $error;

    }

    public function ext() {

        return $this->ext;

    }

}

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

PHP 相关文章推荐
mysql总结之explain
Feb 27 PHP
仿Aspnetpager的一个PHP分页类代码 附源码下载
Oct 08 PHP
PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解
Jul 03 PHP
php5.5新数组函数array_column使用
Jul 08 PHP
php 启动时报错的简单解决方法
Jan 27 PHP
PHP获取当前页面URL函数实例
Oct 22 PHP
php 使用array函数实现分页
Feb 13 PHP
php实现递归与无限分类的方法
Feb 16 PHP
PHP运行模式汇总
Nov 06 PHP
php数据结构之顺序链表与链式线性表示例
Jan 22 PHP
微信公众平台开发教程②微信端分享功能图文详解
Apr 10 PHP
JS操作XML中DTD介绍及使用方法分析
Jul 04 PHP
PHP日期函数date格式化UNIX时间的方法
Mar 19 #PHP
PHP使用GETDATE获取当前日期时间作为一个关联数组的方法
Mar 19 #PHP
php使用gettimeofday函数返回当前时间并存放在关联数组里
Mar 19 #PHP
PHP使用gmdate实现将一个UNIX 时间格式化成GMT文本的方法
Mar 19 #PHP
php查询mysql数据库并将结果保存到数组的方法
Mar 18 #PHP
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
Mar 18 #PHP
PHP使用mysql_fetch_row查询获得数据行列表的方法
Mar 18 #PHP
You might like
PHP XML操作类DOMDocument
2009/12/16 PHP
php 上一篇,下一篇文章实现代码与原理说明
2010/05/09 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
PHP开发api接口安全验证操作实例详解
2020/03/26 PHP
在Javascript中为String对象添加trim,ltrim,rtrim方法
2006/09/22 Javascript
JS 分号引起的一段调试问题
2009/06/18 Javascript
javascript appendChild,innerHTML,join性能比较代码
2009/08/29 Javascript
js监听表单value的修改同步问题,跨浏览器支持
2009/12/31 Javascript
JS实现QQ图片一闪一闪的效果小例子
2013/07/31 Javascript
Javascript自定义函数判断网站访问类型是PC还是移动终端
2014/01/10 Javascript
js字符串完全替换函数分享
2014/12/03 Javascript
jquery中ajax使用error调试错误的方法
2015/02/08 Javascript
关于JavaScript中事件绑定的方法总结
2016/10/26 Javascript
功能强大的jquery.validate表单验证插件
2016/11/07 Javascript
jquery实现转盘抽奖功能
2017/01/06 Javascript
JSONP跨域请求
2017/03/02 Javascript
vue-swiper的使用教程
2018/08/30 Javascript
详解在vue-cli项目下简单使用mockjs模拟数据
2018/10/19 Javascript
Vue2.x通用条件搜索组件的封装及应用详解
2019/05/28 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
原生JS实现弹幕效果的简单操作指南
2020/11/10 Javascript
Python去除、替换字符串空格的处理方法
2018/04/01 Python
Python统计单词出现的次数
2018/04/04 Python
Python中一些不为人知的基础技巧总结
2018/05/19 Python
pytorch方法测试——激活函数(ReLU)详解
2020/01/15 Python
python文件读取失败怎么处理
2020/06/23 Python
美国大城市最热门旅游景点门票:CityPASS
2016/12/16 全球购物
德国baby-markt婴儿用品瑞士网站:baby-markt.ch
2017/06/09 全球购物
北京银河万佳Java面试题
2012/03/21 面试题
车间统计员岗位职责
2014/01/05 职场文书
火车的故事教学反思
2014/02/11 职场文书
医药类个人求职的自我评价
2014/02/12 职场文书
歌颂祖国演讲稿
2014/05/04 职场文书
大专学生求职自荐信
2014/07/06 职场文书
机关党员四风问题个人整改措施
2014/10/26 职场文书
2015年环卫工作总结
2015/04/28 职场文书