网络编程
位置:首页>> 网络编程>> 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
投稿

猜你喜欢

  • 写这篇文章的时候,还真不知道如何取名,也不知道这个该如何将其归类。这个是同事遇到的一个案例,案例比较复杂,这里抽丝剥茧,仅仅构造一个简单的案
  • 数据准备moduls.py# 构建表结构from django.db import models# 表app01_publishclass
  • 今天分享 3 个 Python 编程小技巧,来看看你是否用过?1、如何按照字典的值的大小进行排序我们知道,字典的本质是哈希表,本身是无法排序
  • 写在之前「装饰器」作为 Python 高级语言特性中的重要部分,是修改函数的一种超级便捷的方式,适当使用能够有效提高代码的可读性和可维护性,
  • swiper是我之前做前端页面会用到的一个插件,我自己认为是非常好用的。swiper提供了形式多种多样、适应各个终端的轮播图效果。本文是小编
  • 在设计主键的时候往往需要考虑以下几点: 1.无意义性:此处无意义是从用户的角度来定义的。这种无意义在一定程度上也会减少数据库的信息冗余。常常
  • 在python中进行两个整数相除的时候,在默认情况下都是只能够得到整数的值,而在需要进行对除所得的结果进行精确地求值时,想在运算后即得到浮点
  • -- SQL Server 2000 SELECT a.name AS 字段名, CASE WHEN EXISTS (SELECT 1 FR
  • 本文实例讲述了python获取mp3文件信息的方法。分享给大家供大家参考。具体如下:将代码生成.py文件放在目录下运行,可以获取该目录的所有
  • 1.11 – 添加缎带修饰网页局部模块中右上角的蓝色缎带修饰是这个网站界面设计中的一个亮点,只要合理的运用CSS、PNG透明图片和绝对定位属
  • eval() 和 exec() 函数都属于 Python 的内置函数,由于这两个函数在功能和用法方面都有相似之处,所以将它们放到一节进行介绍
  • 环境win10Python:3.6.7Django:2.2.7运行效果1、创建 Django 项目# 创建Download项目django-
  • 文件可以传输,但是对比传输前后的文件:socket_test.txt,末尾有一些不一致服务端代码:#!/usr/bin/python# -*
  • 1. 过程概述Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后虚拟机一条一条执行字节码指令,从而完成程序的执行。2.
  • 很多时候,我们执行页面上某个URL请求的时候,需要有等待的时间。如果是直接的页面跳转,浏览器会有缓冲进度展示,但是如果是AJAX,我觉得应该
  • 相关概念并发:指一个时间段内,有几个程序在同一个cpu上运行,但是任意时刻只有一个程序在cpu上运行。比如说在一秒内cpu切换了100个进程
  • 1、Case 子查询连接查询select * from score create database demo use demo create
  • 成天都要与样式打交道的朋友,相信对CSS选择符(CSS Selectors)都不会陌生。不过对于刚接触或者还不是很熟悉css的朋友来说,能够
  • 说明1、导入unittest模块。2、导入被测对象。3、创建测试类unittest.TestCase。4、重写setUp和tearDown(
  • python列表变量可以存储一个元素,而列表是一个大容器,可以存储N多个元素,程序可以方便的对这些数据进行整体操作(可以存储多个不同的数据类
手机版 网络编程 asp之家 www.aspxhome.com