php教程 php手册 php 图片上传代码(具有生成缩略图与增加水印功能)

php 图片上传代码(具有生成缩略图与增加水印功能)

Jun 13, 2016 am 09:48 AM
php 업로드 그리고 암호 기능 할 수 있다 그림 증가하다 지불 양수표 소스 코드 생성하다

这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。

php教程 图片上传代码(具有生成缩略图与增加水印功能)
这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。

class upfile {
 public $filepath = "www.bKjia.c0m/"; //上传文件存放文件夹

 public $filesize = 1000000; //允许上传的大小

 //如果要修改允许上传文件的类型  请搜索 【 switch ($upfiletype) { //文件类型  】

 public $reimagesize = array (
  true, //是否生成缩略图
  400, //缩略图宽
  300,//缩略图高
  "" //缩略图存放文件夹 如果为空和当前要生成缩略图的文件在同一目录 文件前缀r_
 ); //是否生成缩略图 array(生成或不生成,缩略图宽,缩略图高,存放文件夹); 注意:存放文件夹后跟 '/'

 public $india = true; //是否打水印 true打 false不打

 public $indiaimage = ""; //水印图片地址为空则不打图片水印 如果有文字水印建议不要开启图片水印

 public $indiaimagex = 100; //图片距离图片左边距离

 public $indiaimagey = 10; //图片距离图片上面距离

 public $indiatext = "www.bKjia.c0m"; //水印文字

 public $fontsize = 6; //水印文字大小,1最小6最大

 public $indiatextx = 10; //文字距离图片左边距离

 public $indiatexty = 10; //文字距离图片上面距离

 public $r = 250; //图片颜色三原色 $r红

 public $g = 250; //$g绿

 public $b = 250; //$b蓝

 public $indiapath = ""; //加了水印的图片保存路径,如果为空就直接替代原来的图片

