热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

这篇文章主要介绍了PHP实现批量生成App各种尺寸Logo的方法和示例的核心代码,非常的简单实用,这里推荐给小伙伴们,有需要的可以参考下。
使用PHP GD,使用良好,一键剪裁各种尺寸,打包下载。经常换icon的懂的,美工给你一个1024的logo,你得ps出各种尺寸,于是有了这个东西。

核心代码

代码如下:


<&#63;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;
}
}

以上就是本文所述的全部内容了,希望大家能够喜欢。

推荐阅读
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • Echarts图表重复加载、axis重复多次请求问题解决记录
    文章目录1.需求描述2.问题描述正常状态:问题状态:3.解决方法1.需求描述使用Echats实现了一个中国地图:通过选择查询周期&#x ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Python字典推导式及循环列表生成字典方法
    本文介绍了Python中使用字典推导式和循环列表生成字典的方法,包括通过循环列表生成相应的字典,并给出了执行结果。详细讲解了代码实现过程。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
author-avatar
用户hxjr5k4y3f
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有