HTML > HEAD > TITLE > 上传图片 /TITLE > META NAME = "Generator" CONTENT ="/> HTML > HEAD > TITLE > 上传图片 /TITLE > META NAME = "Generator" CONTENT =">

 >  기사  >  php教程  >  深入探讨PHP生成缩略图的实现方法

深入探讨PHP生成缩略图的实现方法

WBOY
WBOY원래의
2016-06-13 11:10:151054검색

我们在运用

PHP生成缩略图HTML代码

  1.  !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  2.  HTML> 
  3.  HEAD> 
  4.  TITLE> 上传图片  /TITLE> 
  5.  META NAME="Generator" CONTENT="EditPlus"> 
  6.  META NAME="Author" CONTENT=""> 
  7.  META NAME="Keywords" CONTENT=""> 
  8.  META NAME="Description" CONTENT=""> 
  9.  meta http-equiv="Content-Type" content="text/html; charset=gbk" /> 
  10.  /HEAD> 
  11.  
  12.  BODY> 
  13.  FORM METHOD="POST" ACTION="tu.php?act=upload" enctype="multipart/form-data" > 
  14.  p> INPUT TYPE="file" NAME="file"/> /p> 
  15.  p> INPUT TYPE="submit" value="hao"/>   INPUT TYPE="reset"/> /p> 
  16.  /FORM> 
  17.  /BODY> 
  18.  /HTML> 


