图片处理(二)

我说过了,上一个程序存在一个问题就是会生成一些图片。(上一个程序地址:http://blog.csdn.net/pengli2014/article/details/20768921)

那么就影响了整体的速度。

ok,闲话少说我进行改造。

<?php
/**
 *@author:		Raytang
 *@email:		pengli@caisangzi.com
 *@destract:	这是对上一个picture的改进,目的是直接操作图片资源,而不是生成之后在操作,原因很简单,节省服务器的资源。
 *@time:		2014-3-8 10:56:52
 */
class Picture2
{
	private $src;
	private $dir;
	//private $width; //standard needed

	function __construct($src,$dir)
	{
		$this->src = $src;
		//$this->width = $width;
		$this->dir = $dir;
	}

	/**
	 *@distract:	create image source
	 *@param:		image url
	 */
	protected function createImgSource($imgUrl)
	{
		//var_dump($imgUrl);
		$source	= getimagesize($imgUrl);
		switch($source[2])
		{
			case 1 :
				if(!function_exists("imagecreatefromgif")) 
				{
					echo "the GD can't support .gif, please use .jpeg or .png! <a href='javascript:history.back();'>back</a>";
					exit();
				}
				return $imgSource = imagecreatefromgif($imgUrl);
				break;
			case 2 :
				if(!function_exists("imagecreatefromjpeg"))
				{
					echo "the GD can't support .jpeg, please use other picture! <a href='javascript:history.back();'>back</a>";
					exit();
				}
				return $imgSource = imagecreatefromjpeg($imgUrl); 
				break;
			case 3 :
				return $imgSource = imagecreatefrompng($imgUrl); 
				break;

			default:
				exit('pic type error!');
		}
	}

	/**
	 *@distract:	宽度给定,高度自适应。如果图片宽度小于规定,那么居中。
	 */
	function createPicWithByWidth($width)
	{
		$arr = array();
		if(is_array($this->src))
		{
			$src = $this->src;
			foreach( $src as $k => $v )
			{
				//var_dump($v);
				$imgSource		= $this->createImgSource($v);
				$picName		= $this->dir."bg.jpg";//name space
				$source			= getimagesize($v);
				$imgHeight		= $source['1']; //src height
				$imgWidth		= $source['0']; //src width
				if($imgWidth>$width)
				{ 
					$multiple		= ($width)/$imgWidth; //宽度倍数。
					$imgBgWidth		= $width;
					$imgBgheight	= $imgHeight*$multiple; //背景高度
					$imgBgSource	= $this->create($imgBgWidth,$imgBgheight);//这里同样也不需要生成路径。
					//$imgBgSource = Imagecreatefromjpeg($picName);
					$mark = imagecopyresized($imgBgSource,$imgSource,0,0,0,0,$width,$imgBgheight,$imgWidth,$imgHeight);
				}
				else
				{
					$imgBgWidth		= $width;
					$imgBgheight	= $imgHeight;
					$imgBgSource	= $this->create($imgBgWidth,$imgBgheight);//这里同样也不需要生成路径。
					$width1			= ($width-$imgWidth)/2; //center
					$width2			= $imgWidth;	//center
					$mark			= imagecopyresized($imgBgSource,$imgSource,$width1,0,0,0,$width2,$imgBgheight,$imgWidth,$imgHeight);
				}
				
				if($mark)
				{
					echo "create newpic source success.<br />";
				}
				else
				{
					echo "create newpic source faile.<br />";
				}
				//imagejpeg($imgBgSource,$this->dir.'pic'.$k.'.jpg'); //同样这一句也不需要了,不需要生成,直接返回他的资源。
				//$arr[] = $this->dir.'pic'.$k.'.jpg'; //这一句就需要修改了,返回的是资源数组。
				$arr[] = $imgBgSource;
			}
			return $arr;
		}
		else
		{
			exit("param type is not array");
		}
	}

	/**
	 *@distract:	create truecolor pic 
	 *@param:		$picName string this is picture name //实际上picName这个参数就没有必要了,因为我不需要在生成图片了。
	 *@return:		imageResource
	 */
	protected function create($imgBgWidth,$imgBgheight)
	{
		$im = @imagecreatetruecolor($imgBgWidth,$imgBgheight) or die('Cannot Initialize new GD image stream');
		$white = imagecolorallocate($im, 255, 255, 255);
		imagefill($im, 0, 0, $white);//填充背景色。
		if($im)
		{
			echo "create bgpic success!<br />";
			return $im;
		}
		else
		{
			echo "create bgpic faile!<br />";
			return 0;
		}
		/*
		//实际上这一段生成图片的代码就没有必要了。
		if(imagejpeg($im,$picName))
		{
			echo "create bgpic ".$picName." success!<br />";
			return $im;
		}
		else
		{
			echo "create bgpic ".$picName." faile!<br />";
			return 0;
		}
		*/
	}

	/**
	 *@distract:	join the picture with height.//我们把之前的picUrl全变成以图像资源位接入参数的变量。
	 *@param:		imgSource	array		imgSource.//这里会变成资源,如要目的是介绍之前生成的压缩文件的资源。然后通过资源进行拼接。
	 *
	 */
	function joinPic($imgSource,$width)
	{
		if(is_array($imgSource))
		{
			$bgHeight = 0;
			foreach($imgSource as $k => $im)
			{
				//$im = $this->createImgSource($v);
				$picWidth	= imagesx($im);
				$picHeight	= imagesy($im);
				$bgHeight	= $picHeight+$bgHeight;
			}
			$bgWidth	= $width;
			/**
			 *@distract:	这里下面的生成图片都不需要了。我们create用了返回值。
			 */
			/*
			$picName	= $this->dir."longBg.jpg";			//name space
			$this->create($bgWidth,$bgHeight,$picName);
			$imgBgSource = Imagecreatefromjpeg($picName);	//create long bg。
			*/
			$imgBgSource = $this->create($bgWidth,$bgHeight);
			$addHeight = 0;
			foreach($imgSource as $k => $im2)//这一段代码同上理改造
			{
				//$im2 = $this->createImgSource($v);
				$picWidth	= imagesx($im2);
				$picHeight	= imagesy($im2);
				imagecopy($imgBgSource,$im2,0,$addHeight,0,0,$picWidth,$picHeight);
				$addHeight = $addHeight+$picHeight;
			}
			$mark = Imagejpeg($imgBgSource, $this->dir.'pin.jpg');
			/**
			 *这里需不需要生成,如果不生成,那么我的图片资源和图片的二进制有什么关系,如果有关系,我就不需要生成了,直接存2进制的图像。
			 */
			return $mark;
		}
		else
		{
			exit("the param type error; function[joinPic]");
		}
		
	}

	/*
	 *@description:	图片变为2进制形式。用于存储到数据库中。
	 */
	function picToCode($picUrl)
	{
		$res = file_get_contents($picUrl);//这里是获得的图片2进制。
		return $res;
	}

}

?>

除了最后一次生成了最后的拼接图片之外,其他全部用资源进行操作。

避免更多的图像浪费了服务器的空间。

粘一段php应用这个类的代码。

因为我是用的淘宝的api的接口,所以我就不全部粘了。

后面我会去专门写一些关于淘宝的api的调用方法。

$imgs = array(
		"http://img03.taobaocdn.com/imgextra/i3/444857673/T2kr2kXApXXXXXXXXX-444857673.gif",
		"http://img01.taobaocdn.com/imgextra/i1/444857673/T22SfkXClXXXXXXXXX-444857673.jpg",
		"http://img03.taobaocdn.com/imgextra/i3/444857673/T22gMPXkRaXXXXXXXX_!!444857673.jpg"
	);

$width = 480;
$dir = "temp_pic/";
$picObject = new Picture2($imgs,$dir);
$picArr = $picObject->createPicWithByWidth($width);
$mark = $picObject->joinPic($picArr,$width);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值