将所有具有一定宽度的图像重写为不同尺寸的脚本?

发布于 2024-11-18 07:46:11 字数 174 浏览 4 评论 0原文

我正在使用 PHP 的 GD 库。

基本上,我进行了设计更改,需要调整一大堆具有一定宽度的图像的大小。

即任何 876px 宽的东西都需要是 828px。

有没有办法循环遍历目录中的所有 JPG 文件,检查它们的宽度尺寸,如果它们等于 X,则获取它们现有的文件名,重新缩放到相同的名称?

I am using the GD library with PHP.

Basically, I've made a design change and need to resize a whole bunch of images that are a certain width.

ie anything that is 876px wide needs to be 828px.

Is there a way to loop through all JPG files in a directory, check their width dimension, and if they equal X then grab their existing file name, rescale down to the same name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2024-11-25 07:46:11

只需要使用 imagecopyresampled() 中一个循环。

$path = "/my/path/to/jpgs";
$targetWidth = 876;
$newWidth = 828;
$imageQuality = 95;

if (is_dir($path)) {
    $dHandle = opendir($path);
    if ($dHandle) {
        while (($cFileName = readdir($dHandle)) !== false) {
            if (!preg_match("/\.jpg$/", $cFileName)) {
                continue;
            }

            $cImagePath = $path . $cFileName;
            list ($cWidth, $cHeight) = getimagesize($cImagePath);

            if ($cWidth == $targetWidth) {
                $cImage = imagecreatefromjpeg($cImagePath);
                if ($cImage === false) {
                    echo "Error reading: " . $cImagePath . "\n";
                    continue;
                }

                $cNewHeight = round($cHeight * ($newWidth / $cWidth));

                $cNewImage = imagecreatetruecolor($newWidth, $cNewHeight);
                imagecopyresampled($cNewImage, $cImage, 0, 0, 0, 0, $newWidth, $cNewHeight, $cWidth, $cHeight);

                if (imagejpeg($cNewImage, $cImagePath, $imageQuality) === false) {
                    echo "Error writing: " . $cImagePath . "\n";
                    continue;
                }
            }
        }

        closedir($dHandle);
    }
}

Just need to use imagecopyresampled() in a loop.

$path = "/my/path/to/jpgs";
$targetWidth = 876;
$newWidth = 828;
$imageQuality = 95;

if (is_dir($path)) {
    $dHandle = opendir($path);
    if ($dHandle) {
        while (($cFileName = readdir($dHandle)) !== false) {
            if (!preg_match("/\.jpg$/", $cFileName)) {
                continue;
            }

            $cImagePath = $path . $cFileName;
            list ($cWidth, $cHeight) = getimagesize($cImagePath);

            if ($cWidth == $targetWidth) {
                $cImage = imagecreatefromjpeg($cImagePath);
                if ($cImage === false) {
                    echo "Error reading: " . $cImagePath . "\n";
                    continue;
                }

                $cNewHeight = round($cHeight * ($newWidth / $cWidth));

                $cNewImage = imagecreatetruecolor($newWidth, $cNewHeight);
                imagecopyresampled($cNewImage, $cImage, 0, 0, 0, 0, $newWidth, $cNewHeight, $cWidth, $cHeight);

                if (imagejpeg($cNewImage, $cImagePath, $imageQuality) === false) {
                    echo "Error writing: " . $cImagePath . "\n";
                    continue;
                }
            }
        }

        closedir($dHandle);
    }
}
后来的我们 2024-11-25 07:46:11

那里有很多图像调整大小脚本...只需谷歌...但如果您想自己构建一些东西,您基本上会使用 getimagesize imagecopyresampled

There are a lot of image resize scripts out there...just google... but if you're looking to build something yourself, you would basically use getimagesize and imagecopyresampled

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文