PHP生成缩略图PHP代码

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li><span>/*后台登陆界面  </span></li>
<li class="alt"><span>*黄春龙学习编写  </span></li>
<li><span>*首写时间:2009-10-27  </span></li>
<li class="alt"><span>*更新时间:2009/11/24  </span></li>
<li><span>*/  </span></li>
<li class="alt"><span>require_once '../xmphp/init.php';</span></li>
<li class="alt"><span>//全站配置文件  </span></li>
<li><span>require_once XMPHP_COMMON.'/smarty.php';</span></li>
<li><span>//smarty功能  </span></li>
<li class="alt"><span>require_once XMPHP_COMMON.'/mysql.php';</span></li>
<li class="alt"><span>//mysql功能  </span></li>
<li><span>require_once XMPHP_COMMON.'/func.php';</span></li>
<li><span>//常用函数  </span></li>
<li class="alt"><span>if(isset($_GET['act'])&&$_GET['act']<br>=='upload'){  </span></li>
<li>
<span>$</span><span class="attribute">file</span><span>=$_FILES['file'];  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">info</span><span>=</span><span class="attribute-value">pathinfo</span><span>($file['name']);  </span>
</li>
<li>
<span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">array</span><span>('jpg','gif','png');  </span>
</li>
<li class="alt"><span>if(!in_array($info['extension'],$a)){  </span></li>
<li><span>alert("请选择jpg,gif,png格式的图片<br>进行上传");  </span></li>
<li class="alt"><span>exit;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>if($file['name'] !=''){  </span></li>
<li>
<span>$</span><span class="attribute">dirname</span><span>=</span><span class="attribute-value">date</span><span>('Ym');  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">dirname</span><span>=</span><span class="attribute-value">'../upload/'</span><span>.$dirname;</span>
</li>
<li class="alt"><span>//原图路径  </span></li>
<li>
<span>$</span><span class="attribute">xin</span><span>=$dirname.'/s';//PHP生成缩略图路径  </span>
</li>
<li class="alt"><span>if(!file_exists($xin)){  </span></li>
<li><span>@mkdir($xin);  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>$</span><span class="attribute">image1</span><span>=$info['filename'].'120_120'.'.'<br>.$info['extension'];//缩略图名称  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">image2</span><span>=$info['filename'].'300_300'.'.'<br>.$info['extension'];//缩略图名称  </span>
</li>
<li><span>list($imagw,$imagh)=getimagesize($file<br>['tmp_name']);//取得原图的宽高(另外$file<br>['tmp_name']只是一个临时文件路径,有时可能<br>会报错,如报错只需把临时文件路径改成你上传<br>后的原图的路径即可)  </span></li>
<li class="alt"><span>//120图判断各种情况来取得缩略图的宽高  </span></li>
<li>
<span>if($imagw</span><span class="tag"><span> =120&&$imagh</span><span class="tag"><span> =120){  </span></span></span>
</li>
<li class="alt">
<span>$</span><span class="attribute">m120w</span><span>=$imagw;  </span>
</li>
<li>
<span>$</span><span class="attribute">m120h</span><span>=$imagh;  </span>
</li>
<li class="alt">
<span>}elseif($imagw</span><span class="tag">></span><span>$imagh)  </span>
</li>
<li><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">m120w</span><span>=120;  </span>
</li>
<li>
<span>$</span><span class="attribute">m120h</span><span>=</span><span class="attribute-value">intval</span><span>($m120w/number_format(<br>$imagw/$imagh,2));//缩略图的宽除以(原<br>图的宽除以原图的高)number_format查手册  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>else  </span></li>
<li class="alt"><span>{  </span></li>
<li>
<span>$</span><span class="attribute">m120h</span><span>=</span><span class="attribute-value">300</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">m120w</span><span>=</span><span class="attribute-value">intval</span><span>($m120h/number_format(<br>$imagh/$imagw,2));//缩略图的高除以(原图<br>的高除以原图的宽)number_format查手册  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>//300图判断各种情况来取得缩略图的宽高  </span></li>
<li>
<span>if($imagw</span><span class="tag"><span> =300&&$imagh</span><span class="tag"><span> =300){  </span></span></span>
</li>
<li class="alt">
<span>$</span><span class="attribute">m300w</span><span>=$imagw;  </span>
</li>
<li>
<span>$</span><span class="attribute">m300h</span><span>=$imagh;  </span>
</li>
<li class="alt">
<span>}elseif($imagw</span><span class="tag">></span><span>$imagh){  </span>
</li>
<li>
<span>$</span><span class="attribute">m300w</span><span>=300;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">m300h</span><span>=</span><span class="attribute-value">intval</span><span>($m300w/number_format(<br>$imagw/$imagh,2));//缩略图的宽除以(原<br>图的宽除以原图的高)number_format查手册  </span>
</li>
<li><span>}else{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">m300h</span><span>=300;  </span>
</li>
<li>
<span>$</span><span class="attribute">m300w</span><span>=</span><span class="attribute-value">intval</span><span>($m300h/number_format(<br>$imagh/$imagw,2));//缩略图的高除以(原<br>图的高除以原图的宽)number_format查手册  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>if($info['extension']=='jpg'){  </span></li>
<li class="alt"><span>$info['extension']='jpeg';//判断图像类<br>型如果是JPG的则把它转换成JPEG的因为图像处<br>理函数在处理JPG的时候都是用的JPEG而不是JPG  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span>$</span><span class="attribute">iml</span><span>=</span><span class="attribute-value">'imagecreatefrom'</span><span>.$info['extension'];</span>
</li>
<li class="alt"><span>//取得不同图片格式的不同函数  </span></li>
<li>
<span>$</span><span class="attribute">yuan</span><span>=$iml($file['tmp_name']);</span>
</li>
<li><span>//根据前面取的不同函数来取的原图的标识  </span></li>
<li class="alt">
<span>$</span><span class="attribute">mu1</span><span>=</span><span class="attribute-value">imagecreatetruecolor</span><span>($m120w,$m120h);</span>
</li>
<li class="alt"><span>//取的要生成的缩略图的标识  </span></li>
<li>
<span>$</span><span class="attribute">mu2</span><span>=</span><span class="attribute-value">imagecreatetruecolor</span><span>($m300w,$m300h);</span>
</li>
<li><span>//取的要生成的缩略图的标识  </span></li>
<li class="alt">
<span>$</span><span class="attribute">c</span><span>=</span><span class="attribute-value">'image'</span><span>.$info['extension'];</span>
</li>
<li class="alt"><span>//生成输出缩略图的函数,不能分开写,否则报错  </span></li>
<li><span>imagecopyresampled($mu1,$yuan,0,0,0,<br>0,$m120w,$m120h,$imagw,$imagh);//生成缩略图120  </span></li>
<li class="alt">
<span>$</span><span class="attribute">res</span><span>=$c($mu1,$xin.'/'.$image1);</span>
</li>
<li class="alt"><span>//存放缩略图  </span></li>
<li><span>if(!$res){  </span></li>
<li class="alt"><span>alert('生成120缩略图失败');  </span></li>
<li><span>exit;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>imagecopyresampled($mu2,$yuan,0,0<br>,0,0,$m300w,$m300h,$imagw,$imagh);</span></li>
<li><span>//PHP生成缩略图生成缩略图120  </span></li>
<li class="alt">
<span>$</span><span class="attribute">res</span><span>=$c($mu2,$xin.'/'.$image2);</span>
</li>
<li class="alt"><span>//存放缩略图  </span></li>
<li><span>if($res){  </span></li>
<li class="alt"><span>alert('生成300缩略图成功');  </span></li>
<li><span>exit;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>$tpl-</span><span class="tag">></span><span>display("tu/tu.html");  </span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>

以上这一大段的代码示例就是PHP生成缩略图的相关实现方法。


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