php 生成水印图片 完整过程 NFS自动同步刷新varnish缓存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/terry_water/article/details/37883089

1.

需要配置varnish的配置文件:

sub vcl_recv {

 if (req.request == "BAN") {
        #if (!client.ip ~ purge) {
        #    error 405 "Not allowed.";
        #}
        ban("req.url ~ " + req.url );
        #error 200 "Purged.";
        return (lookup);
    }

}

sub vcl_hit {
    if (req.request == "BAN") {
        purge;
        error 200 "Hit Purged.";
    }

    return (deliver);
}

sub vcl_miss {
    if (req.request == "BAN") {
       #purge;
       #error 200 "Miss Purged.";
    }

    return (fetch);
}

2

添加水印图片生成文件

index.php

<?php
//echo $_SERVER['HTTP_HOST'];
//
//exit;
//echo $_SERVER["HTTP_REFERER"];exit;  .tomtop.com
$host = $_SERVER['HTTP_HOST'];
$subhost = strrev(substr(strrev($host),11));

//header("Content-type: image/jpeg");
//原始图像
$imgurl = $_GET['imgurl'];
//水印图像
//$imglogo = "logo/".$_SERVER['HTTP_HOST'].".png";
$imglogo = "http://img2.tomtop.com/createtextimg.php?logo=".$subhost;

//echo $imgurl."<br>";
//echo $imglogo."<br>";
//得到原始图片信息


//$dst_im = imagecreatefromjpeg($imgurl);
$dst_info = getimagesize($imgurl);

        
    switch ($dst_info[2])
    {
        case 1:
            header("Content-type: image/gif");
            $dst_im =imagecreatefromgif($imgurl);
            break;
        case 2:
            header("Content-type: image/jpeg");
            $dst_im =imagecreatefromjpeg($imgurl);
            break;
        case 3:
            header("Content-type: image/png");
            $dst_im =imagecreatefrompng($imgurl);
            break;
        default:
            die("");
            exit;
    }
    
    


$src_im = imagecreatefrompng($imglogo);
$src_info = getimagesize($imglogo);    

//水印透明度
$alpha = 15;

//合并水印图片
imagecopymerge($dst_im,$src_im,($dst_info[0]-$src_info[0])/2,($dst_info[1]-$src_info[1])/2,0,0,$src_info[0],
$src_info[1],$alpha);

//输出合并后水印图片
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>

文字生成图片:

createtextimg.php

<?php
Header("Content-type: image/png");
 
class textPNG {
    var $font = 'fonts/arial.ttf'; //默认字体. 相对于脚本存放目录的相对路径.
    var $msg = "Tomtop Action"; // 默认文字.
    var $size = 24;
    var $rot = 0; // 旋转角度.
    var $pad = 0; // 填充.
    var $transparent = 90; // 文字透明度.
    var $red = 0; // 在黑色背景中...
    var $grn = 0;
    var $blu = 0;
    var $bg_red = 255; // 将文字设置为白色.
    var $bg_grn = 255;
    var $bg_blu = 255;
 
function draw() {
    $width = 0;
    $height = 0;
    $offset_x = 0;
    $offset_y = 0;
    $bounds = array();
    $image = "";
 
    // 确定文字高度.
    $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
    if ($this->rot < 0) {
        $font_height = abs($bounds[7]-$bounds[1]);
    } else if ($this->rot > 0) {
        $font_height = abs($bounds[1]-$bounds[7]);
    } else {
        $font_height = abs($bounds[7]-$bounds[1]);
    }
 
    // 确定边框高度.
    $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
    if ($this->rot < 0) {
        $width = abs($bounds[4]-$bounds[0]);
        $height = abs($bounds[3]-$bounds[7]);
        $offset_y = $font_height;
        $offset_x = 0;
 
    } else if ($this->rot > 0) {
        $width = abs($bounds[2]-$bounds[6]);
        $height = abs($bounds[1]-$bounds[5]);
        $offset_y = abs($bounds[7]-$bounds[5])+$font_height;
        $offset_x = abs($bounds[0]-$bounds[6]);
 
    } else {
        $width = abs($bounds[4]-$bounds[6]);
        $height = abs($bounds[7]-$bounds[1]);
        $offset_y = $font_height;;
        $offset_x = 0;
    }
 
    $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
 
    $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
    $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
 
    if ($this->transparent) ImageColorTransparent($image, $background);
    ImageInterlace($image, false);
 
    // 画图.
    ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
 
    // 输出为png格式.
    imagePNG($image);
}
}
 
$text = new textPNG;
$bg_red = 255;
$bg_grn = 255;
$bg_blu = 255;
$size = 50;
$tr = 80;
$red = 241;
$grn = 241;
$blu = 241;
$msg = $_GET['logo'];
$msg = str_replace("-"," ",$msg);
$msg = str_replace("_"," ",$msg);
$msg = str_replace("."," ",$msg);
if (isset($msg)) $text->msg = $msg; // 需要显示的文字
if (isset($font)) $text->font = $font; // 字体
if (isset($size)) $text->size = $size; // 文字大小
if (isset($rot)) $text->rot = $rot; // 旋转角度
if (isset($pad)) $text->pad = $pad; // padding
if (isset($red)) $text->red = $red; // 文字颜色
if (isset($grn)) $text->grn = $grn; // ..
if (isset($blu)) $text->blu = $blu; // ..
if (isset($bg_red)) $text->bg_red = $bg_red; // 背景颜色.
if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
if (isset($tr)) $text->transparent = $tr; // 透明度 (boolean).
 
$text->draw();

?>
3

安装inotify

添加shell文件:/root/inotify.sh ,监控到文件更新变化后执行复制和更新varnish的工作

