生成缩略图保存数据库
<?php
# Constants
define("IMAGE_BASE", './');
define("MAX_WIDTH", 150);
define("MAX_HEIGHT", 150);
# Get image locationstr_replace('..', '', $_SERVER['QUERY_STRING']);
$image_file = 't.jpg';
$image_path = IMAGE_BASE . "$image_file";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefrompng($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width =150; //floor($scale*$width);
$new_height =150;// floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70 );
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>
先来看看效果图吧:
下面我们来看看php怎么样实现绘图这个功能的.
<?php
/***
* @project Bar Graph Program
* @license GPL
* @package
* @file GrapBar.php
* @date 2007-4-3
* @version 1.0
* @last modified
* 定义 柱状图(柱形图) 类
*
* 注意,使用前请确保字体路径存在并允许访问,如果出错,还要检查在php.ini配置中的open_basedir项,如果没此路径请添加,或在程序中设置包含
*
* 智能化的柱状图程序,用于报表等
*
***/
define("DEFAULT_FONT_PATH", "c:/windows/fonts/simhei.ttf");
class SingleBar
{
private $_x;
private $_y;
private $_h;
public $_l = 50;
private $_w = null;
private $_srcPoints = array();
private $_points = array();
public function __construct($x, $y, $h, $l = 50, $w = null)
{
$this->_x = $x;
$this->_y = $y;
$this->_h = $h;
$this->_l = $l;
$this->_w = $w;
$this->_srcPoints = $this->getSrcPoints();
$this->_points = $this->getPoints();
}
public function getSrcPoints()
{
return array(
array($this->_x , $this->_y),
array($this->_x+$this->_l , $this->_y),
array($this->_x+(1.35*$this->_l), $this->_y-(0.35*$this->_l)),
array($this->_x+(0.35*$this->_l), $this->_y-(0.35*$this->_l)),
array($this->_x , $this->_y+$this->_h),
array($this->_x+$this->_l , $this->_y+$this->_h),
array($this->_x+(1.35*$this->_l), $this->_y+$this->_h-(0.35*$this->_l))
);
}
public function getPoints()
{
$points = array();
foreach($this->_srcPoints as $key => $val)
{
$points[] = $val[0];
$points[] = $val[1];
}
return $points;
}
public function getTopPoints()
{
return array_slice($this->_points, 0, 8); //顶坐标
}
public function getBelowPoints()
{
return array_merge(array_slice($this->_points, 0, 2), array_slice($this->_points, 8, 4), array_slice($this->_points, 2, 2)); //下坐标
}
public function getRightSidePoints()
{
return array_merge(array_slice($this->_points, 2, 2), array_slice($this->_points, 10, 4), array_slice($this->_points, 4, 2)); //右侧坐标
}
public function draw($image, $topColor, $belowColor, $sideColor, $borderColor = null, $type = 'LEFT')
{
if (is_null($borderColor))
{
$borderColor = 0xcccccc;
}
$top_rgb = $this->getRGB($topColor);
$below_rgb = $this->getRGB($belowColor);
$side_rgb = $this->getRGB($sideColor);
$top_color = imagecolorallocate($image, $top_rgb['R'], $top_rgb['G'], $top_rgb['B']);
$below_color = imagecolorallocate($image, $below_rgb['R'], $below_rgb['G'], $below_rgb['B']);
$side_color = imagecolorallocate($image, $side_rgb['R'], $side_rgb['G'], $side_rgb['B']);
imagefilledpolygon($image, $this->getTopPoints(), 4, $top_color); //画顶面
imagepolygon($image, $this->getTopPoints(), 4, $borderColor); //画顶面边线
imagefilledpolygon($image, $this->getBelowPoints(), 4, $below_color); //画下面
imagepolygon($image, $this->getBelowPoints(), 4, $borderColor); //画下面边线
if ($type == 'LEFT')
{
imagefilledpolygon($image, $this->getRightSidePoints(), 4, $side_color); //画右侧面
imagepolygon($image, $this->getRightSidePoints(), 4, $borderColor); //画侧面边线
}
}
public function getRGB($color)
{
$ar = array();
$color = hexdec($color);
$ar['R'] = ($color>>16) & 0xff;
$ar['G'] = ($color>>8) & 0xff;
$ar['B'] = ($color) & 0xff;
return $ar;
}
}
class Bar
{
private $_W;
private $_H;
private $_bgColor = "ffffff";
private $_barHeights = array();
private $_barTexts = array();
private $_barColors = array();
public $_title;
public $_paddingTop = 30;
public $_paddingBottom = 100;
public $_paddingLeft = 45;
public $_paddingRight = 2;
public $_barL = 50;
public $image;
public function __construct($imgW, $imgH, $barHeights, $barTexts = null, $barColors = null)
{
$this->_W = $imgW;
$this->_H = $imgH;
$this->_barHeights = $barHeights;
$this->_barTexts = $barTexts;
$this->_barColors = $barColors;
$this->_paddingBottom = $this->resetPaddingBottom();
$this->_H = $this->resetHeight();
$this->image = imagecreatetruecolor($this->_W, $this->_H);
}
public function stroke()
{
$this->drawBg();
$this->drawBars();
$this->drawTitle();
$this->drawLables();
ob_start();
//header("Content-type: image/png");
//imagepng($this->image);
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_JPEG));
imagejpeg($this->image);
imagedestroy($this->image);
}
public function drawBg()
{
$img_w = $this->_W;
$img_h = $this->_H;
$paddingTop = $this->_paddingTop;
$paddingBottom = $this->_paddingBottom;
$paddingLeft = $this->_paddingLeft;
$paddingRight = $this->_paddingRight;
$rgb = $this->getRGB($this->_bgColor);
$bg = imagecolorallocate($this->image,$rgb['R'], $rgb['G'], $rgb['B']);
imagefilledrectangle($this->image, 0, 0, $img_w, $img_h, $bg);
$side_bg = imagecolorallocatealpha($this->image, 220, 220, 220, 75);
$side_bg2 = imagecolorallocate($this->image, 220, 220, 220);
$border_color = imagecolorallocate($this->image, 190, 190, 190);
$line_color = imagecolorallocate($this->image, 236, 236, 236);
$dial_color = imagecolorallocate($this->image, 131, 131, 131);
$x1 = $paddingLeft;
$y1 = $paddingTop;
$x2 = $img_w - $paddingRight;
$y2 = $img_h - $paddingBottom;
imagerectangle($this->image, $x1, $y1, $x2, $y2, $border_color);
imagefilledpolygon($this->image, array($x1-5,$y1+10, $x1-5,$y2+10, $x1,$y2, $x1,$y1), 4, $side_bg);
imagepolygon($this->image, array($x1-5,$y1+10, $x1-5,$y2+10, $x1,$y2, $x1,$y1), 4, $border_color);
imagefilledpolygon($this->image, array($x1-5,$y2+10, $x2-5,$y2+10, $x2,$y2, $x1,$y2), 4, $side_bg);
imagepolygon($this->image, array($x1-5,$y2+10, $x2-5,$y2+10, $x2,$y2, $x1,$y2), 4, $border_color);
imageline($this->image, $x1, $y2, $x2, $y2, $side_bg2);
$total_h = $img_h - $paddingTop - $paddingBottom;
$every_h = $total_h/11;
for($i=1; $i<=10; $i++)
{
imageline($this->image, $x1, $y1+($every_h*$i), $x2, $y1+($every_h*$i), $line_color); //画网线
}
$max_h = max($this->_barHeights);
for($i=1; $i<=10; $i++)
{
$value = $max_h - (($max_h/10)*($i-1));
$value = strval($value);
$str_w = strlen($value)*5;
imageline($this->image, $x1-5-3, $y1+10+($every_h*$i), $x1-3+1, $y1+10+($every_h*$i), $dial_color); //画刻度线
imagestring($this->image, 3, $x1-5-3-$str_w-1, $y1+10+($every_h*$i)-5, $value, 0x000000);
}
}
public function drawBars()
{
$counts = count($this->_barHeights);
if (empty($this->_barColors))
{
$color = $this->setColor();
$this->_barColors = array_slice($color, 0, $counts);
//shuffle($this->_barColors);
}
$every_w = ($this->_W - $this->_paddingLeft - $this->_paddingRight)/$counts; //每一段宽
$barL = $every_w;
$barL = ($barL > $this->_barL*1.35+6) ? $this->_barL : $barL/1.35 - 6;
$max_h = max($this->_barHeights);
$ruler_h = $this->_H - $this->_paddingTop - $this->_paddingBottom; //标尺总高度
$stander_h = $ruler_h - ($ruler_h/11); //标尺10等分的高度
$i = 0;
foreach ($this->_barHeights as $val)
{
$h = ($stander_h/$max_h)*$val;
$x = $this->_paddingLeft + ($every_w*$i) + (($every_w - ($barL*1.35))/2);;
$y = $this->_H - $this->_paddingBottom + 10 - $h;
//$t_color = $this->_barColors[$i];
$b_color = $this->_barColors[$i];
//$s_color = $this->_barColors[$i];
$rgb = $this->getRGB($this->_barColors[$i]);
$R = $rgb['R'] * 0.7;
$G = $rgb['G'] * 0.7;
$B = $rgb['B'] * 0.7;
$c1 = $R > 0 ? dechex($R) : '00';
$c2 = $G > 0 ? dechex($G) : '00';
$c3 = $B > 0 ? dechex($B) : '00';
$t_color = $b_color;
$s_color = $c1. $c2 . $c3;
$SingleBar = new SingleBar($x, $y, $h, $barL);
$SingleBar->draw($this->image, $t_color, $b_color, $s_color);
$i++;
}
}
public function drawTitle()
{
if (empty($this->_title))
{
return;
}
$font = 5;
$font_w = imagefontwidth($font);
$len = strlen($this->_title);
$x = ($this->_W + $this->_paddingLeft - $this->_paddingRight)/2;
$x -= ($len*$font_w)/2;
$y = ($this->_paddingTop - $font_w)/2 + 12;
//imagestring($this->image, $font, $x, $y, $title, 0x000000);
imagettftext($this->image, 12, 0, $x, $y, 0x000000, DEFAULT_FONT_PATH, $this->_title);
}
public function drawLables()
{
$x1 = $this->_paddingLeft - 5;
$y1 = $this->_H - $this->_paddingBottom + 20;
$x2 = $this->_W - $this->_paddingRight;
$y2 = $this->_H - 10;
//imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, 0xffffff);
imagerectangle($this->image, $x1, $y1, $x2, $y2, 0x000000);
$space = 5;
$x = $x1 + 3;
$y = $y1 + 3;
foreach ($this->_barTexts as $key => $val)
{
$color = $this->_barColors[$key];
$rgb = $this->getRGB($color);
$bg = imagecolorallocate($this->image,$rgb['R'], $rgb['G'], $rgb['B']);
imagefilledrectangle($this->image, $x, $y, $x+12, $y+12, $bg); //绘12*12的矩形
imagerectangle($this->image, $x, $y, $x+12, $y+12, 0x000000); //绘12*12的矩形框
imagettftext($this->image, 12, 0, $x+12+3, $y+12, 0x000000, DEFAULT_FONT_PATH, $val);
$x += 12 + $space + (strlen($val)*8) + $space;
if ($x + (strlen($val)*8) >= $this->_W - $this->_paddingLeft - $this->_paddingRight)
{
$x = $x1 + 3;
$y = $y + 12 + 3;
}
}
}
public function resetPaddingBottom()
{
$ruler_w = $this->_W - $this->_paddingLeft - $this->_paddingRight;
$label_w = $this->getLableTotalWidth();
$lines = ceil($label_w / $ruler_w);
$h = 12 * $lines + (3 * ($lines + 1)) + 30;
return $h;
}
public function resetHeight()
{
$padding_bottom = $this->resetPaddingBottom();
if ($this->_H - $padding_bottom < 222)
{
return 222 + $padding_bottom;
}
return $this->_H;
}
public function getLableTotalWidth()
{
$counts = count($this->_barTexts);
$space = 5;
$total_len = 0;
foreach ($this->_barTexts as $val)
{
$total_len += strlen($val);
}
$tx_w = ($total_len * 9) + ((12 + 3 + $space) * $counts);
return $tx_w;
}
public function setBg($color)
{
$this->_bgColor = $color;
}
public function setTitle($title)
{
$this->_title = $title;
}
public function setColor()
{
$ar = array('ff', '00', '33', '66', '99', 'cc');
$color = array();
for ($i=0; $i<6; $i++)
{
for ($j=0; $j<6; $j++)
{
for($k=0; $k<6; $k++)
{
$color[] = $ar[$i] . $ar[$j] . $ar[$k];
}
}
}
$color2 = array();
for ($i=1; $i<216; $i += 4)
{
$color2[] = $color[$i];
}
return $color2;
}
public function getRGB($color)
{
$ar = array();
$color = hexdec($color);
$ar['R'] = ($color>>16) & 0xff;
$ar['G'] = ($color>>8) & 0xff;
$ar['B'] = ($color) & 0xff;
return $ar;
}
}
/***/
$bar = new Bar(500, 300, array(600, 300, 30, 500, 400, 250, 350, 360), array('AAAAA', 'BBBBB', 'CCCCC', 'DDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGGG', 'HHHHHHHHH'));
$bar->setTitle('打造完美柱状图!');
$bar->stroke();
/***/
?>
以前在奥索看见过很多这样的例子,各各方法复杂,请看如下例子实现图形计数器。在网页中使用请用 <script src="http://文件地址"></script>
<?
//存放计数的文本文件
$count="count.txt";
//计数器的位数,假如不填则默认为6位
$wei="";
//图像的URL路径
$tu="/tcount/images";
?>
<?
if ($wei=="") { $wei=6; }
if (!file_exists($count)) { $fp=fopen($count,"w "); fputs($fp,0,100); fclose($fp); }
if ($REMOTE_ADDR!=$ip) {
$fp=fopen($count,"r ");
$cc=fgets($fp,100);
$cc=trim($cc);
if ($cc=="") { $cou=1; }
else { $cou=$cc 1; }
rewind($fp);
fputs($fp,$cou,100);
fclose($fp);
}
setcookie("ip",$REMOTE_ADDR,time() 86400);
$fp=fopen($count,"r ");
$cou=fgets($fp,100);
$cou=trim($cou);
fclose($fp);
$w=strlen($cou);
while($w<$wei) {
$cou="0".$cou;
$w ;
}
$www=strlen($cou);
$i=0;
echo "document.write("<p align=center>");n";
while($i<$www) {
$ym=substr($cou,$i,1);
echo "document.write("<img src=$tu/$ym.gif>");n";
$i ;
}
?>
图片链接广告大概是最为常用的网络广告模式,但是多条广告的动态随机显示可不是那么简单的事情。PHP的文件上传函数提供了在这块468x60像素的区域(网页广告的标准尺寸)实现能够根据权值大小而随机显示广告的解决方案,从而避免了开发FTP程序或是ASP和Perl CAI中需要外挂扩展才能实现此项功能的弊端。
本例在PHP4 MySQL Apache for Win9x/2000下调试通过,共包括两部分:用来增加新广告的程序AddNewAd.php3以及显示广告的程序ShowAd.php3,都存放在服务器PHP文件目录下的“test/guanggao/”目录中。在guanggao目录中建立一个存放广告链接图片的子目录“adbanner/”。下面我们按步骤进行分析。
1.首先,建立MySQL数据库:guanggao
在Windows环境下,建立一个MySQL空数据库很简单,因为每个数据库都是存在于mysql/data/目录下的一个子目录,所以只要在该目录下新建子目录guanggao就可以了,这时数据库内还没有任何表和数据。
2.建立新增广告程序:AddNewAd.php3
设计思路:其中先用表单得到用户的新广告信息;然后用PHP函数copy()将链接图片(banner)文件上传到服务器adbanner目录下;最后将图片文件名、广告网址、说明、加权信息等写入建立的MySQL数据表ad中。该程序代码如下:
# AddNewAd.php3
PHP一个最大的优点就是它对新技术的支持非常轻易,这种语言的可扩展性使得开发人员能够很方便地添加新的模块,而且遍布世界的技术团体的支持和众多扩展模块的支持使得PHP已经成为功能最齐全的Web编程语言之 一。目前可得到的扩展模块已经能够使开发人员执行IMAP和POP3操作,可以动态产生图象和Shockwave Flash动画,进行信用卡验证,敏感数据的加密解密,还能够解析XML格式的数据。但这还不是全部,现在,又有一个新的模块可以与PHP进行绑定了,那就是PDFLib扩展模块,它能够让开发人员动态产生PDF(Adobe Portable Document Format)格式的文件,下面就先看看如何在PHP里使用这个模块。
为了能够使PHP具有操作PDF格式文档的能力,你必须先在你的系统里安装PDFLib扩展库,假如你使用的是Lunix系统,你可以从 http://www.pdflib.com/pdflib/index.html下载一个并进行编译,假如你使用的是Windows系统,那就更简单了,只需要下载一个编译好的PDFLib库,然后在PHP的配置文件里把相应的行的注释去掉即可。
extension=php_pdf.dll
假如是动态装载,也可以是参照下面的命令:
dl("php_pdf.dll");
此外,你还必须有一个Adobe Acrobat PDF阅读器,用来浏览PDF格式,假如你没有,你可以从http://www.adobe.com/免费下载。
一旦你做好了前期预备,就可以创建PDF文件了,下面就是一个简单的例子:
<?php
// 创建一个新的PDF文档句柄
$pdf = PDF_new();
// 打开一个文件
PDF_open_file($pdf, "PDFTest.pdf");
// 开始一个新页面(A4)
PDF_begin_page($pdf, 595, 842);
// 得到并使用字体对象
$arial = PDF_findfont($pdf, "Arial", "host", 1);
PDF_setfont($pdf, $arial, 10);
// 输出文字
PDF_show_xy($pdf, "This is an exam of PDF Documents, It is a good Lib,",50, 750);
PDF_show_xy($pdf, "If you like,please try yourself!", 50, 730);
// 结束一页
PDF_end_page($pdf);
// 关闭并保存文件
PDF_close($pdf);
?>
然后保存成PHP文件,在浏览器里进行浏览,PHP就会执行上面的代码,它产生一个新的PDF文件,并保存到指定的位置。
现在我们分析一下什么的代码,要使用PHP创建PDF文件,有四个步骤:1,创建文档句柄;2,注册文档的字体和颜色;3,用PDFLib提供的函数向文件句柄写文字或画图;4,保存文档。
相关文章
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
- 在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
- 某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
- mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
- 1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
- 这篇文章主要介绍了node.js如何操作MySQL数据库,帮助大家更好的进行web开发,感兴趣的朋友可以了解下...2020-10-29
- 这篇文章主要介绍了c#从数据库里取得数据并异步更新ui的方法,大家参考使用吧...2020-06-25
- yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2使用中的一些基本的增删改查操作。 User::find()->all(); //返回所有用户数据; User::findOne($id); //返回 主键...2015-11-24
- 1.用phpmyadmin创建数据库和数据表 创建数据库的时候,请将“整理”设置为:“utf8_general_ci” 或执行语句: 复制代码 代码如下:CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 创...2015-10-21
- 这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
- 这篇文章主要介绍了Linux 下使用shell脚本定时维护数据库,本文通过案例分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
- “主机,用户名,密码”得到连接、“数据库,sql,连接”得到结果,最后是结果的处理显示。当然,数据库连接是扩展库为我们完成的,我们能做的仅仅是处理结果而已。...2013-09-29
- 对数据加密分两种,一种是对数据库本身进行加密,另一种是对数据表中的数据进行加密,下面通过本文给大家介绍C#连接加密的Sqlite数据库的方法,感兴趣的朋友一起看看吧...2020-06-25
- 这篇文章主要为大家详细介绍了node.js从数据库获取数据的具体代码,nodejs可以获取具体某张数据表信息,感兴趣的朋友可以参考一下...2016-05-09
- 这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-16