Welcome

首页 / 网页编程 / PHP / PHP简单创建压缩图的方法

本文实例讲述了PHP简单创建压缩图的方法。分享给大家供大家参考,具体如下:
<?php//创建压缩图function _create_thumbnail($srcFile, $toW, $toH, $toFile=""){if ($toFile == ""){$toFile = $srcFile;}$info = "";$data = getimagesize($srcFile, $info);if (!$data)return false;//将文件载入到资源变量im中switch ($data[2]){case 1:$im = imagecreatefromgif($srcFile);break;case 2:$im = imagecreatefromjpeg($srcFile);break;case 3:$im = imagecreatefrompng($srcFile);break;}//计算缩略图的宽高$srcW = imagesx($im);$srcH = imagesy($im);$toWH = $toW / $toH;$srcWH = $srcW / $srcH;if ($toWH <= $srcWH){$ftoW = $toW;$ftoH = (int)($ftoW * ($srcH / $srcW));}else{$ftoH = $toH;$ftoW = (int)($ftoH * ($srcW / $srcH));}if (function_exists("imagecreatetruecolor")){$ni = imagecreatetruecolor($ftoW, $ftoH); //新建一个真彩色图像if ($ni){//重采样拷贝部分图像并调整大小 可保持较好的清晰度imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);}else{//拷贝部分图像并调整大小$ni = imagecreate($ftoW, $ftoH);imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);}}else{$ni = imagecreate($ftoW, $ftoH);imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);}//保存到文件 统一为.png格式imagepng($ni, $toFile); //以 PNG 格式将图像输出到浏览器或文件ImageDestroy($ni);ImageDestroy($im);}?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数学运算技巧总结》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。