温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php GD库的使用

发布时间:2020-06-09 08:38:18 阅读:574 作者:q3428532 栏目:web开发
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>
<?php

/**
 * 调整图片大小
 * $path图片的宽度大于$width则等比缩放
 * $path图片的高度大于$height切成多张图片
 * @param $path 图片地址
 * @param $width integer|string  图片宽度
 * @param @height integer|string 图片高度
 */
function imgResize($path$width$height{
  // 存放生成的图片路径
  $imgPaths array();
  // 获取图片宽度/高度/类型
  list($srcW$srcH$type) = getp_w_picpathsize($path);
  $type -= 1;

  // 创建源图像
  $p_w_picpathcreatefromFuns array('p_w_picpathcreatefromgif''p_w_picpathcreatefromjpeg''p_w_picpathcreatefrompng');
  $src $p_w_picpathcreatefromFuns[$type]($path);
  // 图片的宽度大于$width则等比缩放
  if$srcW $width ) {
    $dstHeight $srcH * ($width $srcW);
    // 新建一个真彩***像
    $dst p_w_picpathcreatetruecolor$width$dstHeight );
    p_w_picpathcopyresampled($dst$src,  0000$width$dstHeight$srcW$srcH);
    $src $dst; 
    // 更新原图的宽度和高度
    $srcW $width;
    $srcH $dstHeight;
  }
  else {
    // 没有调整宽度
    $width $srcW;
  }
  
  // 图片的高度大于$height切成多张图片
  if$srcH $height ) {
    // 切割的高度位置
    $srcY 0;
    do {
      $dstHeight = ($srcY $height $srcH) ? ($srcH $srcY) : ($height);
      $dst p_w_picpathcreatetruecolor$width$dstHeight );
      p_w_picpathcopyresampled($dst$src,  000$srcY$width$dstHeight$width$dstHeight);
      $filePath './img/' . md5(microtime(true)) . '.jpg';
      $imgPaths[] = $filePath;
      ImageJpeg($dst$filePath);
      is_resource($dstand p_w_picpathdestroy($dst);
      $srcY += $dstHeight;
    }while($srcY $srcH);
  }
  else {
    // 如果高度符合 则将调整了原图(也有可能是调整了宽度之后的)保存为文件
    $filePath './img/' . md5(microtime(true)) . '.jpg';
    $imgPaths[] = $filePath;
    ImageJpeg($src$filePath);
  }

  // 释放内存
  isset($srcand is_resource($srcand p_w_picpathdestroy($src);
  isset($dstand is_resource($dstand p_w_picpathdestroy($dst);

  return $imgPaths;
}

/**
 * 调整图片清晰度
 * @param string $path  图片路径
 * @param string $quality 清晰度
 * @return string 新的图片路径
 */
function reQuality( $path$quality {
    $p_w_picpath_type p_w_picpath_type_to_extension(exif_p_w_picpathtype($path), false);
    $fun "p_w_picpathcreatefrom{$p_w_picpath_type}";
    $src $fun($path);
    $newPath generateUniqueFilePath('./img/');
    ImageJpeg($src$newPath$quality);
    return $newPath;
}

/**
 * 生成唯一的文件路径
 * @param $basePath string 文件夹路径
 * @param $suffix string
 * @return string 文件路径
 */
function generateUniqueFilePath($basePath$suffix='.jpg'{
  $filePath $basePath md5(microtime(true)) . $suffix;
  return $filePath;
}

header('Content-Type:p_w_picpath/png');

$path './img/jjj.jpg';
$imgPaths imgResize($path1000010000);
// reQuality($path, 1);

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×