PHP使用ffmpeg实现视频截图(Linux系统和windows系统)

环境:php5.6,apache2.2

windows7:

ffmpeg-windows下载地址:https://download.csdn.net/download/qq_39545346/10312836

说明:

将ffmpeg中的所有dll文件和ext文件扔到C盘下的system32文件夹。

执行下面的代码:
$name = md5(date(‘YmdHis’)).”.png”;  
        $from = “E:\1.mp4”;  
        $to = “E:\cover_images\”;  
        $str = “ffmpeg -i “.$from.” -y -f mjpeg -ss 3 -t 1 -s 740×500 “.$to.$name;  
        system($str);

Linux(centos6.8):

根据安装教程在linux上安装完ffmpeg
直接使用exec函数,在php代码中执行linux命令,即可进行截图:
exec(‘/usr/local/bin/ffmpeg -ss 00:00:01  -i ./1.mp4 ./pic/423.jpg  -r 1 -vframes 1 -an -f mjpeg 1>/dev/null’);
注意,运行时应保证以下几点:
1.保存截图文件的文件夹有相关权限,
2.截取的视频文件有相关权限,
3.php没有禁用exec()函数,在php.ini中可以查看disabled_function
4.web访问用户,即apache服务默认用户有执行ffmepg的权限,
apache默认用户在httpd.conf中查看。
5.修改etc下的sudoers文件,新增加
Defaults visiblepw
%apache ALL=(ALL) NOPASSWD:/usr/bin/sudo, /usr/local/bin/MP4Box, /usr/local/bin/ffmpeg
给予apache用户相关权限。

如何使用代理IP进行数据抓取,PHP爬虫抓取亚马逊商品数据

什么是代理?什么情况下会用到代理IP?
代理服务器(Proxy Server),其功能就是代用户去取得网络信息,然后返回给用户。形象的说:它是网络信息的中转站。通过代理IP访问目标站,可以隐藏用户的真实IP。

比如你要抓取一个网站数据,该网站有100万条内容,他们做了IP限制,每个IP每小时只能抓1000条,如果单个IP去抓因为受限,需要40天左右才能采集完,如果用了代理IP,不停的切换IP,就可以突破每小时1000条的频率限制,从而提高效率。

其他想切换IP或者隐藏身份的场景也会用到代理IP,比如SEO等。

代理IP有开放代理也有私密代理,开放代理是全网扫描而来的,不稳定,不适合爬虫,如果自己随便用用还好。用爬虫抓数据,最好使用私密代理。私密代理网上有很多提供商,稳定性参差不齐,现在我们公司使用的是“百变IP”提供的私密代理。

我们公司有个项目是抓取亚马逊数据来进行分析销量、评论等,用PHP进行抓取,抓取亚马逊要特别注意header头,否则输出的数据就是空了。还有一种方法,可以用PHP通过shell_exec来调用curl命令来进行抓取。

function curl_via_proxy($url) {
    $user_agent = ‘curl’;
    $ch = curl_init($url); //创建CURL对象  
    curl_setopt($ch, CURLOPT_HEADER, 0); //返回头部  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回信息  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); //连接超时时间
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); //读取超时时间
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //对认证证书来源的检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_PROXY, ‘proxy.baibianip.com’); //代理服务器地址
    curl_setopt($ch, CURLOPT_PROXYPORT, ‘8000’); //代理服务器端口
curl_setopt($ch, CURLOPT_ENCODING,’gzip’);
    $headers = array(
‘authority:www.amazon.com’,
‘upgrade-insecure-requests:1’,
‘user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3355.4 Safari/537.36’,
‘accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8′,
‘accept-encoding:gzip, deflate, br’,
‘accept-language:zh-CN,zh;q=0.9,en;q=0.8’,
     );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $res = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    if ($curl_errno) {
        curl_close($ch);
        return false;
    }
    curl_close($ch);
    return $res;
}

