Hello, please sign in or register
You are here: Home

Image Resizing

Preface

I created this script for a books site, it uses the PHP GD functions to resize a batch of images but this is the process for a single image file. A (zip) directory walk is required and you can make this into a batch process

The Code

<?php

$params['settings'] = array
    (
        'orig'    => array
        (
            'dir'       => DIR_IMAGES.'300/',
            'type'      => 'copy'
        ),
        array
        (
            'maxheight' => 120,
            'maxwidth'  => 120,
            'bgcolour'  => '#FFFFFF',
            'dir'       => DIR_IMAGES.'100/',
            'type'      => 'resize'
        ),
        array
        (
            'maxheight'	=> 150,
            'maxwidth'	=> 150,
            'bgcolour'	=> '#FFFFFF',
            'dir'       => DIR_IMAGES.'150/',
            'type'      => 'resize'
        )
   );

resizeandsave($path);                

function resizeandsave($imagefile)
{
    global $exc, $counter, $break, $params, $stop;

    foreach($params['settings'] as $param)
    {

        if($param['type'] != 'resize')
        {
        	continue;
        }
        
        $image = imagecreatefromjpeg($imagefile);  
        $size = getimagesize($imagefile);
        $imagename = basename($imagefile);

		// Determine the sixe of the image
        if($size[0] > $size[1]){
            $w = $param['maxwidth'];
            $h = (int)($param['maxwidth'] * $size[1] / $size[0]);
        }
        else {
            $w = (int)($param['maxheight'] * $size[0] / $size[1]);
            $h = $param['maxheight'];
        }
		// creating the destination image with the new Width and Height
        $newimage = imagecreatetruecolor($w, $h);

		// copy image over to new image with its new size
        imagecopyresized($newimage, $image, 0,0,0,0, $w, $h, $size[0], $size[1]);

		// Save the new image calling a user defined function imagepath to createt the folder
        if(!imagejpeg($newimage,imagepath($imagefile, $param['dir']),100)){
			// else create error
            report("ERROR: Creating Resized Image : ".$imagefile);exit();
        }
        imagedestroy($image);
    }
}
?>

Comments

mulberry outlet
Created 25/05/12
The beautiful and very simple
This is a really good site post, im delighted I came across it. beats by dre http://www.monsterbeatsbydreoutlet.co.uk fashion
Created 09/10/12
Louis Vuitton flagshop opened since
Louis Vuitton Handbags http://www.cheapestlouisvuittonoutlet.org and so on They have become popular with women and girls of all ages Do your own research online today -- the possibilities for fab and fashion are endless with this historic designer la...
Created 23/10/12
Louis Vuitton flagshop opened since
Louis Vuitton Handbags http://www.cheapestlouisvuittonoutlet.org and so on They have become popular with women and girls of all ages Do your own research online today -- the possibilities for fab and fashion are endless with this historic designer la...
Created 24/10/12
1015maoqiuyun
coach outlet online, http://www.coachoutletonline-store.us.com/
Created 15/10/15
ninestab123
ninest123 One canada goose pas cher gucci outlet thing
Created 21/07/16
Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share