note 91117 rejected from function.imagecopyresized by thiago
| From: | thiago@php.net | Date: | Tue, 26 May 2009 11:19:10 +0000 |
| Subject: | note 91117 rejected from function.imagecopyresized by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156016@lists.php.net to get a copy of this message | ||
Note Submitter: akbittu at gmail dot com
----
<?php
/*
Image Resizer v 1.0.0
Copyright (c) 2008, 2009 Abhishek Kumar.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Feature:-
* Resize images in batch.
* Customize resizing dimensions.
* Maintain aspect ratio.
Steps to use this script :-
* Step-1: Create a folder and name it 'Container'.
* Step-2: Put all the images in this folder that you want to resize.
* Step-3: Run the script to resize those images.
*/
echo '<b>Image Resizer v 1.0.0 </b><br><br>';
ImageResizingDirectory("./Container");
function ImageResizingDirectory($iFolder)
{
if ($DirectoryHandle = opendir($iFolder))
{
while (false !== ($Entry = readdir($DirectoryHandle)))
{
if ($Entry != '.' && $Entry != '..')
{
$FilePath = ltrim($iFolder.'/'.$Entry, './');
DisplayStatus('S001', $FilePath);
/*
Argument definition of ImageResizer:
* Argument-1: Image-File-Path,
* Argument-2: Image-Resizing-Width,
* Argument-3: Image-Resizing-Height.
**/
ImageResizer($FilePath, 160, 120);
//
DisplayStatus('S002', $FilePath);
}
}
closedir();
}
}
function DisplayStatus($iMsgId, $iMsgArg)
{
switch($iMsgId)
{
case 'S001': echo str_replace("[Content]", 'Processing image at
<i>'.$iMsgArg.'</i>.', "[Content]<br>");
break;
case 'S002': echo str_replace("[Content]", 'Image at
<i>'.$iMsgArg.'</i> is resized.', "[Content]<br>");
break;
default: echo '<br>';
}
}
function ImageResizer($ImageLocation, $ImageResizingWidth, $ImageResizingHeight)
{
list($PictureWidth, $PictureHeight) = getimagesize($ImageLocation);
$PictureAspectRatio = $PictureWidth/$PictureHeight;
$Width = $ImageResizingWidth;
$Height = $ImageResizingHeight;
$AspectRatio = $Width/$Height;
if ($PictureAspectRatio > $AspectRatio)
{
$NewWidth = $Width;
$NewHeight = $Width/$PictureWidth * $PictureHeight;
}
else if ($PictureAspectRatio < $AspectRatio)
{
$NewWidth = $Height/$PictureHeight * $PictureWidth;
$NewHeight = $Height;
}
else if ($PictureAspectRatio == $AspectRatio)
{
$NewWidth = $Width;
$NewHeight = $Height;
}
$thumb = imagecreatetruecolor($NewWidth, $NewHeight);
$source = imagecreatefromjpeg($ImageLocation);
imagecopyresized($thumb, $source, 0,0,0,0, $NewWidth, $NewHeight, $PictureWidth, $PictureHeight);
imagejpeg($thumb, $ImageLocation, 50);
imagedestroy($thumb);
imagedestroy($source);
}
?>