如何优化服务器上​​的图像

时间:2011-03-11 08:30:01

标签: php

我需要一个PHP脚本来查找给定目录中的所有图像(JPG)并优化(压缩)它而不改变图像的大小和文件名。

5 个答案:

答案 0 :(得分:4)

使用$img = imagecreatefromjpeg("foo.jpg");

加载图像

并以imagejpeg($img, "foo.jpg", 0);或您想要的任何质量作为第三个参数重新保存?

这应该是一个“纯php”​​解决方案。

答案 1 :(得分:3)

您可以循环浏览目录并通过执行以下操作获取所有图像:

$dir = "path/to/images/{*.jpg}";
// Open a known directory, and proceed to read its contents
$images = array();
foreach(glob($dir, GLOB_BRACE) as $key => $img)
{
    // do image manipulation here
}

答案 2 :(得分:2)

使用DirectoryIterator http://www.php.net/manual/en/class.directoryiterator.php

获取一系列文件

用imagecreatefromjpeg加载文件后用imagejpeg重新保存。 或者您可以使用像Wideimage这样的高级库来处理文件http://wideimage.sourceforge.net/

答案 3 :(得分:1)

这可以递归地找到png和jpg没有时间用GIF测试它所以它可能需要在这里和那里进行一些调整,但在压缩jpegs和png文件方面做得很好。

随意玩它。 (还记得在运行压缩之前备份图片!)

使用示例显示在脚本的底部。

<?php

/**
 * Function remove ICC Profile information from image (make image size smaller)
 * @param type $old
 * @param type $new
 * @src https://stackoverflow.com/questions/3614925/remove-exif-data-from-jpg-using-php
 * status - currently not used by this script
 */
function removeIcc()
{

    // ...

}

/**
 * 
 * @param integer $newWidth - width in %
 * @param string $targetFile - path to a file
 * @param type $originalFile - path to a file
 * @param integer $value - image compression strength value from 0 - 9
 * @throws Exception - if file type not supported
 */
function resizeImg($newWidth, $targetFile, $originalFile, $options) {

    $info = getimagesize($originalFile);
    $mime = $info['mime'];

    switch ($mime) {
            case 'image/jpeg':
                    $image_create_func = 'imagecreatefromjpeg';
                    $image_save_func = 'imagejpeg'; // not used - file are saved with oryginal extension
                    $new_image_ext = 'jpg';
                    $args = [$targetFile, $options['quality']]; // first argument will be added to the fornt of the array below - $temp
                    break;

            case 'image/png':
                    $image_create_func = 'imagecreatefrompng';
                    $image_save_func = 'imagepng';
                    $new_image_ext = 'png'; // not used - file are saved with oryginal extension
                    $args = [$targetFile, $options['compresionLvl'], PNG_ALL_FILTERS]; // first argument will be added to the fornt of the array below - $temp
                    break;

            case 'image/gif':
                    $image_create_func = 'imagecreatefromgif';
                    $image_save_func = 'imagegif'; // not used - file are saved with oryginal extension
                    $new_image_ext = 'gif';
                    $args = [$targetFile]; // first argument will be added to the fornt of the array below - $temp
                    break;

            default: 
                    throw new Exception('Unknown image type.');
    }

    $img = $image_create_func($originalFile);
    list($width, $height) = getimagesize($originalFile);
    $newWidth = $width / 100 * $newWidth;
    if($newWidth > $options['maxWidth']) $newWidth = $options['maxWidth'];
    echo $width . " => " . $newWidth.PHP_EOL;
    $newHeight = ($height / $width) * $newWidth;
    $tmp = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

//    if (file_exists($targetFile)) {
//            unlink($targetFile);
//    }
    array_unshift($args, $tmp);
    var_dump($args);

    call_user_func_array($image_save_func, $args); // new
    //$image_save_func($tmp, $targetFile, $value); //old
}

/**
 * Function responsible for image optimisation.
 * Function is used by listFiles function,
 */
function optymizeFile($file, $options){
    resizeImg($options['resizeImgProcent'], $file, $file, $options);
    removeIcc();
}

/**
 * Function is used by searchForFiles function 
 * Function depends on optymizeFile function to run image optimisation.
 * @param string $path - starting path
 * @param string $extRegEx - what file name/extension to search
 */
function listFiles($path, $extRegEx, $options){
    // BLOB REGULAR EXPRESION IS VERY LIMITED - that is why we get all files from directory and make comparisoin with preg_match
    $files = array_filter(glob($path.'*'));

    clearstatcache();
    foreach($files as $file){
        // TRUE REGULAR EXPRESION
        if(preg_match($extRegEx, $file)) {
            // DEPENDENCY - image optymisation

            // echo ceil((filesize($file) / 1024)).'Kb'.PHP_EOL;
            if(filesize($file) > ($options['optymizeFileLargerThen'] * 1024)){
                optymizeFile($file, $options);
            }
        }

    }
}

/**
 * This function search for files with given extension
 * Function depends on listFiles function to list all the files.
 * @param string $path - starting path
 * @param string $extRegEx - what file name/extension to search
 * @param boolean $recursive - search in subdirectories
 */
function searchForFiles($path, $extRegEx, $recursive = false, $options){
    $dir = new DirectoryIterator($path);
    // DEPENDENCY - list files in curent directory
    listFiles($path.'/', $extRegEx, $options);
    foreach ($dir as $fileinfo) {
        // if set as recursive 
        if ($recursive == true && $fileinfo->isDir() && !$fileinfo->isDot()) {
            //echo $fileinfo->getFilename().PHP_EOL;
            // GO RECURSIVE
            searchForFiles($path.'/'.$fileinfo->getFilename(), $extRegEx, $recursive, $options);
        }
    }
}

// USE EXAMPLE
$path = '.';
$extRegEx = '%.*(png|jpg|jpeg|gif)$%'; //where "%" is expresion delimiter
$recursive = true;
$options = [
    'resizeImgProcent' => 100, // this is Procentage. 100 = no resize
    'compresionLvl' => 9, // compresion strength from 0 to 9 where 9 is the strongest and 6 is the default
    'quality' => 20, // compresion strength from 0 to 100 where 100 is the heighest quality and 75 is the default
    'maxWidth' => 1200, // max width allowed for image
    'optymizeFileLargerThen' => 15 // optymise files larger then 15 Kb
];
searchForFiles($path, $extRegEx, $recursive, $options);

答案 4 :(得分:0)

这是一个包含视频教程的完整脚本。 https://a1websitepro.com/optimize-images-with-php-in-a-directory-on-your-server/

switch(strtolower(image_type_to_mime_type($type)))
{
case 'image/jpeg':
$NewImage = imagecreatefromjpeg($SrcImage);
break;
case 'image/JPEG':
$NewImage = imagecreatefromjpeg($SrcImage);
break;
case 'image/png':
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/PNG':
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/gif':
$NewImage = imagecreatefromgif($SrcImage);
break;
default:
return false;
}