PHP implementation Batch Build app various sizes logo_php Tutorials

Source: Internet
Author: User
Tags explode

PHP implementation of batch-generated app size logo


This article mainly introduces the PHP implementation of the bulk of the application of the various sizes of the logo and the core code of the example, very simple and practical, here are recommended to small partners, there is a need to refer to.

Use PHP GD, good use, one-click tailoring various sizes, packaged download. Often change the icon to understand, art to give you a 1024 logo, you have to PS out of various sizes, so with this thing.

Core code

The code is as follows:


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 > && ($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 [] = "Non-conforming format";
if (Stristr ($this->error, "size")) $error [] = "picture file is too large";
return $error;
}
Public Function error_string () {
$error = "";
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. = "non-conforming format/";
if (Stristr ($this->error, "size")) $error. = "Picture file too large/";
if (Eregi ("/$", $error)) {
$error = substr ($error, 0,-3);
}
return $error;
}
Public Function ext () {
return $this->ext;
}
}

The above is the whole content of this article, I hope you can enjoy.

http://www.bkjia.com/PHPjc/970749.html www.bkjia.com true http://www.bkjia.com/PHPjc/970749.html techarticle PHP Implementation of the bulk of the application of the size of the logo this article mainly introduces the PHP implementation of the bulk of the application of various dimensions of the Logo method and sample core code, very simple and practical, recommended here ...

  • Related Article

    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.