#!/bin/bash       
src=/var/www/html/tomtop.com1.7/media
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f'  -e modify,delete,create,attrib $src  | while read  file      
    do  
        fulloldfiledir=`expr substr "$file" 16  ${#file}`
        relativefiledir=`expr substr "$file" 44  ${#file}`
        fullnewfiledir='/sharefile/images/img2.tomtop.com/'${relativefiledir}
        echo $fulloldfiledir
        echo $relativefiledir
        echo $fullnewfiledir
        echo "$fulloldfiledir  was write to file:$fullnewfiledir" >> /dev/shm/inotify.log 2>&1
        mkdir -p -- "$(dirname -- "$fullnewfiledir")" && cp -Rrf "$fulloldfiledir" "$fullnewfiledir" ;
        php /var/www/html/tomtop.com1.7/tomtopdotcom_scripts/img2tomtopvarnishflush.php "$relativefiledir"
       # mkdir -p -- "$(dirname -- "$fullnewfiledir")" && cp -Rrf "$fulloldfiledir" "$fullnewfiledir" ;
#		/usr/bin/php -f /var/www/html/tomtop.com1.7/shell/GetProductReview.php reviewinfo
   done  

4

img2tomtopvarnishflush.php 用来执行更新varnish的代码部分:

<?php

class Varnish_Flush{
	
	public function getSubDomainArray(){
		
		$str = "tomtop-auction.tomtop.com tomtop-au.tomtop.com tomtop-game.tomtop.com tomtop-shop.tomtop.com homegarden2012.tomtop.com tomtop-sales.tomtop.com usbempire.tomtop.com tomtop-home.tomtop.com tomtop-digital.tomtop.com tomtop-wholesaler.tomtop.com tomtop-w.tomtop.com tomtop-uk.tomtop.com antiquechina2009.tomtop.com tomtop-deals.tomtop.com tomorrowhome.tomtop.com ttmallzone365.tomtop.com tomorrowtop.tomtop.com tomtop-usa.tomtop.com ttstoreusa.tomtop.com tomtop-mall.tomtop.com tomorrowyes.tomtop.com costcobuy.tomtop.com tomorrowokla.tomtop.com addstore.tomtop.com newgaga.tomtop.com newmango.tomtop.com lalayes.tomtop.com dudustory.tomtop.com buyonline315.tomtop.com bluebetter.tomtop.com tootooup.tomtop.com ilastone.tomtop.com kokoyesgo.tomtop.com anforme.tomtop.com smartlife2013.tomtop.com dodotech.tomtop.com 7colorstore.tomtop.com topestore360.tomtop.com usbcity888.tomtop.com dadidashop.tomtop.com duoduobox.tomtop.com anderlink.tomtop.com";
		$str_arr = explode(" ",$str);
		$url_array = array();
		foreach($str_arr as $st){
			$url = "http://".$st;
			$url_array[] = $url;
		}
		$url_array[] = "http://img2.tomtop.com";
		return $url_array;
		
	}
	public function purgeUrls() {
			
			if(isset($_SERVER['argv'])){
				$argv =  $_SERVER['argv'];
				$uu = "";
				foreach($argv as $arg){
					$uu = $arg;
				}
			}else{
				exit;
			}	
			$subdomainarray = $this->getSubDomainArray();
			foreach($subdomainarray as $subdomain){
				$url = $subdomain."?imgurl=".$subdomain."/".$uu;
				echo "this url:".$url."\n";
				$curl = curl_init($url); 
				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "BAN"); 
				$result = curl_exec($curl); 
				$info = curl_getinfo($curl);
				if ($info['http_code']!=200 && $info['http_code']!=404){
					echo 'VarnishIndex: Cannot purge url ' . $url . ': http_code: ' . $info['http_code'] . '\n';
				}
			}
			/* Using curl library that allows to perform PURGE requests */
			//$errors = array();
			
			//$url = $uu;
			//$url = "http://img2.tomtop.com";
			
			echo "good!\n";
			exit;
			/*
			try {
				$curl = curl_init();
				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "BAN");
				curl_setopt($curl, CURLOPT_PORT, 80);
				curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($curl, CURLOPT_URL, $url);
				//curl_exec($curl);
				echo "\nurl:".$url."\n";
				// curl error handling
				echo curl_errno($curl);
				if (curl_errno($curl)) {
					$errors[] = 'Cannot purge url ' . $url . ' due to error: ' . curl_error($curl);
				} else {
					$info = curl_getinfo($curl);
					// 404 is ok - might happen when a product is disabled
					// after it is purged from Varnish, Magento will say that url doesn't exist)
					if ($info['http_code']!=200 && $info['http_code']!=404){
						$errors[] = 'Cannot purge url ' . $url . ' http_code: ' . $info['http_code'];
						echo 'VarnishIndex: Cannot purge url ' . $url . ': http_code: ' . $info['http_code'] . '\n';
					}
				}
				
				curl_close ($curl);
			} catch (Exception $e) {
				$errors[] = 'Error occured: ' . $e->getMessage();
				echo 'VarnishIndex: purge: ' . $e->getFile().' '.$e->getLine().' '.$e->getMessage()."\n";
			}
			*/
		
			//return $errors;
    }
}

$shell = new Varnish_Flush();
$shell->purgeUrls();
echo $shell->getSubDomainArray();
?>

5

如果添加水印域名。需要在img2tomtopvarnishflush.php里面添加

index.php文件的截取重新计算
$subhost = strrev(substr(strrev($host),11));

配置nginx和varnish域名

OK!




展开阅读全文

没有更多推荐了,返回首页