≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

php 一个非常完整的图片上传类示例

人气:716 时间:2018-09-20

这篇文章主要为大家详细介绍了php 一个非常完整的图片上传类示例,具有一定的参考价值,可以用来参考一下。

这个php图片上传类功能非常完善,完全可以满足各种图片上传需求,感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
经测试代码如下:

<?php
/**
 * 图片上传类
 *
 * @param 
 * @arrange (512.笔记) www.q1010.com
 **/ 
class mk_imageUpload{
 
var $max_size;
var $allowed_types;
var $thumb_height;
var $dest_dir;
var $extensions;
var $max_height;
var $max_main_height;
 
var $last_ext;
var $last_pid;
var $last_uid;
 
var $image_file;
var $image_field;
 
function __construct( $maxHeightMain, $maxHeightThumb, $destDir ){
$this->max_size = (1024/2)*1000; // 750kb
$this->allowed_types = array( 'jpeg', 'jpg', 'pjpeg', 'gif', 'png' );
$this->extensions = array(
'image/jpeg' => '.jpg',
'image/gif' => '.gif',
'image/png' => '.png',
'image/x-png' => '.png',
'image/pjpeg' => '.jpg'
);
$this->dest_dir = $destDir;
$this->max_height = $maxHeightThumb;
$this->max_main_height = $maxHeightMain;
}
 
function putImage( $formname, $newName ){
$this->image_field = $formname;
if( $this->getImage() ){
 
// Check for errors
if ( !$this->checkType() )
return $this->throwError(2);
 
if ( !$this->checkFileSize() )
return $this->throwError(1);
 
 
// Get Image
$img = $this->image_file;
 
// Check seize
if ( !$this->checkImageSize() )
return $this->throwError(3);
 
// Get image dimensinos
$size = $this->getImageSize();
$size['width'] = $size[0];
$size['height'] = $size[1];
$ratio = $this->max_height/$size['height'];
 
$maxheight = $this->max_height;
$maxwidth = $size['width'] * $ratio;
 
// Create Thumbnail
$s_t = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'s' );
 
if( $size['height'] > $this->max_main_height ){
$ratio = $this->max_main_height/$size['height'];
$maxheight = $this->max_main_height;
$maxwidth = $size['width'] * $ratio;
}else{
$maxheight = $size['height'];
$maxwidth = $size['width'];
}
 
// Create Large rescaled
$s_l = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'l' );
 
// remove temporary file
unlink($img['tmp_name']);
 
if( $s_t && $s_l ){
$nm = split('_',$newName);
$this->last_ext = $this->extensions[$size['mime']];
$this->last_pid = $nm[0];
$this->last_uid = $nm[1];
return 'ok';
}else{
return $this->throwError( 4 );
}
}else{
return $this->throwError( 2 );
}
}
 
function resizeImage($size,$img,$maxwidth,$maxheight,$newName,$ext){
// Create a thumbnail
if($size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){
$new_img = imagecreatefromjpeg($img['tmp_name']);
}elseif($size['mime'] == "image/x-png" || $size['mime'] == "image/png"){
$new_img = imagecreatefrompng($img['tmp_name']);
}elseif($size['mime'] == "image/gif"){
$new_img = imagecreatefromgif($img['tmp_name']);
}
 
if (function_exists('imagecreatetruecolor')){
$resized_img = imagecreatetruecolor($maxwidth,$maxheight);
}else{
return("Error: Please make sure your server has GD library ver 2+");
}
 
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $maxwidth, $maxheight, $size['width'], $size['height']);
if($size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){
$success = ImageJpeg ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]);
}elseif($size['mime'] == "image/x-png" || $size['mime'] == "image/png"){
$success = ImagePNG ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]);
}elseif($size['mime'] == "image/gif"){
$success = ImageGIF ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]);
}
 
// Remove temporary images
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
 
return $success;
}
 
function getImage(){
if( isset($_FILES[$this->image_field]) && is_uploaded_file($_FILES[$this->image_field]['tmp_name']) ){
$this->image_file = $_FILES[$this->image_field];
return true;
}
return false;
}
 
function returnImg(){
return $this->image_file;
}
 
function getImageSize(){
$img = $this->returnImg();
 
return getimagesize($img['tmp_name']);
}
 
function checkImageSize(){
$size = $this->getImageSize();
 
if( $size[1] < $this->max_height )
return false;
return true;
}
 
function checkFileSize(){
$img = $this->returnImg();
 
if( $img['size'] > $this->max_size )
return false;
return true;
}
 
function checkType(){
$img = $this->returnImg();
 
$type = split('/',$img['type']);
if( !in_array( $type[1], $this->allowed_types ) )
return false;
return true;
}
 
function throwError($val){
switch($val){
case 1: return 'Error: File size is too large';
break;
case 2: return 'Error: Improper file format';
break;
case 3: return 'Error: Your image is too small';
break;
case 4: return 'Error: There was an error creating the images';
break;
}
}
 
}

/*** 来自:四海网(www.q1010.com) ***/ 
?>

本文来自:http://www.q1010.com/173/513-0.html

注:关于php 一个非常完整的图片上传类示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:图片上传

您可能感兴趣的文章

  • PHP 实现图片上传并生成缩略图示例
  • PHP 实现图片上传并添加水印的方法
  • PHP 实现图片上传的代码(带返回值)
上一篇:php 检测是否移动设备的浏览器示例
下一篇:php 在当前页验证表单数据的简单示例
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。