var_dump(curl_via_proxy(‘https://www.amazon.com/dp/B01H2S9F6C’));

php 使用代理IP进行数据抓取

什么是代理?什么情况下会用到代理IP?
代理服务器(Proxy Server),其功能就是代用户去取得网络信息,然后返回给用户。形象的说:它是网络信息的中转站。通过代理IP访问目标站,可以隐藏用户的真实IP。

比如你要抓取一个网站数据,该网站有100万条内容,他们做了IP限制,每个IP每小时只能抓1000条,如果单个IP去抓因为受限,需要40天左右才能采集完,如果用了代理IP,不停的切换IP,就可以突破每小时1000条的频率限制,从而提高效率。

其他想切换IP或者隐藏身份的场景也会用到代理IP,比如SEO等。

代理IP有开放代理也有私密代理,开放代理是全网扫描而来的,不稳定,不适合爬虫,如果自己随便用用还好。用爬虫抓数据,最好使用私密代理。私密代理网上有很多提供商,稳定性参差不齐,现在我们公司使用的是“百变IP”提供的私密代理。

我们公司有个项目是抓取亚马逊数据来进行分析销量、评论等,用PHP进行抓取,抓取亚马逊要特别注意header头,否则输出的数据就是空了。还有一种方法,可以用PHP通过shell_exec来调用curl命令来进行抓取。

复制代码
    PHP如果是使用curl函数来抓取,主要设置下面几项即可。

    curl_setopt($ch, CURLOPT_PROXY, 'proxy.baibianip.com'); //代理服务器地址
    curl_setopt($ch, CURLOPT_PROXYPORT, '8000'); //代理服务器端口

    如果是抓取HTTPS,把下面两项设置为false:
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //抓HTTPS可以把此项设置为false
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //抓HTTPS可以把此项设置为false
复制代码
方法一:完整示例代码如下,下面提供两种方式来调用:
复制代码
<?php

//代理ip列表
const PROXY_LIST = [
    '124.232.133.199:3128',
    '171.83.165.90:9999',
    
];

function curl_via_proxy($url,$proxy_ip,$headers = [],$user_agent = 'curl',$method = 'GET')
{
    $arr_ip = explode(':',$proxy_ip);

    $ch = curl_init($url); //创建CURL对象  
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_HEADER, 0); //返回头部  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回信息  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); //连接超时时间
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); //读取超时时间
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //对认证证书来源的检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_PROXY, $arr_ip[0]); //代理服务器地址
    curl_setopt($ch, CURLOPT_PROXYPORT, $arr_ip[1]); //代理服务器端口
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    //添加头部信息
    if(!empty($headers)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }


    $res = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    if ($curl_errno) {
        curl_close($ch);
        return false;
    }
    curl_close($ch);
    return $res;
}
$headers = array(
    'authority:www.amazon.com',
    'upgrade-insecure-requests:1',
    'user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3355.4 Safari/537.36',
    'accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding:gzip, deflate, br',
    'accept-language:zh-CN,zh;q=0.9,en;q=0.8',
);
$url = 'https://api.apiopen.top/recommendPoetry';
var_dump(curl_via_proxy($url,PROXY_LIST[array_rand(PROXY_LIST,1)],$headers));
die;
复制代码

 方式二:利用PHP调用Linux的curl命令来进行抓取,Windows下下载curl.exe即可。

$html = shell_exec("curl -x 'proxy.baibianip.com:8000' 'http://www.baidu.com' --connect-timeout 3 -m 5");
echo $html;

php图像处理(缩放、剪裁、缩放、翻转、旋转、透明、锐化)

注意事项:如果要使用php gd处理我们需要开启gd库


Windows下开启PHP的GD库支持

找到php.ini,打开内容,找到:

;extension=php_gd2.dll

把最前面的分号“;”去掉,再保存即可,如果本来就没有分号,那就是已经开启了。


linux开启GD库


##检测GD库是否安装命令
 php5 -m | grep -i gd
 或者
 php -i | grep -i –color gd
##如未安装GD库,则为服务器安装,方法如下
### 如果是源码安装,则加入参数
 –with-gd
