note 48684 added to function.imagecreatefromjpeg
| From: | php-general at lists dot php dot net | Date: | Tue, 04 Jan 2005 18:53:41 +0000 |
| Subject: | note 48684 added to function.imagecreatefromjpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82711@lists.php.net to get a copy of this message | ||
Hi, here is my method for converting a folder full of jpg's to a specified thumnail size.
some requirements that can be changed in the code are;
folder structure should follow;
-main folder
|-code files
|-"images" folder
|-"thumbs" folder
the file naming convention under the images folder should be
-"images" folder
|-001.JPG
|-002.JPG
|-003.JPG
|-so on up to 999.JPG
here is the code I use to create the thumbs into the "thumbs folder"
note: to display the images youll need to write your own customized code.
Whats good about this is just upload the pics to the images folder, run the
code in the parent folder and all the thumbs will be created automatically in the thumbs folder...
hope this helps someone... and im sure it can be optimized...
*************************************8
<?php
//count the number of files the passed folder
function file_count($dir) {
$count = 0;
if (substr($dir, -1) != DIRECTORY_SEPARATOR) $dir .= DIRECTORY_SEPARATOR;
if ($dirhwnd = opendir($dir)) {
while(($file = readdir($dirhwnd)) !== false) {
$path = $dir . $file;
if (is_file($path)) ++$count;
}
}
return $count;
}
//sets $images number of files in the passed folder
$num_of_images=(file_count("images"))+1;
for ($i=1 ; $i<$num_of_images ; $i++)
{
if ($i < 10) {$image = '00' . $i . '.JPG';}
if (( $i > 9 ) && ( $i < 100 )) {$image = '0' . $i . '.JPG';}
if ( $i > 99 ) {$image = $i . '.JPG';}
$sourcePath = 'images/'; // Path of original image
$sourceName = $image; // Name of original image
$thumbPath = 'thumbs'; // Writeable thumb path
$thumbName = 'TN_' . $sourceName; // thumbnail name
$thumbWidth = 125; // Intended width of thumb
$thumbHeight = 94; // Intended height of thimb
// Beyond this point is simply code.
$sourceImage = imagecreatefromjpeg("$sourcePath/$sourceName");
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
if ( $sourceHeight > $sourceWidth )
{
$thumbWidth = 94; // Intended width of thumb
$thumbHeight = 125; // Intended height of thimb
}
$targetImage = imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($targetImage,$sourceImage,0,0,0,0,$thumbWidth,
$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));
imagejpeg($targetImage, "$thumbPath/$thumbName");
$thumbPath1 = 'thumbs/';
echo "<img src='$thumbPath1$thumbName' alt=''>";
}
?>
**************************
----
Manual Page -- http://www.php.net/manual/en/function.imagecreatefromjpeg.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48684
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48684&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48684&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48684&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48684&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48684&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48684&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48684&report=yes
Search -- http://master.php.net/manage/user-notes.php