PHP enables batch generation of various App Logo sizes

Source: Internet
Author: User
This article mainly introduces the PHP core code to generate various Logo sizes for apps in batches and examples. it is very simple and practical. we recommend this article to our friends. For more information, see. Use php gd, use well, one-click tailoring of various sizes, package download. I often understand the icon. the artist gives you a 1024 logo. you have to ps out of various sizes, so you have this thing.

Core code

The code is as follows:


<? 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, $ 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, $ 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 [] = "Upload file not found ";
If (stristr ($ this-> error, "dimension") $ error [] = "the size of the uploaded image is too large ";
If (stristr ($ this-> error, "extension") $ error [] = "Invalid Format ";
If (stristr ($ this-> error, "size") $ error [] = "the image file is too large ";
Return $ error;
}
Public function error_string (){
$ Error = "";
If (stristr ($ this-> error, "source") $ error. = "the uploaded file cannot be found /";
If (stristr ($ this-> error, "dimension") $ error. = "the size of the uploaded image is too large /";
If (stristr ($ this-> error, "extension") $ error. = "Invalid Format /";
If (stristr ($ this-> error, "size") $ error. = "the image file is too large /";
If (eregi ("/$", $ error )){
$ Error = substr ($ error, 0,-3 );
}
Return $ error;
}
Public function ext (){
Return $ this-> ext;
}
}

The above is all the content described in this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.