### 如果是debian系的linux系统,用apt-get安装,如下
 apt-get install php5-gd
### 如果是CentOS系的系统,用yum安装,如下
 yum install php-gd
### 如果是suse系的linux系统,用yast安装,如下
 yast -i php5_gd


一、创建图片资源

imagecreatetruecolor(width,height);

imagecreatefromgif(图片名称);

imagecreatefrompng(图片名称);

imagecreatefromjpeg(图片名称);

画出各种图像

imagegif(图片资源,保存路径);

imagepng()

imagejpeg();


二、获取图片属性

imagesx(res//宽度

imagesy(res//高度

getimagesize(文件路径)

返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height=”yyy” width=”xxx””,可直接用于 IMG 标记。销毁图像资源

imagedestroy(图片资源);


三、透明处理

PNG、jpeg透明色都正常,只有gif不正常

imagecolortransparent(resource image [,int color])//将某个颜色设置成透明色

imagecolorstotal()

imagecolorforindex();

四、图片的裁剪

imagecopyresized()

imagecopyresampled();


五、加水印(文字、图片)

字符串编码转换string iconv ( string $in_charset , string $out_charset , string $str )


六、图片旋转

imagerotate();//制定角度的图片翻转


七、图片的翻转

沿X轴   沿Y轴翻转

八、锐化

imagecolorsforindex()

imagecolorat()

在图片上画图形
 $img=imagecreatefromgif(“./images/map.gif”);
 $red= imagecolorallocate($img, 255, 0, 0);

 imageline($img, 0, 0, 100, 100, $red);

 imageellipse($img, 200, 100, 100, 100, $red);

 imagegif($img, “./images/map2.gif”);

 imagedestroy($img);图片普通缩放

$filename=”./images/hee.jpg”;

 $per=0.3;

 list($width, $height)=getimagesize($filename);

 $n_w=$width*$per;
 $n_h=$width*$per;

 $new=imagecreatetruecolor($n_w, $n_h);

 $img=imagecreatefromjpeg($filename);
//拷贝部分图像并调整

 imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//图像输出新图片、另存为

 imagejpeg($new, “./images/hee2.jpg”);

 imagedestroy($new);
 imagedestroy($img);图片等比例缩放、没处理透明色

function thumn($background, $width, $height, $newfile) {
 list($s_w, $s_h)=getimagesize($background);//获取原图片高度、宽度

 if ($width && ($s_w < $s_h)) {
     $width = ($height / $s_h) * $s_w;
 } else {
     $height = ($width / $s_w) * $s_h;
 }

 $new=imagecreatetruecolor($width, $height);

 $img=imagecreatefromjpeg($background);

 imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);

 imagejpeg($new, $newfile);

 imagedestroy($new);
 imagedestroy($img);
}

thumn(“images/hee.jpg”, 200, 200, “./images/hee3.jpg”);gif透明色处理

function thumn($background, $width, $height, $newfile) {
 list($s_w, $s_h)=getimagesize($background);

 if ($width && ($s_w < $s_h)) {
     $width = ($height / $s_h) * $s_w;
 } else {
     $height = ($width / $s_w) * $s_h;
 }

 $new=imagecreatetruecolor($width, $height);

 $img=imagecreatefromgif($background);

 $otsc=imagecolortransparent($img);
 if($otsc >=0 && $otst < imagecolorstotal($img)){//判断索引色
  $tran=imagecolorsforindex($img, $otsc);//索引颜色值

  $newt=imagecolorallocate($new, $tran[“red”], $tran[“green”], $tran[“blue”]);

  imagefill($new, 0, 0, $newt);

  imagecolortransparent($new, $newt);
 }

 imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);

 imagegif($new, $newfile);

 imagedestroy($new);
 imagedestroy($img);
}

thumn(“images/map.gif”, 200, 200, “./images/map3.gif”);图片裁剪

function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){

 $back=imagecreatefromjpeg($background);

 $new=imagecreatetruecolor($cut_width, $cut_height);

 imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);

 imagejpeg($new, $location);

 imagedestroy($new);
 imagedestroy($back);
}

