这个类的功能是用来对图片进行处理和拼接。
简单来说吧。目前做的是这样一个功能。
现将给定的一组图片,定比例缩放。然后进行拼接成一个大图。
用途是处理手机页面。
我目前的这个小应用就是针对淘宝的详情页进行图片的拼接处理,放在手机详情页中。
这个很容易看出来就是创建图像资源的方法。返回的也是图像资源。
/**
*@description: 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!');
}
}
创建一个图像,其实也是封装了官方的。imagecreatetruecolor方法。它返回的是图象资源。
/**
*@description: create truecolor pic
*@param: $picName string this is pictrue name
*/
protected function create($imgBgWidth,$imgBgheight,$picName)
{
$im = @imagecreatetruecolor($imgBgWidth,$imgBgheight) or die('Cannot Initialize new GD image stream');
if(imagejpeg($im,$picName))
{
echo "create bgpic ".$picName." success!<br />";
return $im;
}
else
{
echo "create bgpic ".$picName." faile!<br />";
return 0;
}
}
按照固定宽度进行缩放。
/**
*@description: 宽度给定,高度自适应。如果图片宽度小于规定,那么居中。
*/
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; //背景高度
$this->create($imgBgWidth,$imgBgheight,$picName);
$imgBgSource = Imagecreatefromjpeg($picName);
$mark = imagecopyresized($imgBgSource,$imgSource,0,0,0,0,480,$imgBgheight,$imgWidth,$imgHeight);
}
else
{
$imgBgWidth = $width;
$imgBgheight = $imgHeight;
$this->create($imgBgWidth,$imgBgheight,$picName);
$imgBgSource = Imagecreatefromjpeg($picName);
$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';
}
return $arr;
}
else
{
exit("param type is not array");
}
}
最后是将图片进行拼接。
/**
*@description: join the pictrue with height.
*@param: imgUrl array the pictrue url.
*/
function joinPic($imgUrl,$width)
{
if(is_array($imgUrl))
{
$bgHeight = 0;
foreach($imgUrl as $k => $v)
{
$im = $this->createImgSource($v);
$picWidth = imagesx($im);
$picHeight = imagesy($im);
$bgHeight = $picHeight+$bgHeight;
}
$bgWidth = $width;
$picName = $this->dir."longBg.jpg"; //name space
$this->create($bgWidth,$bgHeight,$picName);
$imgBgSource = Imagecreatefromjpeg($picName); //create long bg。
$addHeight = 0;
foreach($imgUrl as $k => $v)
{
$im2 = $this->createImgSource($v);
$picWidth = imagesx($im2);
$picHeight = imagesy($im2);
imagecopy($imgBgSource,$im2,0,$addHeight,0,0,$picWidth,$picHeight);
$addHeight = $addHeight+$picHeight;
}
Imagejpeg($imgBgSource, $this->dir.'pin.jpg');
}
}
最后贴出所有的源程序。这个是整个类的。
<?php
/**
*@author: Raytang
*@email: pengli@caisangzi.com
*@description: use picture class with the standard width and height
for the mobile
*@time: 2014-3-6 13:31:14
*/
class Picture
{
private $src;
private $dir;
//private $width; //standard needed
function __construct($src,$dir)
{
$this->src = $src;
//$this->width = $width;
$this->dir = $dir;
}
/**
*@description: 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!');
}
}
/**
*@description: 宽度给定,高度自适应。如果图片宽度小于规定,那么居中。
*/
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; //背景高度
$this->create($imgBgWidth,$imgBgheight,$picName);
$imgBgSource = Imagecreatefromjpeg($picName);
$mark = imagecopyresized($imgBgSource,$imgSource,0,0,0,0,480,$imgBgheight,$imgWidth,$imgHeight);
}
else
{
$imgBgWidth = $width;
$imgBgheight = $imgHeight;
$this->create($imgBgWidth,$imgBgheight,$picName);
$imgBgSource = Imagecreatefromjpeg($picName);
$width1 = ($width-$imgWidth)/2; //center
$width2 = $imgWidth; //center
$mark = imagecopyresized($imgBgSource,$imgSource,$width1,0,0,0,$width2,$imgBgheight,$imgWidth,$imgHeight);
}
print_r($imgBgSource);
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';
}
return $arr;
}
else
{
exit("param type is not array");
}
}
/**
*@description: create truecolor pic
*@param: $picName string this is picture name
*/
protected function create($imgBgWidth,$imgBgheight,$picName)
{
$im = @imagecreatetruecolor($imgBgWidth,$imgBgheight) or die('Cannot Initialize new GD image stream');
$white = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $white);
if(imagejpeg($im,$picName))
{
echo "create bgpic ".$picName." success!<br />";
return $im;
}
else
{
echo "create bgpic ".$picName." faile!<br />";
return 0;
}
}
/**
*@description: join the picture with height.
*@param: imgUrl array the picture url.
*/
function joinPic($imgUrl,$width)
{
if(is_array($imgUrl))
{
$bgHeight = 0;
foreach($imgUrl as $k => $v)
{
$im = $this->createImgSource($v);
$picWidth = imagesx($im);
$picHeight = imagesy($im);
$bgHeight = $picHeight+$bgHeight;
}
$bgWidth = $width;
$picName = $this->dir."longBg.jpg"; //name space
$this->create($bgWidth,$bgHeight,$picName);
$imgBgSource = Imagecreatefromjpeg($picName); //create long bg。
$addHeight = 0;
foreach($imgUrl as $k => $v)
{
$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');
return $mark;
}
else
{
exit("the param type error; function[joinPic]");
}
}
}
?>
说一下这个程序可能会出现的问题,
因为这个程序属于一个偏重于测试的程序,所以我基本上为了看到结果每一步都进行了图片的生成。
如果10000个用户用这个程序的话就会出现问题了。生成了太多的图片,
所以需要去修改它,不让生成图片直接用资源类型进行处理会比较好。
所以我会在下一个版本进行改造。
最后修改(2014年3月14日9:34:17)