PHP画图之GD库的使用——略缩图(二)
2015-09-28 21:00
886 查看
<?php // 将图像等比放大或缩小 class resize{ private $src; private $image; private $width; private $height; private $imageType; private $imageResize; private $newWidth; private $newHeight; // 文件路径名,期待文件的宽度 public function __construct($fileName, $newWidth){ $this->src = $fileName; $this->newWidth = $newWidth; $this->imageType = exif_imagetype($fileName); $this->image = $this->openImage($this->src); if($this->image){ $this->width = imagesx($this->image); $this->height = imagesy($this->image); } } private function openImage($file){ switch ($this->imageType) { case IMAGETYPE_GIF: $img = imagecreatefromgif($file); break; case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($file); break; case IMAGETYPE_PNG: $img = imagecreatefrompng($file); break; } return $img; } private function saveImage(){ switch ($this->imageType) { case IMAGETYPE_GIF: header("Content-type: " . image_type_to_mime_type(IMAGETYPE_GIF)); imagegif($this->imageResize); break; case IMAGETYPE_JPEG: header("Content-type: " . image_type_to_mime_type(IMAGETYPE_JPEG)); imagejpeg($this->imageResize); break; case IMAGETYPE_PNG: header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG)); imagepng($this->imageResize); break; } imagedestroy($this->image); imagedestroy($this->imageResize); } public function resizeImage(){ $ratio = $this->height/$this->width; $this->newHeight = $this->newWidth*$ratio; $this->imageResize = imagecreatetruecolor($this->newWidth, $this->newHeight); imagecopyresampled($this->imageResize, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); $this->saveImage(); } } $img1 = new resize('GD/img/logo.png',50); $img1->resizeImage(); ?>
相关文章推荐
- win7搭建NTP服务
- 《php和mysql web开发》笔记——第2章数据的存储与检索
- Thinkphp---快捷操作方法:(即快捷函数)
- PHP安全编程:记住登录状态的安全做法
- ThinkPHP--空操作处理和空控制器处理
- PHP 操作MySQL:增删改查
- arcmap制作tpk切片文件形成离线地图包
- yii框架
- PHP过滤各种HTML标签
- PHP知识
- 使用PSFTP工具传输文件的命令使用方法
- 外网访问内网的FTP服务器-原理解析
- 用 PHP 发邮件
- php冒泡排序实例代码
- PHP 性能分析 xhprof 安装
- phpmyadmin乱码问题
- FragmentPagerAdapter与FragmentStatePagerAdapter区别
- 新浪sae开发不能写入文件
- EditPlus正则表达式 替换字符串开头
- PHP中的一些新特性