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 相关文章推荐
mysql5详细安装教程
Jan 15 PHP
解决php中Cannot send session cache limiter 的问题的方法
Apr 27 PHP
php删除指定目录的方法
Apr 03 PHP
php比较两个字符串长度的方法
Jul 13 PHP
HTML中嵌入PHP的简单方法
Feb 16 PHP
php获取flash尺寸详细数据的方法
Nov 12 PHP
php自定义函数br2nl实现将html中br换行符转换为文本输入中换行符的方法【与函数nl2br功能相反】
Feb 17 PHP
PHP实现批量清空删除指定文件夹所有内容的方法
May 30 PHP
PHP判断json格式是否正确的实现代码
Sep 20 PHP
Laravel用户授权系统的使用方法示例
Sep 16 PHP
Laravel Validator 实现两个或多个字段联合索引唯一
May 08 PHP
Laravel框架实现多个视图共享相同数据的方法详解
Jul 09 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判断远程url是否有效的几种方法小结
2011/10/08 PHP
PHP书写格式详解(必看)
2016/05/23 PHP
一个实用的php验证码类
2017/07/06 PHP
浅析PHP中的 inet_pton 网络函数
2019/12/16 PHP
菜鸟学习JavaScript小实验之函数引用
2010/11/17 Javascript
Jquery同辈元素选中/未选中效果的实例代码
2013/08/01 Javascript
javascipt匹配单行和多行注释的正则表达式
2013/11/20 Javascript
让jQuery Mobile不显示讨厌loading界面的方法
2014/02/19 Javascript
JQuery显示隐藏DIV的方法及代码实例
2015/04/16 Javascript
基于Jquery和html5的7款个性化地图插件
2015/11/17 Javascript
基于jQuery实现的双11天猫拆红包抽奖效果
2015/12/01 Javascript
JQuery validate插件验证用户注册信息
2016/05/11 Javascript
JQuery中attr属性和jQuery.data()学习笔记【必看】
2016/05/18 Javascript
JS操作input标签属性checkbox全选的实现代码
2017/03/02 Javascript
angular4 JavaScript内存溢出问题
2018/03/06 Javascript
Vue.set() this.$set()引发的视图更新思考及注意事项
2018/08/30 Javascript
本地文件上传到七牛云服务器示例(七牛云存储)
2014/01/11 Python
Python随机生成均匀分布在单位圆内的点代码示例
2017/11/13 Python
用Eclipse写python程序
2018/02/10 Python
Python爬虫工程师面试问题总结
2018/03/22 Python
python实现字符串和字典的转换
2018/09/29 Python
python函数定义和调用过程详解
2020/02/09 Python
python爬虫要用到的库总结
2020/07/28 Python
一款利用纯css3实现的360度翻转按钮的实例教程
2014/11/05 HTML / CSS
Qoo10马来西亚:全球时尚和引领潮流的购物市场
2016/08/25 全球购物
Clearly新西兰:购买眼镜、太阳镜和隐形眼镜
2018/04/26 全球购物
英国在线自行车店:Merlin Cycles
2018/08/20 全球购物
Eagle Eyes Optics鹰眼光学:高性能太阳镜
2018/12/07 全球购物
意大利香水和化妆品购物网站:Parfimo.it
2019/10/06 全球购物
编辑个人求职信范文
2013/09/21 职场文书
大学军训感言300字
2014/03/09 职场文书
软件项目实施计划书
2014/05/02 职场文书
营业用房租赁协议书
2014/11/26 职场文书
幼儿园庆元旦主持词
2015/07/06 职场文书
农贸批发市场管理制度
2015/08/07 职场文书
Python机器学习之基于Pytorch实现猫狗分类
2021/06/08 Python