调整 PNG 图像大小并保持透明度

2023-11-30

我想调整 PNG 图像的透明度,请帮忙。 这是代码:

function resize($width,$height) {

  $new_image = imagecreatetruecolor($width, $height);

  imagealphablending($new_image, false);
  imagesavealpha($new_image,true);
  $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);

  imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->image = $new_image;
}

Try this

UPDATED

function createThumb($upfile, $dstfile, $max_width, $max_height){
       $size = getimagesize($upfile);
       $width = $size[0];
       $height = $size[1];
       $x_ratio = $max_width / $width;
       $y_ratio = $max_height / $height;
       if( ($width <= $max_width) && ($height <= $max_height)) {
               $tn_width = $width;
               $tn_height = $height;
       } elseif (($x_ratio * $height) < $max_height) {
               $tn_height = ceil($x_ratio * $height);
               $tn_width = $max_width;
       } else {
               $tn_width = ceil($y_ratio * $width);
               $tn_height = $max_height;
       }
       if($size['mime'] == "image/jpeg"){
               $src = ImageCreateFromJpeg($upfile);
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
               imageinterlace( $dst, true);
               ImageJpeg($dst, $dstfile, 100);
       } else if ($size['mime'] == "image/png"){
               $src = ImageCreateFrompng($upfile);

    // integer representation of the color black (rgb: 0,0,0)
    $background = imagecolorallocate($src, 0, 0, 0);
    // removing the black from the placeholder
    imagecolortransparent($src, $background);

    // turning off alpha blending (to ensure alpha channel information 
    // is preserved, rather than removed (blending with the rest of the 
    // image in the form of black))
    imagealphablending($src, false);

    // turning on alpha channel information saving (to ensure the full range 
    // of transparency is preserved)
    imagesavealpha($src, true);
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
               Imagepng($dst, $dstfile);

       } else {

               $src = ImageCreateFromGif($upfile);
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
               imagegif($dst, $dstfile);
       }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

调整 PNG 图像大小并保持透明度 的相关文章

  • 如何将 JSON 文本转换为 PHP 关联数组

    我将以下 JSON 对象存储在文本文件 data txt 中 player black time 0 from 2c to 3d 我使用 php 阅读 问题 有没有简单的方法可以转换 data到 PHP 关联数组 我尝试过使用json de
  • 如何使用 monolog ElasticSearchHandler 登录 Laravel 应用程序

    Monolog 包含弹性搜索处理程序和格式化程序 但它作为自定义通道对 Laravel 的实现并不像 Laravel 文档网站上描述的那么简单 以下是如何执行此操作的简要分步说明 为您的弹性搜索日志记录创建一个配置文件 config ela
  • jetty服务器运行php代码

    我想配置让jetty运行PHP文件 但到目前为止还没有成功 我在 Eclipse IDE 中安装了 Jetty WTP 工具 当我启动 Jetty 服务器时 我得到了例外 java lang ClassNotFoundException o
  • 使用 PHPSpreadsheet 打开受密码保护的 XLSX 文件

    我正在尝试打开受密码保护的 Excel 文件 xlsx PHP电子表格 https github com PHPOffice PhpSpreadsheet 文档 https phpspreadsheet readthedocs io en
  • 过滤 PHP 中所有类型的空格

    我知道有很多类型的空间 em 空间 en 空间 薄空间 不间断空间等 但是 我提到的所有这些都有 HTML 实体 至少 PHP 的 htmlentities 返回类似的内容 但是 那些没有 HTML 实体的空间怎么办 示例 示例 URL 不
  • Apache 重写 - 获取 PHP 中的原始 URL

    我在 nginx 或 Apache 中重写了这个地址 http domain com hello 到像这样的脚本 http domain com test php ref hell 如何在 PHP 中访问这个重写的 URL 因为 如果我使用
  • 如何在SQL Server数据库表列中存储图像[重复]

    这个问题在这里已经有答案了 我有一张名为FEMALE在我的数据库中 它有ID as Primary Key 它有一个Image column 我的问题是如何使用 SQL 查询存储图像 尝试一下 insert into tableName I
  • 需要有关使用 PHP 在 mysql 数据库中插入逗号分隔数据的帮助

    数据库表中已有的演示数据 INSERT INTO csvtbl ID SKU Product Name Model Make Year From Year To VALUES 1 C2AZ 3B584 AR Power Steering P
  • PHP 中的异步数据库/服务调用:Gearman 与 pthreads

    在我们的 LAMP 站点上 我们遇到一些服务必须多次调用数据库才能提取数据的问题 通常在 PHP 中完成此操作的方式 至少我的经验 是串行的 这显然是低效的 我们可以通过使用缓存和聚合一些查询来缓解一些低效率的问题 但在某些情况下我们仍然需
  • 验证数据库匹配中的 $_GET id 是否足够安全?

    我的网站上有 2 个页面 一个是 index php 索引页面列出了数据库中存在的所有帖子 另一个页面是 post php 当单击索引页面上的特定帖子时 帖子页面显示单个帖子 现在我用来列出 index php 上所有帖子的代码是 post
  • 如何使用 Java2D 创建硬件加速图像?

    我正在尝试创建一个快速图像生成器 它可以执行大量 2d 转换和形状渲染 因此我尝试使用 BufferedImage 然后获取 Graphics2D 对象来执行所有绘图 我现在主要关心的是 make 速度非常快 所以我创建一个像这样的 Buf
  • 代码点火器 JSON

    你好 我使用 codeigniter 然后我从控制器中的数据库中回显输出 然后在我的视图文件中执行以下操作 但它没有显示任何内容 S 我的模型文件 function forumList this gt db gt select oversk
  • 在 PHP 中使用 file_get_contents 进行 PUT 请求的错误请求

    这个 api 调用使用 Postman REST 客户端 可以正常工作 但是当我的 GAE 应用程序中的服务器上发出请求时 我当前收到以下错误 HTTP 请求失败 在 C Projects app file php 第 26 行 打开流失败
  • 为什么 mysqli num_rows 总是返回 0?

    我在使用 mysqli 获取要返回的行数时遇到了问题 尽管确实有一些结果 但我每次都只是得到 0 if stmt mysqli gt prepare SELECT id title visible parent id FROM conten
  • 通过 PHP 使用 Eclipse BIRT 报表设计器

    我想在 php web 项目中使用 Birt Reports 因此我安装了推荐的 Java Bridge 和 BIRT Runtime 将 JavaBridgeTemplate621 war 和 birt war 移至我的 Tomcat 之
  • zend 模块特定配置问题

    我使用 zend 框架构建 REST Web 服务 并且使用模块来分隔我的 api 版本 现在 我想为每个模块 v1 和 v2 都有一个单独的配置文件 主要用于指定单独的数据库连接 我有这样的目录结构 application modules
  • 从命令行运行 PHP 脚本

    如何使用用于解析 Web 脚本的 PHP 解释器从命令行运行 PHP 脚本 我有一个phpinfo php从网络访问的文件显示German已安装 但是 如果我运行phpinfo php从命令行使用 php phpinfo php and g
  • PHP 与 .= 相反

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 有谁知道是
  • PHP邮件功能有时可以工作

    我正在编写一个脚本 需要通过PHP邮件功能发送电子邮件 如下所示 它在向 gmail 帐户发送电子邮件时有效 但在我的域中的帐户却无效 我们正在运行 Exchange 服务器 截至目前 电子邮件是从 www server 发送的 有谁知道
  • SQL 大表中的随机行(使用 where 子句)

    我有一个网站 人们可以在其中对汽车进行投票 向用户展示 4 辆汽车 他 她可以投票选出他们最喜欢的汽车 桌子cars有重要的列 car id int 10 not auto increment so has gaps views int 7

随机推荐