cut(“./images/hee.jpg”, 440, 140, 117, 112, “./images/hee5.jpg”);图片加水印

文字水印

function mark_text($background, $text, $x, $y){
  $back=imagecreatefromjpeg($background);

  $color=imagecolorallocate($back, 0, 255, 0);

  imagettftext($back, 20, 0, $x, $y, $color, “simkai.ttf”, $text);

  imagejpeg($back, “./images/hee7.jpg”);

  imagedestroy($back);
 }

 mark_text(“./images/hee.jpg”, “细说PHP”, 150, 250);

//图片水印
function mark_pic($background, $waterpic, $x, $y){
$back=imagecreatefromjpeg($background);
$water=imagecreatefromgif($waterpic);
$w_w=imagesx($water);
$w_h=imagesy($water);
imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
imagejpeg($back,”./images/hee8.jpg”);
imagedestroy($back);
imagedestroy($water);
}
mark_pic(“./images/hee.jpg”, “./images/gaolf.gif”, 50, 200);图片旋转

$back=imagecreatefromjpeg(“./images/hee.jpg”);

 $new=imagerotate($back, 45, 0);

 imagejpeg($new, “./images/hee9.jpg”);图片水平翻转垂直翻转

function turn_y($background, $newfile){
  $back=imagecreatefromjpeg($background);

  $width=imagesx($back);
  $height=imagesy($back);

  $new=imagecreatetruecolor($width, $height);

  for($x=0; $x < $width; $x++){
   imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
  }

  imagejpeg($new, $newfile);

  imagedestroy($back);
  imagedestroy($new);
 }

 function turn_x($background, $newfile){
  $back=imagecreatefromjpeg($background);

  $width=imagesx($back);
  $height=imagesy($back);

  $new=imagecreatetruecolor($width, $height);

  for($y=0; $y < $height; $y++){
   imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
  }

  imagejpeg($new, $newfile);

  imagedestroy($back);
  imagedestroy($new);
 }

 turn_y(“./images/hee.jpg”, “./images/hee11.jpg”);
 turn_x(“./images/hee.jpg”, “./images/hee12.jpg”); 图片锐化

function sharp($background, $degree, $save){
 $back=imagecreatefromjpeg($background);

 $b_x=imagesx($back);
 $b_y=imagesy($back);

 $dst=imagecreatefromjpeg($background);
 for($i=0; $i<$b_x; $i++){
  for($j=0; $j<$b_y; $j++){
   $b_clr1=imagecolorsforindex($back, imagecolorat($back, $i-1, $j-1));\前一个像素颜色数组
   $b_clr2=imagecolorsforindex($back, imagecolorat($back, $i, $j));\取出当前颜色数组

   $r=intval($b_clr2[“red”]+$degree*($b_clr2[“red”]-$b_clr1[“red”]));\加深
   $g=intval($b_clr2[“green”]+$degree*($b_clr2[“green”]-$b_clr1[“green”]));
   $b=intval($b_clr2[“blue”]+$degree*($b_clr2[“blue”]-$b_clr1[“blue”]));

   $r=min(255, max($r, 0));//限制r范围在0-255之间
   $g=min(255, max($g, 0));
   $b=min(255, max($b, 0));

   if(($d_clr=imagecolorexact($dst, $r, $g, $b))==-1){//等于1不在颜色范围内
    $d_clr=Imagecolorallocate($dst, $r, $g, $b);//创建一个颜色
   }

   imagesetpixel($dst, $i, $j, $d_clr);
  }

 }
 imagejpeg($dst, $save);
 imagedestroy($back);
 imagedestroy($dst);
}

sharp(“./images/hee.jpg”, 20, “./images/hee13.jpg”);十月 26th, 2011No comments yet真诚待 php设计实现和应用验证码类
validationcode.class.php

