PHP PHP图像上载创建缩略图类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP PHP图像上载创建缩略图类相关的知识,希望对你有一定的参考价值。
<?php
/**
* A class for uploading images and creating thumbnails
*
* @author Leonel Santos
* @email leonelsantosnet@gmail.com
* @copyright Leonel Santos 2010
* @version 1.1
*/
class img {
protected $_tempName;
protected $_tempTmp;
protected $_tempType;
protected $_filesize;
protected $_errors;
protected $_extension;
protected $_fileTmp;
protected $_filename;
protected $_width = 600;
protected $_height = 450;
protected $_moved = false;
protected $_cropToSquare = false;
// default: same folder where the script file is at
protected $_uploadFolder;
// default: 1M
protected $_limitSize = 1000000;
public function __construct($field){
$this->_tempTmp = $_FILES[$field]['tmp_name'];
$this->_tempType = $_FILES[$field]['type'];
$this->_filesize = $_FILES[$field]['size'];
$this->_tempName = $_FILES[$field]['name'];
$this->_extension = '.'.strtolower(end(explode('.', $this->_tempName)));
$this->_filename = substr($this->_tempName, 0, -strlen($this->_extension));
if($this->_tempType != "image/jpeg"){
$this->_errors = 'You can only upload JPG images. Try again.';
throw new Exception('You can only upload JPG images. Try again.');
exit();
}
if($this->_filesize>=$this->_limitSize){
throw new Exception('The file size of the file you are trying to upload is over limit. Your file size = '.$this->_filesize.' bytes. File size limit = '.$this->_limitSize.' bytes. Try again.');
exit();
}
}
public function setUploadFolder($location){
$this->_uploadFolder = $location;
}
public function setLimitSize($bytes){
if(!is_int($bytes) and $bytes < 0 and $bytes > 20000000){
$this->_errors = 'The size limit must be bigger than 0 and less than 20000000';
}else{
$this->_limitSize = $bytes;
}
}
public function setFilename($filename){
$this->_filename = $filename;
}
public function cropToSquare(){
$this->_cropToSquare = true;
}
public function setDimensions($width, $height){
$this->_width = $width;
$this->_height = $height;
}
public function thumb($thumbWidth=80, $thumbHeight=80){
if($this->_moved==false){
throw new Exception('You have to use the upload() function before you use the thumb() function');
exit();
}
$image_data = imagecreatefromjpeg($this->_fileTmp);
$image = $this->_uploadFolder . $this->_filename . $this->_extension;
$w = $thumbWidth;
$h = $thumbHeight;
$size = getimagesize($image);
$original_width = $size[0];
$original_height = $size[1];
$imageRatio=$original_width/$original_height; // calculate the image ratio
if($this->_cropToSquare==false){
if($imageRatio>1){// landscape
$h = $w/$imageRatio;
}else{// portrait
$w = $w*$imageRatio;
}
$x_offset = 0;
$y_offset = 0;
$src_w = $original_width;
$src_h = $original_height;
}else{
if($original_width > $original_height){//landscape
$x_offset = ($original_width - $original_height) / 2;
$y_offset = 0;
$src_w = $original_width - ($x_offset * 2);
$src_h = $src_w;
$w=$thumbWidth;
$h=$w;
}else{ // portrait and squre
$x_offset = 0;
$y_offset = ($original_height - $original_width) / 2;
$src_w = $original_height - ($y_offset * 2);
$src_h = $src_w;
$h=$thumbHeight;
$w=$h;
}
}
$canvas = imagecreatetruecolor($w, $h);
imagecopyresampled($canvas, $image_data, 0, 0, $x_offset, $y_offset, $w, $h, $src_w, $src_h);
imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . '_thumb' . $this->_extension), 100);
imagedestroy($canvas);
imagedestroy($image_data);
}
public function upload(){
$this->_fileTmp = $this->_uploadFolder . $this->_filename . $this->_extension;
if(move_uploaded_file($this->_tempTmp, $this->_fileTmp)){
$this->_moved = true;
$image_data = imagecreatefromjpeg($this->_fileTmp);
$image = $this->_uploadFolder . $this->_filename . $this->_extension;
$w = $this->_width;
$h = $this->_height;
$size = getimagesize($image);
$original_width = $size[0];
$original_height = $size[1];
$imageRatio=$original_width/$original_height; // calculate the image ratio
if($this->_cropToSquare==false){
if($imageRatio>1){
$xWidth = $w; //landscape
$h = $xWidth/$imageRatio;
}else{ // portrait
$xWidth = $h;
$w = $xWidth*$imageRatio;
}
$x_offset = 0;
$y_offset = 0;
$src_w = $original_width;
$src_h = $original_height;
}else{
if($original_width > $original_height){//landscape
$x_offset = ($original_width - $original_height) / 2;
$y_offset = 0;
$src_w = $original_width - ($x_offset * 2);
$src_h = $src_w;
$w=$this->_width;
$h=$w;
}else{ // portrait and squre
$x_offset = 0;
$y_offset = ($original_height - $original_width) / 2;
$src_w = $original_height - ($y_offset * 2);
$src_h = $src_w;
$h=$this->_height;
$w=$h;
}
}
$canvas = imagecreatetruecolor($w, $h);
if(!imagecopyresampled($canvas, $image_data, 0, 0, $x_offset, $y_offset, $w, $h, $src_w, $src_h)){
exit('imagecopyresampled error');
}
if(!imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . $this->_extension), 100)){
exit('imagejpeg error');
}
if(!imagedestroy($canvas)){
exit('imagedestroy error');
}
if(!imagedestroy($image_data)){
exit('imagedestroy error');
}
} else {
$this->_errors = 'Could not upload image';
}
}
public function getErrors(){
return $this->_errors;
}
}
?>
以上是关于PHP PHP图像上载创建缩略图类的主要内容,如果未能解决你的问题,请参考以下文章