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 相关文章推荐
php4的彩蛋
Oct 09 PHP
怎么样可以把 phpinfo()屏蔽掉?
Nov 24 PHP
PHP 读取和修改大文件的某行内容的代码
Oct 30 PHP
用php实现的下载css文件中的图片的代码
Feb 08 PHP
PHP用星号隐藏部份用户名、身份证、IP、手机号等实例
Apr 08 PHP
Codeigniter+PHPExcel实现导出数据到Excel文件
Jun 12 PHP
php中操作memcached缓存进行增删改查数据的实现代码
Aug 15 PHP
typecho插件编写教程(四):插件挂载
May 28 PHP
php 使用redis锁限制并发访问类示例
Nov 02 PHP
老生常谈php 正则中的i,m,s,x,e分别表示什么
Mar 02 PHP
利用PHP获取汉字首字母并且分组排序详解
Oct 22 PHP
php设计模式之享元模式分析【星际争霸游戏案例】
Mar 23 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
JS实现php的伪分页
2008/05/25 PHP
Can't create/write to file 'C:\WINDOWS\TEMP\...MYSQL报错解决方法
2011/06/30 PHP
php关联数组与索引数组及其显示方法
2018/03/12 PHP
浅析php如何实现爬取数据原理
2018/09/27 PHP
Google排名中的10个最著名的 JavaScript库
2010/04/27 Javascript
JavaScript动态调整TextArea高度的代码
2010/12/28 Javascript
js 中{},[]中括号,大括号使用详解
2011/05/12 Javascript
读jQuery之十一 添加事件核心方法
2011/07/31 Javascript
通过js简单实现将一个文本内容转译成加密文本
2013/10/22 Javascript
无闪烁更新网页内容JS实现
2013/12/19 Javascript
JS动态增加删除UL节点LI及相关内容示例
2014/05/21 Javascript
深入探讨javascript函数式编程
2015/10/11 Javascript
JQUERY表单暂存功能插件分享
2016/02/23 Javascript
javascript中apply/call和bind的使用
2017/02/15 Javascript
JS原生轮播图的简单实现(推荐)
2017/07/22 Javascript
JavaScript实现三级级联特效
2017/11/05 Javascript
详解Node 定时器
2018/02/26 Javascript
iview中Select 选择器多选校验方法
2018/03/15 Javascript
详解webpack模块化管理和打包工具
2018/04/21 Javascript
JavaScript数组方法的错误使用例子
2018/09/13 Javascript
浅谈vux之x-input使用以及源码解读
2018/11/04 Javascript
深入了解JavaScript 私有化
2019/05/30 Javascript
vue如何实现自定义底部菜单栏
2019/07/01 Javascript
react-intl实现React国际化多语言的方法
2020/09/27 Javascript
如何使用python爬取csdn博客访问量
2016/02/14 Python
python学习之matplotlib绘制散点图实例
2017/12/09 Python
查看django执行的sql语句及消耗时间的两种方法
2018/05/29 Python
python读取txt文件并取其某一列数据的示例
2019/02/19 Python
Python基于QQ邮箱实现SSL发送
2020/04/26 Python
Python多线程的退出控制实现
2020/08/10 Python
FOREO官方网站:LUNA露娜洁面仪
2016/11/28 全球购物
买卖正宗运动鞋:GOAT
2019/12/06 全球购物
司法建议书范文
2014/05/13 职场文书
志愿者爱心公益活动策划方案
2014/09/15 职场文书
2014标准社保办理委托书
2014/10/06 职场文书
springboot拦截器无法注入redisTemplate的解决方法
2021/06/27 Java/Android