 //开始上传处理
 function uploadfile($upfile) {
  if ($upfile == "") {
   die("uploadfile:参数不足");
  }
  if (!file_exists($this->filepath)) {
   mkdir($this->filepath);
  }
  $upfiletype = $upfile['type'];
  $upfilesize = $upfile['size'];
  $upfiletmpname = $upfile['tmp_name'];
  $upfilename = $upfile['name'];
  $upfileerror = $upfile['error'];
  if ($upfilesize > $this->filesize) {
   return false; //文件过大
  }
  switch ($upfiletype) { //文件类型
   case 'image/jpeg' :
    $type = 'jpg';
    break;
   case 'image/pjpeg' :
    $type = 'jpg';
    break;
   case 'image/png' :
    $type = 'png';
    break;
   case 'image/gif' :
    $type = 'gif';
    break;
  }
  if (!isset ($type)) {
   return false; //不支持此类型
  }
  if (!is_uploaded_file($upfiletmpname) or !is_file($upfiletmpname)) {
   return false;
   ; //文件不是经过正规上传的;
  }
  if ($this->upfileerror != 0) {
   return false; //其他错误
  }
  if ($this->upfileerror == 0) {
   if (!file_exists($upfiletmpname)) {
    return false; //临时文件不存在
   } else {
    $filename = date("ymdhis", time() + 3600 * 8); //图片已当前时间命名
    $filename = $this->filepath . $filename . "." . $type;
    if (!move_uploaded_file($upfiletmpname, $filename)) {
     return false; //文件在移动中丢失
    } else {
     if ($this->india == true) {
      $this->goindia($filename, $type,true);
     } else {
      if ($this->reimagesize[0] == true) {
       $this->goreimagesize($filename, $type);
      } else {
       return true; //上传成功!
       unlink($upfiletmpname);
      }
     }
    }

   }
  }

 }
 //添加水印处理
 function goindia($filename, $filetype,$reimage=false) {
  if (!file_exists($filename)) {
   $this->reerror(7); //要添加水印的文件不存在
  } else {
   if ($filetype == "jpg") {
    $im = imagecreatefromjpeg($filename);
   } else
    if ($filetype == "gif") {
     $im = imagecreatefromgif($filename);
    } else
     if ($filetype == "png") {
      $im = imagecreatefrompng($filename);
     }
   if ($this->indiatext != "") { //如果水印文字不为空
    $textcolor = imagecolorallocate($im, $this->r, $this->g, $this->b); //设置文字颜色
    imagestring($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); //将文字写入图片
   }
   if ($this->indiaimage != "") {//如果水印图片不为空
    $indiaimagetype = getimagesize($this->indiaimage);
    $logow = $indiaimagetype[0]; //得到水印图片的宽
    $logoh = $indiaimagetype[1]; //得到水印图片的高
    switch ($indiaimagetype[2]) { //判断水印图片的格式
     case 1 :
      $indiaimagetype = "gif";
      $logo = imagecreatefromgif($this->indiaimage);
      break;
     case 2 :
      $indiaimagetype = "jpg";
      $logo = imagecreatefromjpeg($this->indiaimage);
      break;
     case 3 :
      $indiaimagetype = "png";
      $logo = imagecreatefrompng($this->indiaimage);
      break;
    }
    imagealphablending($im, true); //打开混色模式
    imagecopy($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
    imagedestroy($im);
    imagedestroy($logo);
   }
  }
  if ($this->indiapath == "") { //如果水印存放地址不为空
   if ($filetype == "jpg") {
    imagejpeg($im, $filename);
   } else
    if ($filetype == "gif") {
     imagegif($im, $filename);
    } else
     if ($filetype == "png") {
      imagepng($im, $filename);
     }
   if($reimage == true){
    $this->goreimagesize($filename,$filetype);
   }else{
    return true; //添加水印成功
   }
  } else {
   if (!file_exists($this->indiapath)) {
    mkdir($this->indiapath);
    return false; //请重新上传
   } else {
    $indianame = basename($filename);
    $indianame = $this->indiapath . $indianame;
    if ($filetype == "jpg") {
     imagejpeg($im, $indianame);
    } else
     if ($filetype == "gif") {
      imagegif($im, $indianame);
     } else
      if ($filetype == "png") {
       imagepng($im, $indianame);
      }
    if($reimage == true){
     $this->goreimagesize($indianame,$filetype);
     echo $indianame;
    }else{
     return true; //添加水印成功
    }
   }
  }
 }
 function goreimagesize($filename, $filetype) {
  if (!file_exists($filename)) {
   return false; //要生成缩略图的图片不存在
  } else {
   if ($filetype == 'jpg') {
    $reimage = imagecreatefromjpeg($filename);
   }
   elseif ($filetype == 'png') {
    $reimage = imagecreatefrompng($filename);
   } else
    if ($filetype == 'gif') {
     $reimage = imagecreatefromgif($filename);
    }
   if (isset ($reimage)) {
    $srcimagetype = getimagesize($filename);
    $srcimagetypew = $srcimagetype[0]; //得到原始图片宽度
    $srcimagetypeh = $srcimagetype[1]; //得到原始图片高度
    $reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]);
    imagecopyresized($reim, $reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew, $srcimagetypeh);
    $reimagepath = $this->reimagesize[3];
    if ($reimagepath != "") { //如果存放水印地址不为空
     if (!file_exists($reimagepath)) {
      mkdir($reimagepath);
     } else {
      $reimagename = basename($filename);
      $reimagename = $reimagepath . "r_" . $reimagename;
      if ($filetype == "gif")
       imagegif($reim, $reimagename);
      else
       if ($filetype == "jpg")
        imagejpeg($reim, $reimagename);
       else
        if ($filetype == "png")
         imagepng($reim, $reimagename);
      return true;
     }
    } else {
     $filename = basename($filename);
     if($this->indiapath == ""){
      $filename = $this->filepath."r_" . $filename;
     }else{
      $filename = $this->indiapath."r_" . $filename;
     }
     if ($filetype == "gif")
      imagegif($reim, $filename);
     else
      if ($filetype == "jpg")
       imagejpeg($reim, $filename);
      else
       if ($filetype == "png")
        imagepng($reim, $filename);
     return true;
    }

   }
  }
 }

}
if ($_post["submit"]) {
 $file = $_files['uploadfile'];
 $upfile = new upfile();
 echo $upfile->uploadfile($file);
}
?>

 


 

 
 
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP는 오픈 소스 MVC 프레임워크입니다. 이를 통해 애플리케이션 개발, 배포 및 유지 관리가 훨씬 쉬워집니다. CakePHP에는 가장 일반적인 작업의 과부하를 줄이기 위한 여러 라이브러리가 있습니다.

PHP에서 HTML/XML을 어떻게 구문 분석하고 처리합니까? PHP에서 HTML/XML을 어떻게 구문 분석하고 처리합니까? Feb 07, 2025 am 11:57 AM

이 튜토리얼은 PHP를 사용하여 XML 문서를 효율적으로 처리하는 방법을 보여줍니다. XML (Extensible Markup Language)은 인간의 가독성과 기계 구문 분석을 위해 설계된 다목적 텍스트 기반 마크 업 언어입니다. 일반적으로 데이터 저장 AN에 사용됩니다

See all articles