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 相关文章推荐
在PHP中读取和写入WORD文档的代码
Apr 09 PHP
浅析PHP原理之变量(Variables inside PHP)
Aug 09 PHP
thinkphp的CURD和查询方式介绍
Dec 19 PHP
php上传图片存入数据库示例分享
Mar 11 PHP
PHP实现抓取HTTPS内容
Dec 01 PHP
php验证码的制作思路和实现方法
Nov 12 PHP
使用WordPress发送电子邮件的相关PHP函数用法解析
Dec 15 PHP
PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
Jan 07 PHP
PHP微信PC二维码登陆的实现思路
Jul 13 PHP
PHP面向对象五大原则之接口隔离原则(ISP)详解
Apr 04 PHP
PHP集成环境XAMPP的安装与配置
Nov 13 PHP
PHP常量define和const的区别详解
May 18 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
咖啡因含量是由谁决定的?低因咖啡怎么来?低因咖啡适合什么人喝
2021/03/06 新手入门
我的论坛源代码(八)
2006/10/09 PHP
php获取网页标题和内容函数(不包含html标签)
2014/02/03 PHP
简单的自定义php模板引擎
2016/08/26 PHP
Aster vs Newbee BO5 第二场2.19
2021/03/10 DOTA
求得div 下 img的src地址的js代码
2007/02/28 Javascript
javascript中巧用“闭包”实现程序的暂停执行功能
2007/04/04 Javascript
css配合jquery美化 select
2013/11/29 Javascript
JavaScript mixin实现多继承的方法详解
2017/03/30 Javascript
对于Javascript 执行上下文的全面了解
2017/09/05 Javascript
vue多种弹框的弹出形式的示例代码
2017/09/18 Javascript
快速处理vue渲染前的显示问题
2018/03/05 Javascript
JS中call和apply函数用法实例分析
2018/06/20 Javascript
vue-resource请求实现http登录拦截或者路由拦截的方法
2018/07/11 Javascript
layer.open 按钮的点击事件关闭方法
2018/08/17 Javascript
Element-UI中关于table表格的那些骚操作(小结)
2019/08/15 Javascript
jquery实现图片无缝滚动 蒙版遮蔽效果
2020/01/11 jQuery
详解搭建一个vue-cli的移动端H5开发模板
2020/01/17 Javascript
如何通过JS实现转码与解码
2020/02/21 Javascript
python统计一个文本中重复行数的方法
2014/11/19 Python
Python学习笔记之if语句的使用示例
2017/10/23 Python
python机器学习实战之最近邻kNN分类器
2017/12/20 Python
python判断文件是否存在,不存在就创建一个的实例
2019/02/18 Python
在python里协程使用同步锁Lock的实例
2019/02/19 Python
关于Kotlin中SAM转换的那些事
2020/09/15 Python
在css3中background-clip属性与background-origin属性的用法介绍
2012/11/13 HTML / CSS
CSS3 制作绽放的莲花采用效果叠加实现
2013/01/31 HTML / CSS
草莓巧克力:Shari’s Berries
2017/02/07 全球购物
加拿大最大的箱包及旅游配件零售商:Bentley Leathers
2017/07/19 全球购物
地球上最先进的胡子和头发修剪器:Bevel
2018/01/23 全球购物
工商企业管理应届生求职信
2014/05/04 职场文书
实验心得体会
2014/09/05 职场文书
老公保证书
2015/01/17 职场文书
警示教育片观后感
2015/06/17 职场文书
Dubbo+zookeeper搭配分布式服务的过程详解
2022/04/03 Java/Android
Win10系统搭建ftp文件服务器详细教程
2022/08/05 Servers