php文件上传扩展,php文件上传类upload_class的扩展类及示例

本文介绍下,一个用于上传图片的php类,是基于之前的upload_class类的一个扩展。有需要的朋友,可以参考下。

分享一个上传图片文件的php扩展类,基于前文:php文件上传类与实例的一个扩展。

主要用于上传图片文件。

代码:

upload_dir.$this->file_copy;

$this->check_dir($this->thumb_folder); // run these checks to create not existing directories

$this->check_dir($this->foto_folder); // the upload dir is created during the file upload (if not already exists)

$thumb = $this->thumb_folder.$this->file_copy;

$foto = $this->foto_folder.$this->file_copy;

if ($landscape_only) {

$this->get_img_size($filename);

if ($this->y_size > $this->x_size) {

$this->img_rotate($filename, $compression);

}

}

$this->check_dimensions($filename); // check which size is longer then the max value

if ($this->larger_curr_value > $this->larger_dim_value) {

$this->thumbs($filename, $foto, $this->larger_dim_value, $compression);

} else {

copy($filename, $foto);

}

if ($create_thumb) {

if ($this->larger_curr_value > $this->larger_dim_thumb_value) {

$this->thumbs($filename, $thumb, $this->larger_dim_thumb_value, $compression); // finally resize the image

} else {

copy($filename, $thumb);

}

}

if ($delete_tmp_file) $this->del_temp_file($filename); // note if you delete the tmp file the check if a file exists will not work

}

function get_img_size($file) {

$img_size = getimagesize($file);

$this->x_size = $img_size[0];

$this->y_size = $img_size[1];

}

function check_dimensions($filename) {

$this->get_img_size($filename);

$x_check = $this->x_size - $this->x_max_size;

$y_check = $this->y_size - $this->y_max_size;

if ($x_check < $y_check) {

$this->larger_dim = "y";

$this->larger_curr_value = $this->y_size;

$this->larger_dim_value = $this->y_max_size;

$this->larger_dim_thumb_value = $this->y_max_thumb_size;

} else {

$this->larger_dim = "x";

$this->larger_curr_value = $this->x_size;

$this->larger_dim_value = $this->x_max_size;

$this->larger_dim_thumb_value = $this->x_max_thumb_size;

}

}

function img_rotate($wr_file, $comp) {

$new_x = $this->y_size;

$new_y = $this->x_size;

if ($this->use_image_magick) {

exec(sprintf("mogrify -rotate 90 -quality %d %s", $comp, $wr_file));

} else {

$src_img = imagecreatefromjpeg($wr_file);

$rot_img = imagerotate($src_img, 90, 0);

$new_img = imagecreatetruecolor($new_x, $new_y);

imageantialias($new_img, TRUE);

imagecopyresampled($new_img, $rot_img, 0, 0, 0, 0, $new_x, $new_y, $new_x, $new_y);

imagejpeg($new_img, $this->upload_dir.$this->file_copy, $comp);

}

}

function thumbs($file_name_src, $file_name_dest, $target_size, $quality = 80) {

//print_r(func_get_args());

$size = getimagesize($file_name_src);

if ($this->larger_dim == "x") {

$w = number_format($target_size, 0, ',', '');

$h = number_format(($size[1]/$size[0])*$target_size,0,',','');

} else {

$h = number_format($target_size, 0, ',', '');

$w = number_format(($size[0]/$size[1])*$target_size,0,',','');

}

if ($this->use_image_magick) {

exec(sprintf("convert %s -resize %dx%d -quality %d %s", $file_name_src, $w, $h, $quality, $file_name_dest));

} else {

$dest = imagecreatetruecolor($w, $h);

imageantialias($dest, TRUE);

$src = imagecreatefromjpeg($file_name_src);

imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

imagejpeg($dest, $file_name_dest, $quality);

}

}

}

$max_size = 1024*1024; // the max. size for uploading (~1MB)

define("MAX_SIZE", $max_size);

$foto_upload = new Foto_upload;

$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/files/"; // "files" is the folder for the uploaded files (you have to create these folder)

$foto_upload->foto_folder = $_SERVER['DOCUMENT_ROOT']."/files/photo/";

$foto_upload->thumb_folder = $_SERVER['DOCUMENT_ROOT']."/files/thumb/";

$foto_upload->extensions = array(".jpg"); // specify the allowed extension(s) here

$foto_upload->language = "en";

$foto_upload->x_max_size = 300;

$foto_upload->y_max_size = 200;

$foto_upload->x_max_thumb_size = 120;

$foto_upload->y_max_thumb_size = 150;

if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") {

$foto_upload->the_temp_file = $_FILES['upload']['tmp_name'];

$foto_upload->the_file = $_FILES['upload']['name'];

$foto_upload->http_error = $_FILES['upload']['error'];

$foto_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true

$foto_upload->do_filename_check = "n";

if ($foto_upload->upload()) {

$foto_upload->process_image(false, true, 80);

$foto_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto"

}

}

$error = $foto_upload->show_error_string();

?>

2,上传图片的调用示例

php图片上传类的调用示例-bbs.it-home.org

上传图片

选择图片文件:

替换旧的图片文件吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值