php按指定大小等比缩放生成上传图片缩略图
php教程按指定大小等比缩放生成上传图片缩略图
/**
* *
*等比缩放
* @param unknown_type $srcImage 源图片路径
* @param unknown_type $toFile 目标图片路径
* @param unknown_type $maxWidth 最大宽
* @param unknown_type $maxHeight 最大高
* @param unknown_type $imgQuality 图片质量
* @return unknown
*/
function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{
list($width, $height, $type, $attr) = getimagesize($srcImage);
if($width < $maxWidth || $height < $maxHeight) return ;
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
if($scale < 1) {
$newWidth = floor($scale*$width);
$newHeight = floor($scale*$height);
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = "";
$toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);
switch($type) {
case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
return "$newName.gif"; break;
case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
return "$newName.png"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
}
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}
$array = array(
array(1,2),
array(3,4),
array('www.111cn.net','111cn.net')
);
//看到上面二维数结构了吧,下面我们用foreach来实例
function array_2to1($array)
{
static $result_array=array();
foreach($array as $value)
{
if(is_array($value))
{
arrau_2to1($value);
}
else
$result_array[]=$value;
}
return $result_array;
}
//上面这段代码还可以简写
function _rebuild_array($arr){ //rebuild a array
static $tmp=array();
for($i=0; $i<count($arr); $i++){
if(is_array($arr[$i])) _rebuild_array($arr[$i]);
else $tmp[]=$arr[$i];
}
return $tmp;
}
$arr = array_2to1( $array );
foreach( $arr as $v )
{
echo $v;
}
//现在再来看一个反一维数据保存到二维数据的实例
$arr_new=array();
$insert_key =array('uid','hostname','shopname','province','city','county','address','www.111cn.net','qq','Mobile','msn');
$insert_value=array('2','hostname','shopname','province','city','www.111cn.net','address','shopimg','qq','Mobile','msn');
//一在我们把2个数组一一对应写到一个新的二维数组里去
foreach($insert_key as $key => $val){
$arr_new[$val]=$insert_value[$key];
}
print_r($arr_new);
//英文验证码相对简单,不要作hex处理,直接用色彩值就OK了。如果
session_start();
function rand_create()
{
//通知浏览器将要输出PNG图片
Header("Content-type: image/PNG");
//准备好随机数发生器种子
srand((double)microtime()*1000000);
//准备图片的相关参数
$im = imagecreate(62,22);
$black = ImageColorAllocate($im, 0,0,0); //RGB黑色标识符
$white = ImageColorAllocate($im, 255,255,255); //RGB白色标识符
$gray = ImageColorAllocate($im, 200,200,200); //RGB灰色标识符
//开始作图
imagefill($im,0,0,$gray);
while(($randval=rand()%100000)<10000);{
$_SESSION["Auth_code"] = $randval;
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $randval, $black);
}
//加入干扰象素
for($i=0;$i<200;$i++){
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
//输出验证图片
ImagePNG($im);
//销毁图像标识符
ImageDestroy($im);
}
rand_create();
//调用方法<img src=www.111cn.net.php />
//中文验证码程序如下
Header("Content-type: image/PNG");
$str = "这里设置一中文如果中国WEB第一站www.111cn.net";
$imagesW = 140;
$imagesH = 40;
//
$Auimg = imagecreate($imagesW,$imagesH);
$bgc = ImageColorAllocate($Auimg,255,255,255);
$font = "heiti.ttf";//这里设置字体,你可以随便下载一款字体哦。
$white=imagecolorallocate($Auimg,234,185,95);
imagearc($Auimg, 150, 8, 20, 20, 75, 170, $white);
imagearc($Auimg, 180, 7,50, 30, 75, 175, $white);
imageline($Auimg,20,20,180,30,$white);
imageline($Auimg,20,18,170,50,$white);
imageline($Auimg,25,50,80,50,$white);
$noise_num = 800;
$line_num = 20;
imagecolorallocate($Auimg,0xff,0xff,0xff);
$rectangle_color=imagecolorallocate($Auimg,0xAA,0xAA,0xAA);
$noise_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
$font_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
$line_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++){
imagesetpixel($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),$noise_color);
}
for($i=0;$i<$line_num;$i++){
imageline($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),mt_rand(0,$imagesW),mt_rand(0,$imagesH),$line_color);
}
$mtRnd=rand(0,strlen($str)-4);
if($mtRnd%2)$mtRnd+=1;
$str = substr($str,$mtRnd,8);
$str = iconv("GB2312","UTF-8",$str);
ImageTTFText($Auimg, 20, 0, 16, 30, $font_color, $font, $str);
ImagePNG($Auimg);
ImageDestroy($Auimg);
/*
共同点就是验证码都借助于其它容器来保存如session,cookie等,否则就没有验证的意义了
本文章由www.111cn.net整,转载请注明来源谢谢合作。
//$pattern = "/file-([0-2]d{3})-([1-9]d?)/"; 这个正则表达式,对文件夹的月日进行捕获,再进一步判断处理
02 for($i =2005;$i<=2009;$i++)
03 {
04 for($j = 1;$j<30;$j++)
05 {
06 $dest_dir = "text/file-".$i."-$j";
07 if(!is_dir($dest_dir)) mkdir($dest_dir,0777);
08 }
09 }
10 //创建一些不满足条件的文件夹
11
12 for($t = 0;$t<10;$t++)
13 {
14 $dest_dir = "text/file-".$t;
15 if(!is_dir($dest_dir)) mkdir($dest_dir,0777);
16 }
17
18 //遍历文件夹处理 .
19 function listFile($dir)
20 {
21 $a = array();
22 $handle = opendir($dir);
23 while($file = readdir($handle))
24 {
25 handle_dir($dir,$file,&$a);
26 }
27
28 echo "the rege_array is ";
29 print("<pre>");
30 print_r($a);
31 print("</pre>");
32 }
33
34 function handle_dir($dir,$file,$a)
35 {
36
37 if($file == "." || $file == "..") return ;
38 $minYear = 2008;
39 $maxDay = 13;
40
41 $pattern = "/file-([0-2]d{3})-([1-9]d?)/";
42 $destPath = $dir."/".$file;
43
44 if(!is_dir($destPath))
45 {
46 //echo "$destPath is not a dir ;";
47 return;
48 }
49
50 //删除不满足格式的文件
51 if(!preg_match($pattern,$file))
52 {
53 //echo "<font color=blue>$file</font><font color=red>is not the right rege. the program will unlink -- $destPath --</font><br/>";
54 // unlink($destPath);
55 return;
56 }
57
58 preg_match_all($pattern,$file,$matchs);
59
60 //echo "<font color=blue>$file</font> is the right rege";
61 //print_r($matchs);
62 //echo "the year is ".$matchs[1][0]." and the day is ".$matchs[2][0]."</br>";
63 if(intval($matchs[1][0]) >$minYear && intval($matchs[2][0]) < $maxDay)
64 {
65 $a[]= $destPath;
66 }else { //不满足条件的
67 //unlink($destPath);
68
69 }
70 }
71
72 listFile("text");
ar=array(0=>array('name'=>'食物','id'=>1,'pid'=>0),
02 1=>array('name'=>'植物','id'=>2,'pid'=>0),
03 3=>array('name'=>'白菜','id'=>3,'pid'=>1),
04 4=>array('name'=>'树','id'=>4,'pid'=>2),
05 5=>array('name'=>'苹果','id'=>5,'pid'=>3),
06 6=>array('name'=>'松树','id'=>6,'pid'=>4),
07 7=>array('name'=>'饮料','id'=>7,'pid'=>1),
08 8=>array('name'=>'测试1','id'=>8,'pid'=>7),
09 9=>array('name'=>'测试2','id'=>9,'pid'=>8),
10 10=>array('name'=>'人民','id'=>10,'pid'=>9),
11 11=>array('name'=>'小米','id'=>11,'pid'=>0),
12 );
13
14 echo '<table border="1" width="100%">';
15 tre($ar,0);
16 echo '</table>';
17
18 function tre($tree,$id)
19 {
20 $str = '';
21 $strid = '';
22 foreach ($tree as $key => $item)
23 {
24 if ($item['pid'] == $id) {
25 $i = 0;
26 $i = sonNum($tree,$item['id']);
27 if ($i == 1 || $i == 0) {
28 echo '<tr><td>';
29 } else {
30 echo '<tr><td rowspan="'.$i.'">';
31 }
32 //echo '<tr><td>';
33 echo $item['name'];
34 echo '</td></tr>';
35 tre($tree,$item['id']);
36 }
37 }
38 //tre($tree,$strid);
39 }
40
41 function sonNum($tre,$sum,$totale=1)
42 {
43 foreach ($tre as $key => $value)
44 {
45 if ($value['pid'] == $sum){
46 $totale += sonNum($tre,$value['id'],1);
47 }
48 }
49 return $totale;
50 }
相关文章
pytorch nn.Conv2d()中的padding以及输出大小方式
今天小编就为大家分享一篇pytorch nn.Conv2d()中的padding以及输出大小方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27- 在本篇内容里小编给大家分享的是关于C#比较时间大小的方法总结,对此有需要的朋友们可以学习下。...2020-06-25
- 我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25
安卓利用按钮Button更改的字体大小、字体颜色、背景颜色代码
本文章来介绍在android开发中,我们通过button按钮来动态改变字体大小、字体颜色、背景颜色代码,有需要了解的朋友可参考参考。 实现的逻辑:通过遍历View的方式,判断Vi...2016-09-20- 这篇文章主要介绍了java 用itext设置pdf纸张大小操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-31
- 这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
- matploglib能够绘制出精美的图表,有时候我们希望把一组图放在一起进行比较,就需要用到matplotlib中提供的subplot了,这篇文章主要给大家介绍了关于python中subplot大小的设置方法,需要的朋友可以参考下...2021-06-28
- 这篇文章主要介绍了C++获取文件大小的4种常见技巧分享,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
怎么限制Apache日志文件大小(error.log、access.log)
如何删除error.log、access.log文件以及限制Apache日志文件大小的方法介绍,Apache服务器下access.log以及error.log日志文件一直没有没有动过,今天wordpress 的MYSQL数据库连...2016-01-28- 这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
- 这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# WinForm中禁止改变窗口大小的方法,需要把FormBorderStyle和MaximizeBox的值固定即可,需要的朋友可以参考下...2020-06-25
- if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
- 这篇文章主要介绍了Oracle如何设置表空间数据文件大小,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-22
- 这篇文章主要为大家详细介绍了vue实现上传图片添加水印,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-13
- 这篇文章主要给大家介绍了在CentOS系统下编译安装nginx及配置缩略图插件的方法教程,文中给出了详细的安装步骤,对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。...2017-07-06
- 这篇文章主要介绍了pytorch 实现查看数据类型和大小的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-05-20
- 有时候我们需要限制上传文件的大小,那么可以参考下面的方法...2016-01-27
php+jquery Ajax异步上传图片(ajaxSubmit)实例
下面我们一起来看一个php+jquery Ajax异步上传图片(ajaxSubmit)实例,这个我们真正的利用了ajax而不是使用iframe之类的哦。 效果如下 ...2016-11-25- 用PHP给上传图片加水印的程序是通过判定文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像程序当中判定水...2016-11-25