/* 开发一个验证码类
 *
 * 1. 什么是验证码
 *
 * 2. 验证码的作用
 *
 * 3. 编写验证码类(PHP图像处理
 *
 * 4. 使用验证码
 *
 *
 */<?php
 class ValidationCode {
  private $width;
  private $height;
  private $codeNum;
  private $image;   //图像资源
  private $disturbColorNum;
  private $checkCode;

  function __construct($width=80, $height=20, $codeNum=4){
   $this->width=$width;
   $this->height=$height;
   $this->codeNum=$codeNum;
   $this->checkCode=$this->createCheckCode();
   $number=floor($width*$height/15);

   if($number > 240-$codeNum){
    $this->disturbColorNum= 240-$codeNum;
   }else{
    $this->disturbColorNum=$number;
   }

  }
  //通过访问该方法向浏览器中输出图像
  function showImage($fontFace=””){
   //第一步:创建图像背景
   $this->createImage();
   //第二步:设置干扰元素
   $this->setDisturbColor();
   //第三步:向图像中随机画出文本
   $this->outputText($fontFace);
   //第四步:输出图像
   $this->outputImage();
  }

  //通过调用该方法获取随机创建的验证码字符串
  function getCheckCode(){
   return $this->checkCode;
  }

  private function createImage(){
   //创建图像资源
   $this->image=imagecreatetruecolor($this->width, $this->height);
   //随机背景色
   $backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
   //为背景添充颜色
   imagefill($this->image, 0, 0, $backColor);
   //设置边框颜色
   $border=imagecolorallocate($this->image, 0, 0, 0);
   //画出矩形边框
   imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
  }

  private function  setDisturbColor(){
   for($i=0; $i<$this->disturbColorNum; $i++){
    $color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color);
   }

   for($i=0; $i<10; $i++){
    $color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
    imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
   }
  }

  private function createCheckCode(){
   $code=”23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ”;
   $string=”;
   for($i=0; $i < $this->codeNum; $i++){
    $char=$code{rand(0, strlen($code)-1)};
    $string.=$char;
   }

   return $string;
  }

  private function outputText($fontFace=””){
   for($i=0; $i<$this->codeNum; $i++){
    $fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
    if($fontFace==””){
     $fontsize=rand(3, 5);
     $x=floor($this->width/$this->codeNum)*$i+3;
     $y=rand(0, $this->height-15);
     imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
    }else{
     $fontsize=rand(12, 16);
     $x=floor(($this->width-8)/$this->codeNum)*$i+8;
     $y=rand($fontSize+5, $this->height);
     imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
    }
   }
  }

  private function outputImage() {
   if(imagetypes() & IMG_GIF){
    header(“Content-Type:image/gif”);
    imagepng($this->image);
   }else if(imagetypes() & IMG_JPG){
    header(“Content-Type:image/jpeg”);
    imagepng($this->image);
   }else if(imagetypes() & IMG_PNG){
    header(“Content-Type:image/png”);
    imagepng($this->image);
   }else if(imagetypes() & IMG_WBMP){
    header(“Content-Type:image/vnd.wap.wbmp”);
    imagepng($this->image);
   }else{
    die(“PHP不支持图像创建”);
   }
  }

  function __destruct(){
   imagedestroy($this->image);
  }
 }code.php

<?php
 session_start();
 include “validationcode.class.php”;

 $code=new ValidationCode(80, 20, 4);

 $code->showImage();   //输出到页面中供 注册或登录使用

 $_SESSION[“code”]=$code->getCheckCode();  //将验证码保存到服务器中demo.php

<?php
 session_start();
 echo $_POST[“code”].”<br>”;
 echo $_SESSION[“code”].”<br>”;

 if(strtoupper($_POST[“code”])==strtoupper($_SESSION[“code”])){
  echo “ok”;
 }else{
  echo “error”;
 }
?>
<body>
 <form action=”login.php” method=”post”>
  user:<input type=”text” name=”usenrame”><br>
  pass:<input type=”passowrd” name=”pass”><br>

  code: <input type=”text” name=”code” οnkeyup=”if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()”> <img src=”code.php” οnclick=”this.src=’code.php?’+Math.random()”><br>

  <input type=”submit” name=”sub” value=”login”><br>
 </form>
