具体用法如下:
$a=thumbImage('1.jpg',200,100,'2.jpg');//将1.jpg生成宽为200,高为100的缩略图,并保存为2.jpg print_r($a);
打印结果:
Array(
[status] => 0
[file] => 1.jpg
[width] => 200
[height] => 200
[savePath] => 2.jpg
[info] => 缩略图片生成成功
)
$a=thumbImage('1.jpg',200,100);//将1.jpg生成宽为200,高为100的缩略图,并直接覆盖1.jpg,不生成新图片
函数代码如下:
<?php /* *php生成图片缩略图的函数 *www.phpernote.com */ function thumbImage($file,$thumbWidth,$thumbHeight,$savePath='',$isCut=true,$quality=100){ $result=array('status'=>0,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>''); if(!file_exists($file)){ return array('status'=>1,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片文件不存在'); } //检测文件类型 $fp=fopen($file,'rb'); $str=fread($fp,2); //只读2字节 fclose($fp); $str=@unpack("c2chars",$str); $typeCode=intval($str['chars1'].$str['chars2']); $fileType=''; switch($typeCode){ case 255216: $fileType='jpg'; break; case 7173: $fileType='gif'; break; case 6677: $fileType='bmp'; break; case 13780: $fileType='png'; break; default: $fileType=$typeCode; break; } if($str['chars1']=='-1'&&$str['chars2']=='-40'){ $fileType='jpg'; } if($str['chars1']=='-119'&&$str['chars2']=='80'){ $fileType='png'; } if(!in_array($fileType,array('jpg','gif','bmp','png'))){ return array('status'=>2,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片文件类型不正确:'.$fileType); } //图片缩放处理 if($fileType=='jpg'){ $im=imagecreatefromjpeg($file); } if($fileType=='gif'){ $im=imagecreatefromgif($file); } if($fileType=='png'){ $im=imagecreatefrompng($file); } if(empty($savePath)) $savePath=$file; $width=imagesx($im); $height=imagesy($im); if($width<$thumbWidth&&$height<$thumbHeight){ return array('status'=>3,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片尺寸小于生成缩略图的尺寸'); } $ratio=$width/$height;//实际图象的比例 $thumbRatio=$thumbWidth/$thumbHeight;//改变后的图象的比例 if($isCut){//裁切处理 if(function_exists('imagepng')&&(str_replace('.','',PHP_VERSION)>=512)){//针对php版本大于5.12参数变化后的处理情况 $quality=9; } if($ratio>=$thumbRatio){//高度优先 $newimg=imagecreatetruecolor($thumbWidth,$thumbHeight); imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,(($height)*$thumbRatio),$height); imagejpeg($newimg,$savePath,$quality); } if($ratio<$thumbRatio){//宽度优先 $newimg=imagecreatetruecolor($thumbWidth,$thumbHeight); imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,$width,(($width)/$thumbRatio)); imagejpeg($newimg,$savePath,$quality); } }else{//不裁切处理 if($ratio>=$thumbRatio){ $newimg=imagecreatetruecolor($thumbWidth,($thumbWidth)/$ratio); imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,($thumbWidth)/$ratio,$width,$height); imagejpeg($newimg,$savePath,$quality); } if($ratio<$thumbRatio){ $newimg=imagecreatetruecolor(($thumbHeight)*$ratio,$thumbHeight); imagecopyresampled($newimg,$im,0,0,0,0,($thumbHeight)*$ratio,$thumbHeight,$width,$height); imagejpeg($newimg,$savePath,$quality); } } ImageDestroy($im);//www.phpernote.com/php-function/1007.html return array('status'=>0,'file'=>$file,'width'=>$thumbWidth,'height'=>$thumbHeight,'savePath'=>$savePath,'info'=>'缩略图片生成成功'); }
您可能感兴趣的文章
- php生成图片缩略图的类方法
- PHP 生成连续的数字(字母)数组函数range()分析,PHP抽奖程序函数
- PHP生成随机密码函数
- php获取内容中的所有图片列表并输出的方法
- php提取身份证号码中的生日日期以及验证是否为未成年人的函数
- php生成柱状图(统计图)程序
- php生成验证码函数
- php生成短网址的思路以及实现方法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
