详情页

php 批量修改图片宽高

时间:2023年12月16日

编辑:佚名

收藏一下,免得丢了,要用的时候找半天都找不到,不用的时候就收藏一下吧
<?php
die;
// $array = glob(__DIR__ . '/*.{jpg,jpeg,png,gif}',GLOB_BRACE);
// print_r($array);
// foreach ($array as $k=>$v){
//     $k++;
//     rename($v,__DIR__ . "/".md5($k).'.jpg');
// }
// die;
// 文件夹路径
$folder_path = __DIR__;
// 新图片的宽度和高度
$new_width = 500;
$new_height = 281;
// 获取文件夹中的所有文件
$files = glob($folder_path . "/*.{jpg,jpeg,png,gif}", GLOB_BRACE);
foreach ($files as $file) {
    // 打开原始图片文件
    $source_image = imagecreatefromjpeg($file);
    // 获取原始图片的宽度和高度
    $source_width = imagesx($source_image);
    $source_height = imagesy($source_image);
    // 创建空白的新图片
    $new_image = imagecreatetruecolor($new_width, $new_height);
    // 将原始图片调整大小并复制到新图片中
    imagecopyresampled($new_image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $source_width, $source_height);
    // 保存新图片
    $new_file = $folder_path . "/" . basename($file);
    imagejpeg($new_image, $new_file);
    // 释放内存
    imagedestroy($source_image);
    imagedestroy($new_image);
}
echo "图片修改完成!";
相关文章
猜你需要