Use PHP GD, use good, one key to cut various sizes, pack and download. Always change the icon to understand, art to give you a 1024 logo, you have to PS a variety of sizes, so have this thing.
Core code
Copy Code code 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,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 [] = "Cannot find upload file";
if (Stristr ($this->error, "Dimension")) $error [] = "Upload picture size too large";
if (Stristr ($this->error, "extension")) $error [] = "not conforming to the required 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. = "No upload file found/";
if (Stristr ($this->error, "Dimension")) $error. = "Upload picture size too large/";
if (Stristr ($this->error, "extension")) $error. = "Not conforming to the required 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 full content of this article, I hope you can enjoy.