php创建不失真高清图片实现代码
//创建图像
$im=imagecreatetruecolor(100,100);
//将背景设为红色
$red=imagecolorallocate($im,255,0,0);
imagefill($im,0,0,$red);
//输出图像
header('content-type: image/png');
imagepng($im);
imagedestroy($im);
/*
执行该代码,将生成背景为红色的图形。
*/
//代码二
//创建真彩色图像
$img=imagecreatetruecolor(400,400);
//通过循环执行操作
for($i=10;$i<=350;$i=$i+20)
{
//定义颜色
$color=imagecolorallocate($img,200,50,$i);
//画出椭圆
imageellips教程e($img,200,200,350,$i,$color);
}
//输出图像
header("content-type: image/png");
imagepng($img);
//销毁图像
imagedestroy($img);
/*
该代码的执行结果如图:22.7所示:
*/
//代码三
//创建真彩色图像
$img=imagecreatetruecolor(200,200);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$blue=imagecolorallocate($img,0,0,255);
//在图像上画图
imagearc($img,100,100,50,150,360,0,$red);
imagearc($img,100,100,150,50,0,360,$blue);
//输出图像
header("content-type: image/png");
imagepng($img);
//销毁图像
imagedestroy($img);
/*
该代码的的执行结果如图22.6所示:
*/
//实例四
//发送头文件
header("content-type: image/png");
//创建图像,如果失败输出内容
$im=imagecreatetruecolor(500,500); //创建图像
//定义背景颜色
$black=imagecolorallocate($im,0,0,0);
//定义线颜色
$color=imagecolorallocate($im,0,255,255);
//在图像上画出虚线
imageline($im,1,1,450,450,$color);
//输出图像文件
imagepng($im);
//销毁图像
imagedestroy($im);
在php教程如果想利用它图片处理函数就必须在php.ini里面的gd库开启哦,
*/
//发送头文件
header("content-type: image/png");
//创建图像,如果失败输出内容
$im=@imagecreate(150,50) or die("cannot initialize new gd image stream");
//定义背景颜色
$background_color=imagecolorallocate($im,255,255,255);
//定义文字颜色
$text_color=imagecolorallocate($im,233,14,91);
//在图像上画出文件
imagestring($im,3,5,5,"hello world",$text_color);
//输出图像文件
imagepng($im);
//销毁图像
imagedestroy($im);
/*
该代码的执行结果如图22.5所示:
*/
// 2图片等比例缩小
//定义一个文件
$filename='1.jpg';
//定义缩放百分比
$percent=0.5;
//输出头文件
header('content-type: image/jpeg');
//获取新的大小
list($width,$height)=getimagesize($filename);
$newwidth=$width * $percent;
$newheight=$height * $percent;
//创建图形区域,并载入图像
$thumb=imagecreatetruecolor($newwidth,$newheight);
$source=imagecreatefromjpeg($filename);
//重新调整大小
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//输出图像
imagejpeg($thumb);
/*
执行该代码,将把原图像缩放50%,并以新图像输出
*/
//在图片上写文字
//定义内容
$data='ivborw0kggoaaaansuheugaaabwaaaascamaaab/2u7waaaabl'.
'bmveuaaad///+l2z/daaaasuleqvr4xqwquqoaiaxc2/0vxzdr'.
'ex4ijtrkb7lobnustxsb0jixiamssqnwlsv+wulf4avk9flq2r'.
'8a5hse35q3eo2xp1a1wqkzsgetvdtkdqaaaabjru5erkjggg==';
//对内容进行base64编码
$data=base64_decode($data);
//根据字符串新建图像
$im=imagecreatefromstring($data);
if($im!== false)
{
//如果成功创建,则输出图像
header('content-type: image/png');
imagepng($im);
}
else
{
//如果创建失败,则输出内容
echo 'an error occured.';
}
/*
该代码的执行结果如图:22.4所示:
*/
//在图片上写文字
header("content-type: image/png");
//创建图像,如果失败输出内容
$im=@imagecreate(100,50) or die("cannot initialize new gd image stream");
//定义背景颜色
$background_color=imagecolorallocate($im,255,255,255);
//定义文字颜色
$text_color=imagecolorallocate($im,233,14,91);
//在图像上画出文件
imagestring($im,1,5,5,"a simple text string",$text_color);
//输出图像文件
imagepng($im);
//销毁图像
imagedestroy($im);
/*
执行该代码将生成一个jpeg图像。
并输出指定字符串
*/
*/
//创建图像
$image=imagecreatetruecolor(300,300);
//定义画饼状图所需要的颜色
$white=imagecolorallocate($image, 0xff, 0xff, 0xff);
$gray=imagecolorallocate($image, 0xc0, 0xc0, 0xc0);
$darkgray=imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy=imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy=imagecolorallocate($image, 0x00, 0x00, 0x50);
$red=imagecolorallocate($image, 0xff, 0x00, 0x00);
$darkred=imagecolorallocate($image, 0x90, 0x00, 0x00);
/*
imagecolorallocate -- 为一幅图像分配颜色 说明 int imagecolorallocate ( resource image, int red, int green, int blue) imagecolorallocate() 返回一个标识符,代表了由给定的 rgb 成分组成的颜色。image 参数是 imagecreate() 函数的返回值。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xff。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。
*/
//画图
for($i=160;$i > 150; $i--)
{
imagefilledarc($image, 150, $i, 200, 80, 0, 45, $darknavy, img_arc_pie);
imagefilledarc($image, 150, $i, 200, 80, 45, 75 , $darkgray, img_arc_pie);
imagefilledarc($image, 150, $i, 200, 80, 75, 360 , $darkred, img_arc_pie);
}
imagefilledarc($image, 150, 150, 200, 80, 0, 45, $navy, img_arc_pie);
imagefilledarc($image, 150, 150, 200, 80, 45, 75 , $gray, img_arc_pie);
imagefilledarc($image, 150, 150, 200, 80, 75, 360 , $red, img_arc_pie);
header('content-type: image/png');
imagepng($image);
imagedestroy($image);
/*
imagefilledarc()函数,可以在画出椭圆弧的同时,使用指定颜色对其进行填充。使用此函数的这种功能,就可以很简单的画出一个用于统计的饼状图。下面演示imagefilledarc()函数的使用方法
该代码的执行结果如图22.8所示:
*/
//在图片画线条
//创建真彩色图像
$img=imagecreatetruecolor(300,300);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
//在图像上画椭圆
imagefilledellips教程e($img,150,150,250,100,$red);
imagefilledellipse($img,150,150,100,250,$green);
//输出图像
header("content-type: image/png");
imagepng($img);
//销毁图像
imagedestroy($img);
/*
该代码的执行结果如图22.9所示:
*/
int exif_imagetype ( string $filename )
1 imagetype_gif
2 imagetype_jpeg
3 imagetype_png
4 imagetype_swf
5 imagetype_ps教程d
6 imagetype_bmp
7 imagetype_tiff_ii (intel byte order)
8 imagetype_tiff_mm (motorola byte order)
9 imagetype_jpc
10 imagetype_jp2
11 imagetype_jpx
12 imagetype_jb2
13 imagetype_swc
14 imagetype_iff
15 imagetype_wbmp
16 imagetype_xbm
*/
$img="image.gif"; //定义文件
if(exif_imagetype($img)!=imagetype_gif) //判断文件类型
{
echo "指定的图片不是gif图片"; //输出结果
}
else
{
echo "指定的图片是gif图片";
}
/*
array exif_read_data ( string $filename [, string $sections = null [, bool $arrays = false [, bool $thumbnail = false ]]] )
*/
echo "test1.jpg:<br/>n"; //输出文件名
$exif=exif_read_data('tests/test1.jpg','ifd0'); //读取文件1信息
//根据结果判断
echo $exif===false ? "no header data found.<br/>n":"image contains headers<br/>n";
$exif=exif_read_data('tests/test2.jpg',0,true); //读取文件2信息
echo "test2.jpg:<br/>n"; //输出文件名
foreach($exif as $key=>$section) //循环读取信息
{
foreach($section as $name=>$val)
{
echo "$key.$name:$val<br/>n";
}
}
/*
test1.jpg:
no header data found.
test2.jpg:
file.filename: test2.jpg
file.filedatetime: 1017666176
file.filesize: 1240
file.filetype: 2
file.sectionsfound: any_tag, ifd0, thumbnail, comment
computed.html: width="1" height="1"
computed.height: 1
computed.width: 1
computed.iscolor: 1
computed.byteordermotorola: 1
computed.usercomment: exif test image.
computed.usercommentencoding: ascii
computed.copyright: photo (c) m.boerger, edited by m.boerger.
computed.copyright.photographer: photo (c) m.boerger
computed.copyright.editor: edited by m.boerger.
ifd0.copyright: photo (c) m.boerger
ifd0.usercomment: ascii
thumbnail.jpeginterchangeformat: 134
thumbnail.jpeginterchangeformatlength: 523
comment.0: comment #1.
comment.1: comment #2.
comment.2: comment #3end
thumbnail.jpeginterchangeformat: 134
thumbnail.thumbnail.height: 1
thumbnail.thumbnail.height: 1
//*/
$index="1"; //定义索引
$string=exif_tagname($index); //获得图像索引的头信息
echo $string; //输出结果
//
if(array_key_exists('file',$_request))
{
$image=exif_thumbnail($_request['file'],$width,$height,$type); //取得文件的缩略图
}
else //如果数组不存在相应的键值
{
$image=false; //返回错误
}
if($image!==false) //如果返回true
{
header("content-type:".image_type_to_mime_type($type)); //发送头文件
echo $image; //输出缩略图
exit; //结束php教程代码
}
else //如果返回错误
{
echo "no thumbnail available"; //输出信息
}
代码如下 | 复制代码 |
<?php教程 /************************************ //函数: watermark($bigimg, $smallimg, $coord = 1) //作用: 添加水印 //参数: $bigimg 必选。大图片--要加上水印的图片 $smallimg 必选。小图片 $coord 可选。水印在大图中的位置, 1 左上角; 2 右上角; 3 右下角; 4 左下角; 5 中间 //示例: watermark('datu.png', 'xiaotu.png', 3); //给datu.png打上水印,水印位置在右下角 *************************************/ function watermark($bigimg, $smallimg, $coord = 1){ //载入两张图片,并转成php识别的编码格式, //等同于 imagecreate 函数,只不过这里创建的不是一个空图片。 $bi = getimagesize($bigimg); switch($bi[2]){ case 1: $im1 = imagecreatefromgif($bigimg);break; case 2; $im1 = imagecreatefromjpeg($bigimg);break; case 3; $im1 = imagecreatefrompng($bigimg);break; } $si = getimagesize($smallimg); switch($si[2]){ case 1: $im2 = imagecreatefromgif($smallimg);break; case 2; $im2 = imagecreatefromjpeg($smallimg);break; case 3; $im2 = imagecreatefrompng($smallimg);break; } // 创建水印--原理:复制小图到大图上。这里注意坐标值的计算 switch($coord){ case 1: imagecopy ( $im1, $im2, 0, 0, 0, 0, $si[0], $si[1] ); break; case 2: imagecopy ( $im1, $im2, $bi[0]-$si[0], 0, 0, 0, $si[0], $si[1] ); break; case 3: imagecopy ( $im1, $im2, $bi[0]-$si[0], $bi[1]-$si[1], 0, 0, $si[0], $si[1] ); break; case 4: imagecopy ( $im1, $im2, 0, $bi[1]-$si[1], 0, 0, $si[0], $si[1] ); break; case 5: imagecopy ( $im1, $im2, ($bi[0]-$si[0])/2, ($bi[1]-$si[1])/2, 0, 0, $si[0], $si[1] ); break; } //根据后缀名生成不同格式的图片文件 switch($bi[2]){ case 1: imagegif($im1);break; case 2; imagejpeg($im1);break; case 3; imagepng($im1);break; } imagedestroy($im1); } /************************************************ //函数: thumbnail($srcimg, $multiple) //作用: 生成一张缩略图 //参数: // $srcimg 必选。源图片文件名 // $multiple 可选。缩略倍数,默认为2倍,即缩小为原来的1/2 //注意: 只支持gif、jpg、png的格式图片。 //示例: thumbnail('我的图片.jpg', 5); *************************************************/ function thumbnail($srcimg, $multiple = 2){ //载入图片并保存其信息到数组 $srcimg_arr = getimagesize($srcimg); //计算缩略倍数 $thumb_width = $srcimg_arr[0] / $multiple; $thumb_height = $srcimg_arr[1] / $multiple; //判断:要建立什么格式的图片(转成php识别的编码) switch($srcimg_arr[2]){ case 1: $im = imagecreatefromgif($srcimg);break; case 2; $im = imagecreatefromjpeg($srcimg);break; case 3; $im = imagecreatefrompng($srcimg);break; } |
相关文章
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
- 有一种方法,可以不打开网站而直接查看到这个网站的源代码.. 这样可以有效地防止误入恶意网站... 在浏览器地址栏输入: view-source:http://...2016-09-20
- <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13- 本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
- 本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
- 本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
- php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
- php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p> </p> <p> </p> <form name="form1...2016-11-25
- 公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- 其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
- 本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
- Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
- 一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
- 微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31