PHP生成缩略图
2017-07-29 15:37
309 查看
/** * 生成唯一字符串 * @return string */ function getUniName(){ return md5(uniqid(microtime(true),true)); } /** * 得到文件的扩展名 * @param string $filename * @return string */ function getExt($filename){ return strtolower(end(explode(".",$filename))); }
/** * 生成缩略图 * @param string $filename * @param string $destination * @param int $dst_w * @param int $dst_h * @param bool $isReservedSource * @param number $scale * @return string */ function thumb($filename,$destination=null,$dst_w=null,$dst_h=null,$isReservedSource=true,$scale=0.5){ //默认不保留源文件$isReservedSource=false list($src_w,$src_h,$imagetype)=getimagesize($filename); if(is_null($dst_w)||is_null($dst_h)){ $dst_w=ceil($src_w*$scale); $dst_h=ceil($src_h*$scale); } $mime=image_type_to_mime_type($imagetype); //echo $mime;//image/jpeg,image/gif $createFun=str_replace("/", "createfrom", $mime);//imagecreatefromjpeg $outFun=str_replace("/", null, $mime);//imagejpeg $src_image=$createFun($filename); $dst_image=imagecreatetruecolor($dst_w, $dst_h); imagecopyresampled($dst_image, $src_image, 0,0,0,0, $dst_w, $dst_h, $src_w, $src_h); if($destination&&!file_exists(dirname($destination))){ mkdir(dirname($destination),0777,true); } $dstFilename=$destination==null?getUniName().".".getExt($filename):$destination; $outFun($dst_image,$dstFilename); imagedestroy($src_image);//销毁资源 imagedestroy($dst_image);//销毁资源 if(!$isReservedSource){ unlink($filename);//删掉源文件 } return $dstFilename; }
相关文章推荐
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- php 等比例生成缩略图
- 经典的PHP缩略图生成程式
- php 修改图片的大小、裁剪图片以及生成缩略图方法
- php 根据url自动生成缩略图,并处理高并发问题
- php超全缩略图生成程序
- 使用PHP生成图片的缩略图的方法
- Resizing images with PHP(Php生成缩略图)
- php生成缩略图示例代码分享(使用gd库实现)
- PHP生成图片缩略图源代码
- PHP基于GD库的缩略图生成代码(支持jpg,gif,png格式)
- PHP基于GD库实现的生成图片缩略图函数示例
- PHP使用imagick读取PDF生成png缩略图的两种方法
- php生成缩略图的类代码
- php生成图片缩略图功能示例
- php生成验证码,缩略图及水印图的类分享
- 利用PHP为大图片生成缩略图代码实现
- php实现图片上传并利用ImageMagick生成缩略图
- PHP做视频网站,让程序自动实现视频格式转换、设置视频大小、生成视频缩略图