Still loading
logo
logo
logo
logo
logo
logo
logo
logo
logo
logo
logo

Resize large images on Linux


Advertisement
Resize large images on Linux – recursive Sometimes, we are in a situation, when client wanted to host his website under our environment. Our needs are that website must not use nearly unlimited files. In days of heavy-pixel cameras, it is not a problem to see images with 50+ MB images in size, which may cause many problems. Probably the worst is using hard drive space, followed by a need to backup insanely large directories, which takes time, and may harm another websites on the same server due to high load during backup. That’s why we use this script below, which use BASH combined with PHP functions, to check all client’s websites, and resize images, that are insanely large in size.  You can change any parameters as you need, of course you need approval from your clients to change th..

Sign up


By registering I agree with your terms

400 tracked servers
576,000 pings / day
51 server outages today

Resize large images on Linux – recursive

Sometimes, we are in a situation, when client wanted to host his website under our environment. Our needs are that website must not use nearly unlimited files. In days of heavy-pixel cameras, it is not a problem to see images with 50+ MB images in size, which may cause many problems. Probably the worst is using hard drive space, followed by a need to backup insanely large directories, which takes time, and may harm another websites on the same server due to high load during backup. That’s why we use this script below, which use BASH combined with PHP functions, to check all client’s websites, and resize images, that are insanely large in size.  You can change any parameters as you need, of course you need approval from your clients to change their image size on-the-fly. By resizing images once, their CMS systems does not need to create thumbnails on user access, so even their webpages are loaded much faster (at least on the first page load or website recaching).

#!/usr/bin/php
ini_set("memory_limit","4096M");
print_r(getDirContents('/dir-to-delete'));

function getDirContents($dir){
    $results = array();
    $files = scandir($dir);

    foreach($files as $key => $value){
        if ($value=="..") {
            
        } else if ($value==".") {} else if(!is_dir($dir. DIRECTORY_SEPARATOR .$value)){
            processFile($dir."/".$value);
            $results[] = $value;
        } else if(is_dir($dir. DIRECTORY_SEPARATOR .$value)) {
            $results[] = $value;
            getDirContents($dir. DIRECTORY_SEPARATOR .$value);
        }
    }
}

function processFile($file) {
    $fs=strtolower($file);
    if (substr($fs,-4)==".jpg") {
        echo $file."\n";
        $size=filesize($file);
        
        if ($size>300000) {
            $dim=getimagesize($file);
            $fn=$file."-small";
            resizeImage($file,$fn,1600,1200,80);
            
            echo "Size: ".$size." ".$dim[0]."x".$dim[1]."\n";
            if (is_file($fn)) {
                unlink($file);
                rename($fn,$file);
                $size=filesize($file);
                echo "New size: ".$size."\n";
            }
            
        }
    }
}

function resizeImage($sourceImage, $targetImage, $maxWidth, $maxHeight, $quality = 80)
{
    // Obtain image from given source file.
    if (!$image = @imagecreatefromjpeg($sourceImage))
    {
        return false;
    }

    // Get dimensions of source image.
    list($origWidth, $origHeight) = getimagesize($sourceImage);

    if ($maxWidth == 0)
    {
        $maxWidth  = $origWidth;
    }

    if ($maxHeight == 0)
    {
        $maxHeight = $origHeight;
    }

    // Calculate ratio of desired maximum sizes and original sizes.
    $widthRatio = $maxWidth / $origWidth;
    $heightRatio = $maxHeight / $origHeight;

    // Ratio used for calculating new image dimensions.
    $ratio = min($widthRatio, $heightRatio);

    // Calculate new image dimensions.
    $newWidth  = (int)$origWidth  * $ratio;
    $newHeight = (int)$origHeight * $ratio;

    // Create final image with new dimensions.
    $newImage = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
    imagejpeg($newImage, $targetImage, $quality);

    // Free up the memory.
    imagedestroy($image);
    imagedestroy($newImage);

    return true;
}
?>



From Our Blog

  1. Cloud server hosting (4 months ago)
  2. Up/Down test (3 months ago)
  3. Web hosting plans (2 months ago)
  4. Web Hosting Company Directory (2 months ago)
  5. Resize large images on Linux (1 months ago)
  6. PID file check script in PHP (1 months ago)
  7. What is SolarEdge monitoring (1 months ago)
  8. XBOX ONE - Streaming to Windows 10 (1 months ago)
  9. 6 Plugins for Import - Exporting Products from WooCommerce (1 months ago)
  10. Proč se v USA hledá „facebook iniciar sesion“? (1 months ago)
  11. Looking for facebook initiar sesion (1 months ago)
  12. The end of 32 bits: support for old architecture on Linux is ending (1 months ago)
  13. Security perceptions: the ATM opens the gates (1 months ago)
  14. How to work with mobile targeting on PPC Facebook ads? (1 months ago)
  15. WWW and its capabilities (1 months ago)
  16. Ten of the best games for Linux for the past 10 years. (1 months ago)
  17. Does your facebook profile repel your customers? (1 months ago)

Free variant

Free

  • Personal usage
  • 1 website
  • 10 minutes interval

Basic variant

JPY 1,569 / Mo

  • Pro's
  • Up to 100 websites
  • 1 minute interval

Unlimited variant

JPY 9,020 / Mo

  • Pro's
  • Up to 1000 websites
  • 10 seconds interval

Sign up


By registering I agree with your terms


↑ Scroll Up ↑