php 生成二维码及图片尺寸转换

GD和图像处理(三)


摘要:根据字符串内容,生成二维码图片,及对图片尺寸进行转换。
**1. 生成二维码图片:**
  • 下载二维码生成类库
    https://sourceforge.net/projects/phpqrcode/

    下载后解压得到:phpqrcode.php,将其复制到自己的项目路径下即可。

  • 生成二维码图片

    require_once ‘./phpqrcode.php’;
    $str = 'QTX just do it ! ';
    $file_path = ‘C:\Users\Administrator\Desktop\123445.png’; //要保存的路径文件名
    QRcode::png($str, $file_path, QR_ECLEVEL_M, 2, 0);
    string


2. 图片尺寸转换:

  • 方法一

      // 定义方法,可用以处理正方形图片或长方形图片尺寸,按宽高比例自适应
      function resize($imgsrc, $width, $height = 0) {  //参数定义为:源图片,目标宽度,目标高度 
      	$data = file_get_contents($imgsrc);
      	$img_s = imagecreatefromstring($data);  // 获得源图片资源
          $width_s = imagesx($img_s);  // 获得源图片宽度
          $height_s = imagesy($img_s);  // 获得源图片高度
      
          $width_t = $width;  // 获得目标图片宽度
          $height_t = ($height == 0) ? $width : $height;  // 获得目标图片高度
      
          $image_t = imagecreatetruecolor($width_t, $height_t); //创建一个彩色的底图 
          $white = imagecolorallocate($image_t, 255, 255, 255);
          imagefill($image_t, 0, 0, $white); // 初始化背景为白色
      
          if (($width_s / $height_s) < ($width_t / $height_t)) {
          	$_final['width'] = $width_s * $height_t / $height_s;
          	$_final['height'] = $height_t;
          	$dst['x'] = ($width_t - $_final['width']) / 2;
          	$dst['y'] = 0;
          } else {
          	$_final['width'] = $width_t;
      		$_final['height'] = $width_t * $height_s / $width_s;
      		$dst['x'] = 0;
      		$dst['y'] = ($height_t - $_final['height']) / 2;
          }
      
          ImageCopyResized($image_t, $img_s, $dst['x'], $dst['y'], 0, 0, $_final['width'], $_final['height'], $width_s, $height_s);
          $rel = imagepng($image_t, $imgsrc);
          imagedestroy($img_s);
          imagedestroy($image_t);
          return $rel ? $imgsrc : false;
      }
    

    $result = resize($file_path, 100);
    echo $result;
    这里写图片描述

  • 方法二

    /**
    * 生成新的小尺寸图片,图片占用内存会降低
    * @param string $im 源图片路径
    * @param string $dest 目标图片路径
    * @param int $maxwidth 生成图片宽
    * @param int $maxheight 生成图片高
    */
    function resizeImage($im, $dest, $maxwidth, $maxheight) {
        $img = getimagesize($im);
        switch ($img[2]) {
            case 1:
                $im = @imagecreatefromgif($im);
                break;
            case 2:
                $im = @imagecreatefromjpeg($im);
                break;
            case 3:
                $im = @imagecreatefrompng($im);
                break;
            default:
                throw new Exception('图片格式不匹配');
        }
    
        $pic_width = imagesx($im);
        $pic_height = imagesy($im);
        $resizewidth_tag = false;
        $resizeheight_tag = false;
    
        if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
            if ($maxwidth && $pic_width > $maxwidth) {
                $widthratio = $maxwidth / $pic_width;
                $resizewidth_tag = true;
            }
    
            if ($maxheight && $pic_height > $maxheight) {
                $heightratio = $maxheight / $pic_height;
                $resizeheight_tag = true;
            }
    
            if ($resizewidth_tag && $resizeheight_tag) {
                if ($widthratio < $heightratio)
                    $ratio = $widthratio;
                else
                    $ratio = $heightratio;
            }
    
            if ($resizewidth_tag && !$resizeheight_tag)
                $ratio = $widthratio;
            if ($resizeheight_tag && !$resizewidth_tag)
                $ratio = $heightratio;
    
            $newwidth = $pic_width * $ratio;
            $newheight = $pic_height * $ratio;
    
            if (function_exists("imagecopyresampled")) {
                $newim = imagecreatetruecolor($newwidth, $newheight);
                imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
            } else {
                $newim = imagecreate($newwidth, $newheight);
                imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
            }
            imagejpeg($newim, $dest);
            imagedestroy($newim);
        } else {
            imagejpeg($im, $dest);
        }
    }
    




参考连接:https://www.jb51.net/article/148716.htm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值