</body>


1、画图

验证码,统计图

安装GD库(图片处理库)

■创建一个画布—-创建资源类型——–高度、宽度
■绘制图像
1、制定各种颜色
2、矩形、园、点、线段、扇形、画字(字符、字符串、freetype字体),每一个图像对应一个函数

■输出图像/保存处理好的图像
1、输出各种类型(gif、png、jpeg)

imagegif();
imagepng();
imagegpeg();

■释放资源
<?php
//step 1创建图片资源
$img=imagecreatetruecolor(200,200);
$red=imagecolorallocate($img,255,0,0);
$yellow=imagecolorallocate($img,255,255,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
imagefill($img,0,0,$yellow);//颜色填充
//step2画各种图形
imagefilledrectangle($img,10,10,80,80,$red);//画矩形并填充
imagerectangle($img,10,10,80,80,$red));//画矩形不填充,颜色填充

//线段
 imageline($img,0, 0, 200, 200 ,$blue);
 imageline($img,200, 0, 0, 200, $blue);

 //点
 imagesetpixel($img,50, 50 ,$red);
 imagesetpixel($img,55, 50 ,$red);
 imagesetpixel($img,59, 50 ,$red);
 imagesetpixel($img,64, 50 ,$red);
 imagesetpixel($img,72, 50 ,$red);

 //圆
 imageellipse($img, 100, 100, 100, 100,$green);
 //圆
 imagefilledellipse($img, 100, 100, 10, 10,$blue);
边框
//输出图像或保存图像
header(“Content-Type:img/gif”);
imagegif($img);
//释放资源
imagedestory($img);

画饼状图

//step 1创建图片资源
 $img=imagecreatetruecolor(200, 200);

// $img=imagecreate(200, 200);

 $white=imagecolorallocate($img, 255, 255, 255);
 $gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
 $darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
 $navy=imagecolorallocate($img, 0, 0, 0x80);
 $darknavy=imagecolorallocate($img, 0, 0, 0x50);
 $red= imagecolorallocate($img, 0xFF, 0, 0);
 $darkred=imagecolorallocate($img, 0x90, 0, 0);

 imagefill($img, 0, 0, $white);

 //3D效果
 for($i=60; $i>50; $i–){
  imagefilledarc($img, 50, $i,100, 50, -160, 40, $darkgray, IMG_ARC_PIE);
  imagefilledarc($img, 50, $i,100, 50, 40, 75, $darknavy, IMG_ARC_PIE);
  imagefilledarc($img, 50, $i,100, 50, 75, 200, $darkred, IMG_ARC_PIE);
 }
  imagefilledarc($img, 50, $i,100, 50, -160, 40, $gray, IMG_ARC_PIE);
  imagefilledarc($img, 50, $i,100, 50, 40, 75, $navy, IMG_ARC_PIE);
  imagefilledarc($img, 50, $i,100, 50, 75, 200, $red, IMG_ARC_PIE);

 header(“Content-Type:image/gif”);
 imagegif($img);

 //释放资源
 imagedestroy($img);画文字

<?php

 //step 1创建图片资源
 $img=imagecreatetruecolor(200, 200);

