Hongmu Notes
Home Language Notes PHP batch image width/height modification
Language Notes PHP

PHP batch image width/height modification

PHP batch image width/height modification

Save it away – just in case you lose it! If you need it later, you might have to search everywhere before finding it; but when you don't need it, simply save it!

<?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 "图片修改完成!";

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(PC+WAP) Biotechnology Engineering Category – pbootCMS Website Template; Download Source Code for Health Management, Pharmaceutical and Healthcare Websites – 0070

(PC+WAP) Biotechnology Engineering Category – pbootCMS Website Template; Download Source Code for Health Management, Pharmaceutical and Healthcare Websites – 0070 Practical Collection pbootcms Template

A PbootCMS website template dedicated to biotechnology, health management, and pharmaceuticals, compatible with both PC and mobile devices. Its rigorous and professional design effectively showcases biotechnology products and healthcare services. This template helps biopharmaceutical companies establish a professional online presence and demonstrate their R&D capabilities and product portfolio. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin
👁 47
High-end whole-house solid wood custom furniture website template – 0804

High-end whole-house solid wood custom furniture website template – 0804 Practical Collection Yiyou template

An EyouCMS website template designed for high-end whole-house solid wood custom furniture businesses. Its luxurious and sophisticated design style effectively showcases custom solid wood furniture, whole-house renovation case studies, and design concepts, helping premium furniture customization brands attract high-quality customers online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 40
(Adaptive Mobile Version) HTML5 Responsive Baidu MIP Three-in-One Wellness Information Article/News/Blog PbootCMS Template – 0696

(Adaptive Mobile Version) HTML5 Responsive Baidu MIP Three-in-One Wellness Information Article/News/Blog PbootCMS Template – 0696 Practical Collection pbootcms Template

A Baidu MIP 3-in-1 wellness information, news, and blog PbootCMS template compatible with both PC and WAP devices. Its fresh and user-friendly design effectively aggregates articles on health knowledge and wellness practices, enhancing mobile SEO performance. Ideal for building wellness and health-related information websites. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: w...
👁 47
Responsive New Energy Development Enterprise Website Template 0155

Responsive New Energy Development Enterprise Website Template 0155 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for new energy development enterprises. Its modern and eco-friendly design effectively showcases new energy projects, clean energy products, technological R&D capabilities, and corporate strength. It helps new energy companies present their brand online and attract investment and partnerships. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 50
(PC+WAP) Electric Gate & Extending Door Website – pbootCMS Template; Download Blue Automatic Extending Door Website Source Code – 0071

(PC+WAP) Electric Gate & Extending Door Website – pbootCMS Template; Download Blue Automatic Extending Door Website Source Code – 0071 Practical Collection pbootcms Template

A PbootCMS website template designed for electric gate, telescopic gate, and automatic door products, featuring a blue color scheme and compatibility with both PC and WAP devices. Its modern, tech-inspired design is ideal for showcasing automatic door equipment and security solutions. This template helps telescopic gate manufacturers highlight their product advantages online and attract more project inquiries. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 45
Road Barrier – Transportation Facilities Website Template 0805

Road Barrier – Transportation Facilities Website Template 0805 Practical Collection Yiyou template

This EyouCMS template is ideal for the road guardrail and traffic facility industry. Its sophisticated and professional design is perfect for showcasing traffic guardrail products, road safety infrastructure items, and project case studies. It enables transportation facility manufacturers to present their products online and attract clients from municipal and transportation authorities. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Installation Questions for EyouCMS...
👁 36