/* *功能:php多種方式完美實現下載遠程圖片保存到本地 *參數:文件url,保存文件名稱,使用的下載方式 *當保存文件名稱為空時則使用遠程文件原來的名稱 */ function getImage($url,$filename='',$type=0){ if($url==''){return false;} if($filename==''){ $ext=strrchr($url,'.'); if($ext!='.gif' && $ext!='.jpg'){return false;} $filename=time().$ext; } //文件保存路徑 if($type){ $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $img=curl_exec($ch); curl_close($ch); }else{ ob_start(); readfile($url); $img=ob_get_contents(); ob_end_clean(); } $size=strlen($img); //文件大小 $fp2=@fopen($filename,'a'); fwrite($fp2,$img); fclose($fp2); return $filename; } //合成圖片方法 function hc_image($gid){ //生成二維碼 $imagem = new \Think\Image();//實例化二維碼 $im="http://qr.topscan.com/api.php?text=http://pcg.kuoer.net/index.php?s=/Home/Goods/index/id/".$gid;//二維碼圖片 getImage($im,'./Uploads/fenxiang/'.$gid.'ewm.jpg');//下載二維碼 $imagem->open('./Uploads/fenxiang/'.$gid.'ewm.jpg');//打開二維碼 $imagem->thumb (140,140)->save('./Uploads/fenxiang/'.$gid.'ewm.jpg');//設置二維碼大小 $ewm='./Uploads/fenxiang/'.$gid.'ewm.jpg';//二維碼地址 //找到商品圖片 $goods=M('w_goods')->where(array('id'=>$gid))->field('content,image,money')->find(); $goods['image']=explode(',',$goods['image']); $image_goods=$goods['image'][0]; $imageg=new \Think\Image();//實例化商品圖片 $imageg->open($image_goods);//打開商品圖片 $imageg->thumb(375,375)->save('./Uploads/fenxiang/'.$gid.'image_goods.jpg');//生成縮略圖 $image_goods='./Uploads/fenxiang/'.$gid.'image_goods.jpg';//商品圖片地址 $imagec=new \Think\Image();//實例化商品圖片 $imagec->open($image_goods);//打開圖片 $imagec->thumb(375, 375,\Think\Image::IMAGE_THUMB_CENTER)->save($image_goods);//居中裁剪 $image = new \Think\Image();//實例化圖片 $image->open('./Public/Home/images/beijin.jpg');//打開背景圖 $image->water($image_goods,\Think\Image::IMAGE_WATER_NORTH,100)->save($image_goods);//合成圖片 $loa=array('0',315);//模糊圖片地址 $imagea=new \Think\Image(); $imagea->open($image_goods);//打開圖片 $imagea->water('./Public/Home/images/zc_5.png',$loa,100)->save($image_goods); //文字第一行 $location=array('10','380'); $text=mb_substr($goods['content'],0,20,'utf-8'); $imageb=new \Think\Image(); $imageb->open($image_goods); $imageb->text($text,'./msyh.ttf',12,'#000000',$location,0)->save($image_goods); //文字第二行 $location1=array('10','400'); $text2=mb_substr($goods['content'],'20',19,'utf-8'); if($text2) { $text2 = $text2 . '...'; $imageb1 = new \Think\Image(); $imageb1->open($image_goods);//打開圖片 $imageb1->text($text2, './msyh.ttf', 12, '#000000', $location1, 0)->save($image_goods); } //添加二維碼 $imagec=new \Think\Image(); $imagec->open($image_goods); $imagec->water($ewm,array('200','430'),100)->save($image_goods); //添加 $money='¥'.$goods['money']; $imagez=new \Think\Image(); $imagez->open($image_goods); $imagez->text($money,'./msyh.ttf',20,'#cc0033',array('20','435'))->save($image_goods); unlink($ewm);//同時刪除二維碼圖片 //同時把圖片地址添加到數據庫; M('w_goods')->where(array('id'=>$gid))->setField('fenxiang',$image_goods); }
感謝我磊哥