PHP批量生成图片缩略图的方法_PHP
May 30, 2016 am 08:44 AM
php
그림
일괄 생성
미리보기 이미지
本文实例讲述了PHP批量生成图片缩略图的方法。分享给大家供大家参考。具体如下:
<?php //用PHP批量生成图片缩略图 function mkdirs($dirname,$mode=0777) //创建目录(目录, [模式]) { if(!is_dir($dirname)) { mkdirs($dirname,$mode); //如果目录不存在,递归建立 return mkdir($dirname,$mode); } return true; } function savefile($filename,$content='') //保存文件(文件, [内容]) { if(function_exists(file_put_contents)) { file_put_contents($filename,$content); } else { $fp=fopen($filename,"wb"); fwrite($fp,$content); fclose($fp); } } function getsuffix($filename) //获取文件名后缀 { return end(explode(".",$filename)); } function checksuffix($filename,$arr) //是否为允许类型(当前, 允许) { if(!is_array($arr)) { $arr=explode(",",str_replace(" ","",$arr)); } return in_array($filename,$arr) ? 1 : 0; } class image { var $src; //源地址 var $newsrc; //新图路径(本地化后) var $allowtype=array(".gif",".jpg",".png",".jpeg"); //允许的图片类型 var $regif=0; //是否缩略GIF, 为0不处理 var $keep=0; //是否保留源文件(1为保留, 0为MD5) var $over=0; //是否可以覆盖已存在的图片,为0则不可覆盖 var $dir; //图片源目录 var $newdir; //处理后的目录 function __construct($olddir=null,$newdir=null) { $this->dir=$olddir ? $olddir : "./images/temp"; $this->newdir=$newdir ? $newdir : "./images/s"; } function reNames($src) { $md5file=substr(md5($src),10,10).strrchr($src,"."); //MD5文件名后(例如:3293okoe.gif) $md5file=$this->w."_".$this->h."_".$md5file; //处理后文件名 return $this->newdir."/".$md5file; //将源图片,MD5文件名后保存到新的目录里 } function Mini($src,$w,$h,$q=80) //生成缩略图 Mini(图片地址, 宽度, 高度, 质量) { $this->src=$src; $this->w=$w; $this->h=$h; if(strrchr($src,".")==".gif" && $this->regif==0) //是否处理GIF图 { return $this->src; } if($this->keep==0) //是否保留源文件,默认不保留 { $newsrc=$this->reNames($src); //改名后的文件地址 } else //保持原名 { $src=str_replace("\\","/",$src); $newsrc=$this->newdir.strrchr($src,"/"); } if(file_exists($newsrc) && $this->over==0) //如果已存在,直接返回地址 { return $newsrc; } if(strstr($src,"http://") && !strstr($src,$_SERVER['HTTP_HOST'])) //如果是网络文件,先保存 { $src=$this->getimg($src); } $arr=getimagesize($src); //获取图片属性 $width=$arr[0]; $height=$arr[1]; $type=$arr[2]; switch($type) { case 1: //1 = GIF, $im=imagecreatefromgif($src); break; case 2: //2 = JPG $im=imagecreatefromjpeg($src); break; case 3: //3 = PNG $im=imagecreatefrompng($src); break; default: return 0; } //处理缩略图 $nim=imagecreatetruecolor($w,$h); $k1=round($h/$w,2); $k2=round($height/$width,2); if($k1<$k2) { $width_a=$width; $height_a=round($width*$k1); $sw=0; $sh=($height-$height_a)/2; } else { $width_a=$height/$k1; $height_a=$height; $sw=($width-$width_a)/2; $sh = 0; } //生成图片 if(function_exists(imagecopyresampled)) { imagecopyresampled($nim,$im,0,0,$sw,$sh,$w,$h,$width_a,$height_a); } else { imagecopyresized($nim,$im,0,0,$sw,$sh,$w,$h,$width_a,$height_a); } if(!is_dir($this->newdir)) { mkdir($this->newdir); } switch($type) //保存图片 { case 1: $rs=imagegif($nim,$newsrc); break; case 2: $rs=imagejpeg($nim,$newsrc,$q); break; case 3: $rs=imagepng($nim,$newsrc); break; default: return 0; } return $newsrc; //返回处理后路径 } function getimg($filename) { $md5file=$this->dir."/".substr(md5($filename),10,10).strrchr($filename,"."); if(file_exists($md5file)) { return $md5file; } //开始获取文件,并返回新路径 $img=file_get_contents($filename); if($img) { if(!is_dir($this->dir)) { mkdir($this->dir); } savefile($md5file,$img); return $md5file; } } function reImg($src,$w,$h,$q) //转换缩略图(文件名和结构不变) { $this->keep=1; return $this->Mini($src,$w,$h,$q); //return 生成的地址 } } $image=new image(); echo $image->reImg("images/zht.jpg",75,75,80); echo "<br/>"; echo $image->reImg("images/m8920.jpg",75,75,80); echo "<br/>"; echo $image->getimg("./images/s/zht.jpg"); ?>
로그인 후 복사
希望本文所述对大家的php程序设计有所帮助。
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

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

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

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

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7309
9


자바 튜토리얼
1623
14


Cakephp 튜토리얼
1344
46


라라벨 튜토리얼
1259
25


PHP 튜토리얼
1207
29



Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법
