php图片格式转换(webp,png,jpg,gif,png)之间互相转换
2016/03/12    标签: php   
<?php

//将图片保存到服务器临时文件
/***
	$img=imagecreatefromjpeg('http://img.xiaolinsi.com/2015/0720/20150720101726233.jpg');
	$imgNameTem='/temp/temp.jpg';
	$imgPathTem=$_SERVER['DOCUMENT_ROOT'].$imgNameTem;
	$rs=imagejpeg($img,$imgPathTem,87);
	if($rs){//保存到服务器临时文件结束
			imagedestroy($img);
			for ($i=0; $i <1; $i++) { 
	$imgCommStr=date('YmdHis').substr(str_shuffle('abcdefghigklmnopqrstuvwxyz'),0,4);
			//降临时文件图片转换为webp
			$imgwebp=imagecreatefromjpeg($imgPathTem);
			$imgNameWebp='/temp/'.$imgCommStr.'.webp';
			$imgPathWebp=$_SERVER['DOCUMENT_ROOT'].$imgNameWebp;
			imagewebp($imgwebp,$imgPathWebp);
			echo '<img src=''.$imgNameWebp.''/><br/>';	
			imagedestroy($imgwebp);
		};
	}
	***/
	//图片格式转换
	/***
	pam:
		原图片//可为http图片(必须以http开头)或者服务器端图片
		例如:	var_dump(imgConvert($_SERVER['DOCUMENT_ROOT'].'/images/1.webp'));
		或者var_dump(imgConvert('http://localhost:80/images/1.webp'));
		新图片类型(default:原图片类型)
		新文件名(default:原文件名,文件名不带类型)
		新文件路径(default:imageconvert/+图片类型+/)图片路径必须以/结尾;
	rt:
		array或者1(传递非图片时返回)或者2(失败时返回);
	***/
	function imgConvert($imgurl,$newType,$newName,$newPath){
			$imgOldType='';		
		if(stripos($imgurl, 'http')===0){//是http的图片的时候
			$imgheaderInfo=get_headers($imgurl);
			$imgurlType=implode('', $imgheaderInfo);
			if(stripos($imgurlType,'image')===false){//验证是否是图片
				return 1;
			};
			foreach ($imgheaderInfo as $key => $value) {//获取图片原始格式
				if(stripos($value,'image')>0){
					$imgOldType=substr($value, stripos($value,'/')+1,strlen($value));
				}
			}
		}else{
			if(is_file($imgurl)){
				$file = fopen($imgurl, "rb");
				$bin = fread($file, 2); //只读2字节
				fclose($file);
				$strInfo = @unpack("C2chars", $bin);
				$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
				switch ($typeCode) {
				//case 7790: $imgOldType = 'exe'; break;
				//case 7784: $imgOldType = 'midi'; break;
				//case 8297: $imgOldType = 'rar'; break;
				case 255216: $imgOldType = 'jpeg'; break;
				case 7173: $imgOldType = 'gif'; break;
				case 6677: $imgOldType = 'bmp'; break;
				case 13780: $imgOldType = 'png'; break;
				case 8273:$imgOldType='webp';break;
				default: return 2;
				break;
				}
			}
		}


//		var_dump($imgOldType);
		$imgOldNameWithType=substr($imgurl,strripos($imgurl,'/')+1,strlen($imgurl));//获取图片原始名字;
		$imgOldName=substr($imgOldNameWithType, 0,strripos($imgOldNameWithType,'.'));
//		var_dump($imgOldName);

		if(!isset($newType)){//新图片类型
			$newType=$imgOldType;
		}
		//var_dump($imgheaderInfo);
		if(!isset($newPath)){//图片新路径
			$newPath='imageconvert/'.$newType.'/';
		}
		if(!file_exists($newPath)){
			mkdir($newPath);
		}
		if(!isset($newName)){//新图片名称
			$newName=$imgOldName;
		}
		//如果图片已经存在则文件名后加1;
		if(file_exists($newPath.$newName.'.'.$newType)) {
			$count=1;
			for ($i=1; file_exists($newPath.$newName.'('.$i.').'.$newType) ; $i++) {
			//var_dump($newPath.$newName.'('.$i.').'.$newType);
				$count++;
			}
			$newName=$newName.'('.$count.')';//最终文件名
		}
//			var_dump($imgOldType);
		switch ($imgOldType) {
			case 'jpeg':
				$imgTemSource=imagecreatefromjpeg($imgurl);
				switch ($newType) {
					case 'jpeg':
						imagejpeg($imgTemSource,$newPath.$newName.'.'.$newType,80);
						break;
					case 'webp':
						imagewebp($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					case 'png':
						imagepng($imgTemSource,$newPath.$newName.'.'.$newType,2);
						break;
					case 'gif':
						imagegif($imgTemSource,$newPath.$newName.'.'.$newType);
						break;
					case 'wbmp':
						imagewbmp($imgTemSource,$newPath.$newName.'.'.$newType);
					case 'xbm':
						imagexbm($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					default:
						# code...
						break;
				}
				break;
			case 'webp':
				$imgTemSource=imagecreatefromwebp($imgurl);
				switch ($newType) {
					case 'jpeg':
						imagejpeg($imgTemSource,$newPath.$newName.'.'.$newType,80);
						break;
					case 'webp':
						imagewebp($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					case 'png':
						imagepng($imgTemSource,$newPath.$newName.'.'.$newType,2);
						break;
					case 'gif':
						imagegif($imgTemSource,$newPath.$newName.'.'.$newType);
						break;
					case 'wbmp':
						imagewbmp($imgTemSource,$newPath.$newName.'.'.$newType);
					case 'xbm':
						imagexbm($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					default:
						# code...
						break;
				}
				break;
			case 'png':
				$imgTemSource=imagecreatefrompng($imgurl);
				switch ($newType) {
					case 'jpeg':
						imagejpeg($imgTemSource,$newPath.$newName.'.'.$newType,80);
						break;
					case 'webp':
						imagewebp($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					case 'png':
						imagepng($imgTemSource,$newPath.$newName.'.'.$newType,2);
						break;
					case 'gif':
						imagegif($imgTemSource,$newPath.$newName.'.'.$newType);
						break;
					case 'wbmp':
						imagewbmp($imgTemSource,$newPath.$newName.'.'.$newType);
					case 'xbm':
						imagexbm($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					default:
						# code...
						break;
				}
				break;
			case 'gif':
				$imgTemSource=imagecreatefromgif($imgurl);
				switch ($newType) {
					case 'jpeg':
						imagejpeg($imgTemSource,$newPath.$newName.'.'.$newType,80);
						break;
					case 'webp':
						imagewebp($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					case 'png':
						imagepng($imgTemSource,$newPath.$newName.'.'.$newType,2);
						break;
					case 'gif':
						imagegif($imgTemSource,$newPath.$newName.'.'.$newType);
						break;
					case 'wbmp':
						imagewbmp($imgTemSource,$newPath.$newName.'.'.$newType);
					case 'xbm':
						imagexbm($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					default:
						# code...
						break;
				}
				break;
			case 'wbmp':
				$imgTemSource=imagecreatefromwbmp($imgurl);
				switch ($newType) {
					case 'jpeg':
						imagejpeg($imgTemSource,$newPath.$newName.'.'.$newType,80);
						break;
					case 'webp':
						imagewebp($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					case 'png':
						imagepng($imgTemSource,$newPath.$newName.'.'.$newType,2);
						break;
					case 'gif':
						imagegif($imgTemSource,$newPath.$newName.'.'.$newType);
						break;
					case 'wbmp':
						imagewbmp($imgTemSource,$newPath.$newName.'.'.$newType);
					case 'xbm':
						imagexbm($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					default:
						# code...
						break;
				}
				break;
			case 'xbm':
				$imgTemSource=imagecreatefromxbm($imgurl);
				switch ($newType) {
					case 'jpeg':
						imagejpeg($imgTemSource,$newPath.$newName.'.'.$newType,80);
						break;
					case 'webp':
						imagewebp($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					case 'png':
						imagepng($imgTemSource,$newPath.$newName.'.'.$newType,2);
						break;
					case 'gif':
						imagegif($imgTemSource,$newPath.$newName.'.'.$newType);
						break;
					case 'wbmp':
						imagewbmp($imgTemSource,$newPath.$newName.'.'.$newType);
					case 'xbm':
						imagexbm($imgTemSource,$newPath.$newName.'.'.$newType);
						# code...
						break;
					default:
						# code...
						break;
				}
				break;
			
			default:
				return 2;
				# code...
				break;
		}
		imagedestroy($imgTemSource);
		$arrImgInfo = array('newType' => $newType,'newName'=>$newName,'newPath'=>$newPath);
		return $arrImgInfo;
		//var_dump(preg_match('image',);

	}
	var_dump(imgConvert($_SERVER['DOCUMENT_ROOT'].'/images/1.webp'));
?>

转换函数中转为webp格式的时候有时会出现转换成功,但chrome打开空白的情况;等php升级到7.0以后再试;原创文章转载请注明出处:www.lpsjj.cn