zoukankan      html  css  js  c++  java
  • 将一张图片生成多张缩略图并保存

    首先上封装的函数代码:

    function thumb($filename,$destination=null,$dst_w=null,$dst_h=null,$isReservedSource=true,$scale=0.5){
    	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/jpg
    	$createFun=str_replace("/", "createfrom", $mime);
               //$createFun使得image/jpg转换为imagecreatefromjpg来生成图像,这样就可以使用$mime得到图片的类型(如jpg,png)等
    	  //不用每次都设定函数为imagecreatefromjpg,imagecreatefrompng等
          $outFun=str_replace("/", null, $mime);
    	$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;
    }
    

     该函数保存在 ../lib/image.func.php中

    有因为getUniName()函数和getExt()函数在 ../lib/string.func.php中,所以

    调用函数为:

    require_once '../lib/string.func.php';
    require_once '../lib/image.func.php';
    $filename="des_big.jpg";
    //thumb($filename);
    thumb($filename,"image_50/".$filename,50,50,true); //调用../lib/image.func.php函数
    thumb($filename,"image_220/".$filename,220,220,true);
    thumb($filename,"image_350/".$filename,350,350,true);
    thumb($filename,"image_800/".$filename,800,800,true);
    

     输出结果是会在image_50,image_220,image_350,image_800的文件夹内分别产生50*50,220*220,350*350,800*800的图片(以des_big.jpg为原型产生的)

    /**
     * 生成唯一字符串
     * @return string
     */
    function getUniName() {
    	return md5(uniqid(microtime(ture),(ture)));   //这里得到一个可以变化的字母家数字,为了使上传的文件即使相同,传到服务器上也不相同 ;我觉得用date(y.m.d).time()这样更好,又能得到时间,后面的time()时间戳还能变化
    }
    
    /**
     * 得到文件的扩展名
     * @param string $filename
     * @return string
     */
    function getExt($filename){
    	return strtolower(end(explode(".",$filename)));
    }
    

    以下介绍在函数中用到的函数:

    imagecreatetruecolor — 新建一个真彩色图像
    说明
    resource imagecreatetruecolor ( int $width , int $height )
    
    imagecopyresampled — 重采样拷贝部分图像并调整大小
    说明
    bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
    
    imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。 
    
    file_exists() 函数检查文件或目录是否存在。
    
    如果指定的文件或目录存在则返回 true,否则返回 false。
    语法
    
    file_exists(path)
    
    imagedestroy — 销毁一图像
    说明
    bool imagedestroy ( resource $image )
    
    imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,
    
    image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型
    MIME类型有如jpeg,png之类的
    imagecreatefromjpeg — 由文件或 URL 创建一个新图象。
    说明
    resource imagecreatefromjpeg ( string $filename )
    
    imagecreatefromjpeg() 返回一图像标识符,代表了从给定的文件名取得的图像。 
    
  • 相关阅读:
    Flink核心技术
    Flink学习问题和答案
    Spark知识点总结
    Scala知识点总结
    Spark测试题
    Hadoop概念试题
    IntelliJ IDEA 2019 快捷键终极大全
    Linux入门
    javaSe知识点总结
    Data
  • 原文地址:https://www.cnblogs.com/jacson/p/4246763.html
Copyright © 2011-2022 走看看