图片处理(多图压缩、按照定宽拼接大图)
2014-03-08 10:45
429 查看
这个类的功能是用来对图片进行处理和拼接。
简单来说吧。目前做的是这样一个功能。
现将给定的一组图片,定比例缩放。然后进行拼接成一个大图。
用途是处理手机页面。
我目前的这个小应用就是针对淘宝的详情页进行图片的拼接处理,放在手机详情页中。
这个很容易看出来就是创建图像资源的方法。返回的也是图像资源。
创建一个图像,其实也是封装了官方的。imagecreatetruecolor方法。它返回的是图象资源。
按照固定宽度进行缩放。
最后是将图片进行拼接。
最后贴出所有的源程序。这个是整个类的。
说一下这个程序可能会出现的问题,
因为这个程序属于一个偏重于测试的程序,所以我基本上为了看到结果每一步都进行了图片的生成。
如果10000个用户用这个程序的话就会出现问题了。生成了太多的图片,
所以需要去修改它,不让生成图片直接用资源类型进行处理会比较好。
所以我会在下一个版本进行改造。
最后修改(2014年3月14日9:34:17)
简单来说吧。目前做的是这样一个功能。
现将给定的一组图片,定比例缩放。然后进行拼接成一个大图。
用途是处理手机页面。
我目前的这个小应用就是针对淘宝的详情页进行图片的拼接处理,放在手机详情页中。
这个很容易看出来就是创建图像资源的方法。返回的也是图像资源。
/** *@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)
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- php xml 入门学习资料
- PHP简单系统数据添加以及数据删除模块源文件下载