php - 我如何在 PHP 中使用 imagescale 调整图像大小
问题描述
我尝试使用以下方法调整图像大小:
代码
$thumbimg = $img->resizeImage($thumb, 200, 200);
public function resizeImage($imagePath,$new_width,$new_height) {
$fileName = pathinfo($imagePath,PATHINFO_FILENAME);
$ext = pathinfo($imagePath,PATHINFO_EXTENSION);
$fullPath = pathinfo($imagePath,PATHINFO_DIRNAME)."/".$fileName.'.'.$ext;
if (file_exists($fullPath)) {
return $fullPath;
}
$image = $this->openImage($imagePath);
if ($image == false) {
return null;
}
$width = imagesx($image);
$height = imagesy($image);
$imageResized = imagecreatetruecolor($width, $height);
if ($imageResized == false) {
return null;
}
$image = imagecreatetruecolor($width , $height);
$imageResized = imagescale($image,$new_width,$new_height);
touch($fullPath);
$write = imagepng($imageResized,$fullPath);
if (!$write) {
imagedestroy($imageResized);
return null;
}
imagedestroy($imageResized);
return $fullPath;
}
实际它以原始宽度和高度保存我的图像。但为什么?怎么了?
解决方案
如果$imagePath
引用现有图像,$fullPath
则将引用同一文件。因此,要么文件存在,在这种情况下返回,要么文件不存在,在这种情况下,您无法打开图像并null
返回。无论哪种方式,都不使用图像调整大小代码。
推荐阅读
- javascript - 在 ReCharts 中的 X 轴上显示 24 小时时间
- c++ - GCC 需要为等待对象移动 ctor,即使它是左值
- powershell - 让 PowerShell 根据当前月份名称读取对象名称的语法
- django - Django 无法持久化缓存数组
- php - Google Calendar APIv3 - 如何使用 webhook 处理传入的资源更改通知?
- python - python pandas:如何修改列标题名称和修改日期格式
- node.js - 使用 Node 包或开源工具将 pdf 转换为图像
- java - docker中连接的Java JDBC无法连接到主机数据库
- json - 将具有另一个数组元素的 JSON 数组移动到相同的级别
- search - 3 列产品类别和 4 列搜索结果