网络编程
位置:首页>> 网络编程>> php编程>> PHP实现批量生成App各种尺寸Logo

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

作者:hebedich  发布时间:2023-07-23 03:59:59 

标签:PHP,生成,App,Logo

使用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;
    }
}

0
投稿

猜你喜欢

  • 最近网上再度兴起了CSS布局和Table 布局的争论。我最初颇有些不以为然:我原以为CSS 布局的意义早已深入人心,却没想到还有这么多设计师
  • 网上有这样一道题目:一个字符串String=“adadfdfseffserfefsefseetsdg”,找出里面出现次数最多的字母和出现的次
  • 毫无疑问,Google是当今世界上最成功的互联网公司之一,但是Google也曾推出过一些失败的实验品。还记得Google Accelerat
  • <P><HTML><HEAD><TITLE>javascriptboy</TITLE&
  • 在HTML中,我们设置border=”1″ 时,表格边框实际大小是2px,那如果我们要做成1px的细线表格要怎么办?以前在做1px的表格的时
  • MySQL 5.0.16的乱码问题可以用下面的方法解决:1.设置phpMyAdminLanguage:Chinese simplified
  • 1 运行SQLPLUS工具 sqlplus 2 以OS的默认身份连接 / as sysdba 3 显示当前用户名 show user 4 直
  • 年前在重写淘宝旺铺里的会员卡脚本的时候,无意中发现了一个有趣的事情。代码类似:var associative_array = new Arr
  • 什么是Dynamic HTML 今天我们以问答的形式来讲述什麽是Dynamic Html。问:亲爱的网猴,我经常看到讲述有关“Dynamic
  • 以下的文章主要是介绍SQL Server数据库与其实际应用元数据,我前两天在相关网站看见SQL Server数据库与其实际应用元数据的资料,
  • 先写一个批处理文件,给个例子。  代码如下:set rq=%date:~0,10% exp system/system的
  • 错误号     错误信息5     &n
  • GetRepeatTimes(TheChar,TheString) 得到一个字符串在另一个字符串当中出现几次的函数(新)如:response
  • 1.彻底弄懂CSS盒子模式一(DIV布局快速入门)3.彻底弄懂CSS盒子模式三(浮动的表演和清除的自述) 4.彻底弄懂CSS盒子模式四(绝对
  • 下面这段代码能够显示,当前用户所能够看到的所有的用户和表有兴趣的, 可以把每个表的内容加上<% Dim objOraSess
  • 我用的数据库是Access2000的,系统为Win2000 Advance Server.今天在程序调试中遇到了以下几个怪现象:1.如果Ac
  • asp无组件上传VBS编写的大家见的多了,这个是纯javascript实现的上传,原来unicode可以解决读取位置的问题,这次真的是纯JS
  • 其实这个话题已经在侧面写了好几篇深刻反思,用我自己几年工作实践的体会来看,性格决定了将来的发展。某些特质虽然可以掩饰,但在这之上必然不可能有
  • 我一般看书喜欢做笔记,这份笔记不知道是什么时候看的什么书做的,也忘了是否是摘自其他地方,总之一份汇总,应该适合初学者,对于Javascrip
  • 在网络上看到的数字人整合动网论坛的方法都非常不全,站长们都是抄人家的,也不说明可不可用,提供下载的文件也不能下载.现在我提供一些信息。一、整
手机版 网络编程 asp之家 www.aspxhome.com