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 相关文章推荐
不用数据库的多用户文件自由上传投票系统(3)
Oct 09 PHP
在同一窗体中使用PHP来处理多个提交任务
May 08 PHP
PHP 时间日期操作实战
Aug 26 PHP
php ZipArchive压缩函数详解实例
Nov 06 PHP
PHP弹出提示框并跳转到新页面即重定向到新页面
Jan 24 PHP
PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
Apr 01 PHP
php验证手机号码
Nov 11 PHP
php 访问oracle 存储过程实例详解
Jan 08 PHP
详谈PHP中public,private,protected,abstract等关键字的用法
Dec 31 PHP
对于Laravel 5.5核心架构的深入理解
Feb 22 PHP
php传值和传引用的区别点总结
Nov 19 PHP
基于PHP+Mysql简单实现了图书购物车系统的实例详解
Aug 06 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格式化日期和时间格式化示例分享
2014/02/24 PHP
php生成随机颜色方法汇总
2014/12/03 PHP
php防止sql注入简单分析
2015/03/18 PHP
PHP附件下载中文名称乱码的解决方法
2015/12/17 PHP
PHP session会话操作技巧小结
2016/09/27 PHP
PHP链表操作简单示例
2016/10/15 PHP
PHP正则之正向预查与反向预查讲解与实例
2020/04/06 PHP
PHP获取php,mysql,apche的版本信息及更多服务器信息
2021/03/09 PHP
javascript事件模型代码
2007/07/01 Javascript
javascript 打印页面代码
2009/03/24 Javascript
cookie丢失问题(认证失效) Authentication (用户验证信息)也会丢失
2009/06/04 Javascript
JavaScript 高效运行代码分析
2010/03/18 Javascript
JavaScript实现常用二级省市级联下拉列表的方法
2015/03/25 Javascript
使用Function.apply()的参数数组化来提高 JavaScript程序性能的技巧
2015/12/23 Javascript
微信小程序 使用canvas制作K线实例详解
2017/01/12 Javascript
Javascript Event(事件)的传播与冒泡
2017/01/23 Javascript
详解Angular 4.x 动态创建组件
2017/04/25 Javascript
详解JavaScript中的六种错误类型
2017/09/21 Javascript
Angular ElementRef简介及其使用
2018/10/01 Javascript
详解Vue 项目中的几个实用组件(ts)
2019/10/29 Javascript
小程序富文本提取图片可放大缩小
2020/05/26 Javascript
[02:26]2016国际邀请赛8月3日开战 中国军团出征西雅图
2016/08/02 DOTA
在Python的setuptools框架下生成egg的教程
2015/04/13 Python
使用python实现mqtt的发布和订阅
2019/05/05 Python
Python中字符串与编码示例代码
2019/05/20 Python
Python学习笔记之迭代器和生成器用法实例详解
2019/08/08 Python
Django操作session 的方法
2020/03/09 Python
PyQt5连接MySQL及QMYSQL driver not loaded错误解决
2020/04/29 Python
PurCotton全棉时代官网:100%天然棉花生产的生活护理用品
2016/11/18 全球购物
Nike爱尔兰官方网站:Nike.com (IE)
2018/03/12 全球购物
有个性的自我评价范文
2013/11/15 职场文书
解除财产保全担保书
2014/05/20 职场文书
国际商务专业求职信
2014/07/15 职场文书
如何签定毕业生就业协议书
2014/09/28 职场文书
奖学金申请书(范文)
2019/08/14 职场文书
python tqdm用法及实例详解
2021/06/16 Python