采集图片水印添加

采集图片水印添加

<?php
$curl = curl_init();
$url = 'http://nj.rent.house365.com/';
curl_setopt($curl , CURLOPT_URL , $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl); //返回结果
$zz = '/<dd class="item">s+<div class="pic"><a target="_blank" href=".*">'.
'<img alt="(.*)" src="(.*)" width="140" height="110" /></a> </div>s+'.
'/iUs';
preg_match_all($zz,$str,$match);

foreach ($match[2] as $key => $value) {
file_put_contents('./img/'.$key.'.jpg', file_get_contents($value));
/*打开图片*/
//1、配置图片路径
$src = './img/'.$key.'.jpg';
//2、获取图片信息
$info = getimagesize($src);
//外传:计算文字大小
$width = substr($info[3], 7,3);
$height = substr($info[3],20,3 );
$width > $height ? $size = $height : $size = $width;
$size = $size/10;
$wsize = $size*6;
//3、获取图片类型
$type = image_type_to_extension($info[2], false);
//4、在内存中创建一个和我们图像类型一样的图像
$func = "imagecreatefrom{$type}";
//5、把图片复制到我们的内存中
$image = $func($src);
//var_dump($image);die;
/* 操作图片 */
//1、设置字体路径
$font = "heiti.ttf";
//2、填写水印内容
$content = "面试通过";
//3、设置字体颜色和透明度
$color = imagecolorallocatealpha($image, 0, 0, 0, 50);
//4、写入文字
imagettftext($image, $size, 0, rand(0,$width-$wsize), rand($size,$height-$size), $color, $font, $content);
/* 输出图片 */
//2、保存图片
imagejpeg($image,'./img/'.$key.'.jpg');}?>