zoukankan      html  css  js  c++  java
  • 在画布中添加二维码加文字 和 压缩多图片到一个压缩包中

    一、在画布中添加二维码加文字

    <?php 

    class Image{

        /**     * 生成水印图片 图片结合底部文字     * @param string $file

         * @param string $code
    * @return array
    */
    public function getPicture($file = '', $text = '',$date = '')
    {
    try {
    $info = getimagesize($file); //获取图片信息
    $type = image_type_to_extension($info[2], false); // 获取图片扩展名
    $fun = "imagecreatefrom{$type}"; // 构建处理图片方法名-关键是这里
    $image = $fun($file); // 调用方法处理
    $font = public_path('/font/Century-Gothic.ttf'); // 字体文件
    $color = imagecolorallocate($image, 0, 0, 0); // 文字颜色

    $text = mb_convert_encoding($text, "html-entities", "utf-8");
    //获取文字宽度及高度
    $bounds = ImageTTFBBox(40, 0, $font, $text);
    $min_x = min(array($bounds[0], $bounds[2], $bounds[4], $bounds[6]));
    $max_x = max(array($bounds[0], $bounds[2], $bounds[4], $bounds[6]));
    $min_y = min(array($bounds[1], $bounds[3], $bounds[5], $bounds[7]));
    $max_y = max(array($bounds[1], $bounds[3], $bounds[5], $bounds[7]));
    $width = ($max_x - $min_x);
    $height = ($max_y - $min_y);

    $image_thumb = imagecreatetruecolor($info[0], $info[1] + $height); //创建画布
    $background = imagecolorallocate($image_thumb, 255, 255, 255);  //白色
    imagefill($image_thumb, 0, 0, $background); //填充背景颜色
    //把二维码嵌入画布中
          imagecopyresampled($image_thumb, $image, 0, 0, 0, 0, $info[0],
           $info[1], $info[0], $info[1]);
    //添加文字
        imagettftext($image_thumb, 40, 0, ($info[0] - $width) / 2, 5 + $info[1]
          + $height / 2, $color, $font, $text);
    // header("Content-Type:" . $info['mime']);
    $imageTypeFun = "image" . $type;
    $dir = public_path('/uploads/qr/picture/'.$date);
    if(!is_dir($dir)){
    @mkdir($dir,0777,true);
    }
    $src = $dir.'/'.$text . '.' . $type;
    //保存
    $path = '/uploads/qr/picture/'.$date .'/'. $text . '.' . $type;
    $imageTypeFun($image_thumb, $src);
    imagedestroy($background);
    ob_end_clean();
    return jsonReturn(0, '添加文字成功', $path);
    } catch (Exception $e) {
    return jsonReturn(-1, $e->getMessage());
    }
    }
     public function erweima($device){
    require_once 'mobile/example/phpqrcode/phpqrcode.php';
    $device_sn = $device;
    $urls =$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME
      $url = $urls."/Mobile/index.html?device_sn=".$device_sn;
        $url = urldecode($url);
    $qr_code_path = 'uploads/QRcodeDevice/'.date("Y-m-d").'/';
    if (!file_exists($qr_code_path)) {
    mkdir($qr_code_path);
    }
    /* 生成二维码 */
    $qr_code_file = $qr_code_path.$device_sn.'.png';
    QRcode::png($url, $qr_code_file, 'H', 6,6);//最后一个6是控制白边大小
    $background = "mobile/font/back.png";
    $logo = "mobile/font/logo.png";
    $this->mark_photo($qr_code_file,$device_sn,$logo,$qr_code_file);
    $where = array('device_sn'=>$device);

    $arr['QRcode'] = $urls."/".$qr_code_file;
    return $arr;
    }

    //拼接图片,logo,文字
    private function mark_photo($background,$text,$logo,$filename){
      $info = getimagesize($background); // 获取图片信息
      $type = image_type_to_extension($info[2],false); // 获取图片扩展名
      $fun = "imagecreatefrom{$type}"; // 构建处理图片方法名-关键是这里
      $back= $fun($background); // 调用方法处理
        $color=imagecolorallocate($back,0,0,0);
       $logo_info = getimagesize($logo);
      $logo_type = image_type_to_extension($info[2],false); // 获取图片扩展名
      $logo_fun  = "imagecreatefrom{$logo_type}"; // 构建处理图片方法名-关键是这里
        $logo_w=$info[0];
    $logo_h=$info[1];
    $font="mobile/font/STYH.otf"; // 字体文件
    //imagettftext只认utf8字体,所以用iconv转换
    imagettftext($back, 21, 0, 40, 337, $color, $font, $text);//调二维码中字体位置
    //执行合成调整位置
    imagecopyresampled($back, $logo, 139,140, 0, 0, 65, 65, $logo_w, $logo_h);//调中间logo位置
      $image_fun = 'image'.$type;
    $image_fun($back,$filename);//保存
    imagedestroy($back);
    imagedestroy($logo);
      return json_encode('code'=>0,'msg'=>'成功',array('filename'=>$filename));
    }
    /**
     *压缩文件
    * @return mixed
    */
    public function getZipper()
    {
    $zip = new ipArchive();
    // 图片路径
    $img_path = public_path('/uploads/qr');


    $img_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($img_path));


    $path = public_path('/uploads/download');

    if(!file_exists($path)){
    mkdir($path,0777,true);
    }
    $zip_file = $path.'/download.zip';

    $zip->open($zip_file, ipArchive::CREATE | ipArchive::OVERWRITE);

    $this->forZip($zip, $img_files, $img_path, '');

    $zip->close();
    return response()->download($zip_file);
    }
    //循环文件 添加压缩包中
    private function forZip($zip, $files, $file_path, $new_path)
    {
    foreach ($files as $name => $file) {
    // 我们要跳过所有子目录
    if ( ! $file->isDir()) {
    $filePath = $file->getRealPath();
    // 用 substr/strlen 获取文件名
    $relativePath = $new_path . substr($filePath, strlen($file_path));
    $zip->addFile($filePath, $relativePath);
    }
    }
    }

    }

  • 相关阅读:
    matplotlib 进阶之origin and extent in imshow
    Momentum and NAG
    matplotlib 进阶之Tight Layout guide
    matplotlib 进阶之Constrained Layout Guide
    matplotlib 进阶之Customizing Figure Layouts Using GridSpec and Other Functions
    matplotlb 进阶之Styling with cycler
    matplotlib 进阶之Legend guide
    Django Admin Cookbook-10如何启用对计算字段的过滤
    Django Admin Cookbook-9如何启用对计算字段的排序
    Django Admin Cookbook-8如何在Django admin中优化查询
  • 原文地址:https://www.cnblogs.com/hua-nuo/p/13526897.html
Copyright © 2011-2022 走看看