// $img=imagecreate(200, 200);

 $white=imagecolorallocate($img, 255, 255, 255);
 $gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
 $darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
 $navy=imagecolorallocate($img, 0, 0, 0x80);
 $darknavy=imagecolorallocate($img, 0, 0, 0x50);
 $red= imagecolorallocate($img, 0xFF, 0, 0);
 $darkred=imagecolorallocate($img, 0x90, 0, 0);

 imagefill($img, 0, 0, $gray);

 imagechar($img, 5, 100, 100, “A”, $red);
 imagechar($img, 5, 120, 120, “B”, $red);
 imagecharup($img, 5, 60, 60, “C”, $red);
 imagecharup($img, 5, 80, 80, “D”, $red);

 imagestring($img, 3, 10, 10, “Hello”, $navy);
 imagestringup($img, 3, 10, 80, “Hello”, $navy);

 imagettftext($img, 25, 60, 150, 150, $red, “simkai.ttf”, “##”);
 imagettftext($img, 12, -60, 50, 150, $red, “simli.ttf”, “##”);

 header(“Content-Type:image/gif”);
 imagegif($img);

 //释放资源
 imagedestroy($img);2、处理原有的图像

ThinkPHP访问路径隐藏index.php问题

因为工作需要首次接触Tp,刚刚拿到项目就遇到一个问题。正常的管理后台能正常访问,但是前台跳转,不能正常访问。后经过排查,是因为本地的代码中没有设置忽略tp的index.php入口文件导致的。因为前端请求的接口都忽略了index.php;所以导致了请求接口的404错误。

1234567在入口文件index.php的同级目录中增加一个.htaccess文件就解决了<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]</IfModule>

如果问题还存在则需要查看并修改httpd-conf配置文件:
apache的配置文件没有将mod_rewrite.so模块加载。 \
将AllowOverride 的值改为All。

php数组(gb2312)转json(utf-8)

for($i = 0;$i<count($before);i++){
    $str[i] = iconv('GB2312', 'UTF-8', '$before[i]');//gb2312转换为utf-8
}
$after = json_encode($str,JSON_UNESCAPED_UNICODE);//转换成json

iconv: 字符串按要求的字符编码来转换

iconv(‘源’,’目标’,’数据’)

json_encode:对变量进行 JSON 编码

json_encode(‘数据’,JSON_UNESCAPED_UNICODE)

JSON_UNESCAPED_UNICODE:不要编码Unicode,直接是汉字

PHP将字符串全部转成大写或小写

<?php
$string = “Learn PHP string functions at jb51.net”;
$lower = strtolower($string);
$upper = strtoupper($string);
print(“$lower\n”);
print(“$upper\n”);
print(“\n”);

?>

版权声明:本文为CSDN博主「执着的小鱼儿」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhizhuo0915/article/details/81064650


显示如下:

learn php string functions at jb51.net

php函数serialize()与unserialize()

serialize()和unserialize()在php手册上的解释是:

serialize — Generates a storable representation of a value

serialize — 产生一个可存储的值的表示

unserialize — Creates a PHP value from a stored representation

unserialize — 从已存储的表示中创建 PHP 的值

<?php 
//声明一个类 
class dog { 

    var $name; 
    var $age; 
    var $owner; 

    function dog( $in_name = “unnamed”, $in_age = “0”, $in_owner = “unknown”) { 
        $this -> name = $in_name; 
        $this -> age = $in_age; 
        $this -> owner = $in_owner; 
    } 

    function getage() { 
        return ( $this -> age * 365); 
    } 
    
    function getowner() { 
        return ( $this -> owner); 
    } 
    
    function getname() { 
        return ( $this -> name); 
    } 

//实例化这个类 
$ourfirstdog = new dog( “Rover”, 12, “Lisa and Graham”); 
//用serialize函数将这个实例转化为一个序列化的字符串 
$dogdisc = serialize( $ourfirstdog); 
print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:”dog”:3:{s:4:”name”;s:5:”Rover”;s:3:”age”;i:12;s:5:”owner”;s:15:”Lisa and Graham”;} 

print ‘<BR>’; 

/* 
—————————————————————————————– 
    在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件 
—————————————————————————————– 
*/ 

//我们在此注销这个类 
unset( $ourfirstdog); 

/*    还原操作   */ 

/* 
—————————————————————————————– 
    在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件 
—————————————————————————————– 
*/ 


//我们在这里用 unserialize() 还原已经序列化的对象 
$pet = unserialize( $dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了 
//获得年龄和名字属性 
$old = $pet -> getage(); 
$name = $pet -> getname(); 
//这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态 
print “Our first dog is called $name and is $old days old<br>”; 
print ‘<BR>’; 
?>
本文地址:php函数serialize()与unserialize()