Solved

resize local image files using a script

Posted on 2010-08-30
3
288 Views
Last Modified: 2012-05-10
Does anyone know of a way to loop through a directory of images on a windows file system and resize some images?  I would like to automate this and not have to resize each image using Gimp or something like that.  Unfortunately, I can't use a file upload plugin/php to resize images since my max file upload is 8mb and that can't be changed as my hosting site wont increase it.  
0
Comment
Question by:cgray1223
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Snarfles earned 500 total points
ID: 33564309
Got the perfect script for you.

Well the purpose of this one is for creating thumbs... but you can alter it slightly if you want.

If you want to override the originals then just change $_thpath = "images/thumb/"; to the same as the original path.

Use these lines to determine the sizes

define("THUMB_MAX_WIDTH", 400);
define("THUMB_MAX_HEIGHT", 400);

also this is setup for png... but you can do gif or jpg as well by just changing a couple of things.

if (strtolower($fileext) == 'png')
<?php

/***************************************************************************

* Version 1.0.1.0

* Changes from Version 1.0.0.1

       -  Fixed some other notice problems

       -  Added Fresh thumbnail option slow excution but good when gallary photos change

          over short time - delete old files in thumb directory

       -  Added my name under the gallery -- it can be removed easly

* Changes from version 1

*      - Fixed Notice of php v5.2.7

*  This is a photo gallary that works with JPG

*  Only for Now It is very simple you may use it as stand alone

*  or embed it inside another php using include or HTML (inline)

*  just change the config section and you are ready ....

*  Note:

*      It is nessessary to make the dir name that holds your photos 

*      reletive to running scripts caller

*      Both dir names must ends with "/" it may start with "." or ".."

*      If the galley dir not exists Php error will be produced I have done 

*      nothing to stop it just for you to Know that there is somthing wrong 

*      on your configration.    

*  Feel free to redistribute change modify or improve... it is apsoulutly free

*  of all means no charges no copyrights... nothing just use it if it suits you

*  And send me email to show me where you are using it..If you wish!!!! not compalsary!!!!

*  Part of this Gallary is "Light box" the copyrights and every thing concern it  you will find on

*  JS Dir

*  Last modified on 06/12/2008  

*  Adil Haimoura .... adil@tamidors.com

*******************************************************************************/

// if you are using this file as embed or included move the head part to your page

// for light box to work





?>

<?php



// config section

$title = "";

$_Nocoloumns = 2;

$_Borders = 0;

$_Bcolors = "#ffcc44";

$_path = "images/";

$_thpath = "images/thumb/";

$count = 1;

$_headerfontcolor = "#339966";

$_tableBgcolor = "#1a1a1a";

$_freshthumb = "off";

// end configration



// Define a path - We are assuming that this will be a relative path working

//  both in the filesystem, and through the web for links:

$path = $_path;

$thpath = $_thpath;

// Open the directory

$do = dir($path);

// now check if the thumb dir is available if not, create it!!

if (!is_dir($thpath))

{

				mkdir($_thpath);

}

if ($_freshthumb=="on") // delete old thumbnails and start over the thumbs

{

	$do2=dir($thpath);

	while (($file2=$do2->read())!== false )

{

	if (is_dir($thpath . $file2))

				{

								continue; //this is directory skip it to next

				}else{

					// found old thumb delete the file

					unlink($thpath.$file2);				

				}

				

}

}



// intialize the table

echo ("<table border=$_Borders align=\"center\" >\n<tr>\n");

//echo ("<table border=$_Borders align=\"center\" bordercolor=$_Bcolors bgcolor=$_tableBgcolor>\n<tr>\n");

?>

<td colspan=<?php



echo $_Nocoloumns;



?> align="center"><h1><font color=<?php



echo $_headerfontcolor;



?> >

    <?php

//Not needed

//echo $title;

?>

    </font></h1></td>

</tr>

<tr>

  <?php



$xt = 1;

// Loop through the dir, looking for any/all JPG files:

while (($file = $do->read()) !== false)

{

			$approved = 0;

			//make sure the banner has been approved

			for ( $counter = 0; $counter <= count($approved_banners); $counter++) {

				

				if ($approved_banners[$counter].".png" == $file){

					$approved = 1;

					$filename = $approved_banners[$counter];

				}

				

			

			}

			if ($approved == 1){

				// Parse this for the extension

				//check if there is a directory inside our directory this will create a notice in php 5.2.7 when                     // proccessing the file list ... just skip all directorys

				if (is_dir($path . $file))

				{

								continue; //this is directory skip it to next

				}

				else

				{

								$info = pathinfo($path . $file);



								$fileext = $info['extension'];

								// Continue only if this is a Jpeg

								//if (strtolower($fileext) == 'jpg')

								if (strtolower($fileext) == 'png')

								{

												// We found one!  Start a CELL to display & link to this:

												if ($count == $_Nocoloumns + 1)

												{

																$count = 1;

																echo "<tr>\n";

																

												}

												

												echo "<td align=center ><a href=\"{$path}{$file}\" rel=\"lightbox[myalbum]\">\n";

												//echo "<td align=center width=200 height=90 ><a href=\"{$path}{$file}\" rel=\"lightbox[myalbum]\">\n";

												echo "<td align=center ><a href=\"#\">\n";

												

												if (!is_file($thpath . $file))

												{

												// CREATE THE THUMBER NAIL TO VIEW AND SAVE FOR FUTRE USE

																thumbup($_path, $file, $_thpath);

																$sf = $_thpath . $file;

														//echo "<img src=\"$sf\" align=\"center\" ><br> $file</a><br>";

														echo "<img src=\"$sf\" align=\"center\" > </a><br>";

														

														echo "</td>\n";

														

														$count++;

														if ($count == $_Nocoloumns + 1)

																{

																		echo "</tr>\n";

																		



																}

												}

												else

												{

														$sf = $_thpath . $file;

														// Allready exist thumbnail no need to recreate just display

														//echo "<img src=\"$sf\" align=center ><br> $file</a><br>";

														echo "<img src=\"$sf\" align=center ></a><br>";

														//NAME IS ADDED HERE

																echo "</td>\n";

																

																$count++;

																if ($count == $_Nocoloumns + 1)

																{

																     echo "</tr>\n";

																	 



																}



												}

								}

				}

			}



}



?>

  </table>



<?php



// if you are using this file as embed or included move the java script part below to end your page

// for light box to work





?>

<!--<script type="text/javascript">

			initLightbox();

		</script>-->

<?php



function thumbup($dirn, $file, $thumpdir)

{

	// define the thumbernail dimention

								if (!defined('THUMB_MAX_WIDTH'))

								{

								//define("THUMB_MAX_WIDTH", 80);

								define("THUMB_MAX_WIDTH", 400);

								 }

								 	if (!defined('THUMB_MAX_HEIGHT'))

								{

								//define("THUMB_MAX_HEIGHT", 80);

								define("THUMB_MAX_HEIGHT", 400);

								}

								/*

								if (!defined('THUMB_MAX_WIDTH'))

								{

								define("THUMB_MAX_WIDTH", 160);

								 }

								 	if (!defined('THUMB_MAX_HEIGHT'))

								{

								define("THUMB_MAX_HEIGHT", 160);

								}*/

							

				$filename = $dirn . $file;

				$thfilename = $thumpdir . $file;

				// get the filename and the thumbernail directory

				if (is_file($filename))

				{

								// create the thumbernail from the original picture

								//$im = ImageCreateFromJPEG($filename);

								$im = ImageCreateFrompng($filename);

								$width = ImageSx($im); // Original picture width is stored

								$height = ImageSy($im); // Original picture height is stored

								

							

								if (($width < THUMB_MAX_WIDTH) and ($height < THUMB_MAX_HEIGHT))

								{

									$n_height = $height;

									$n_width = $width;

								}

								else

								{

									// saveing the aspect ratio

									$aspect_x = $width / THUMB_MAX_WIDTH;

									$aspect_y = $height / THUMB_MAX_HEIGHT;

									if ($aspect_x > $aspect_y)

									{

									$n_width = THUMB_MAX_WIDTH;

									$n_height = $height / $aspect_x;

									}

									else

									{

									$n_height = THUMB_MAX_HEIGHT;

									$n_width = $width / $aspect_y;

									}

								}



								$newimage = imagecreatetruecolor($n_width, $n_height);

								// resizing the picture

								imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);



								// writing to file the thumbnail

								Imagejpeg($newimage, $thfilename, 100);



								imagedestroy($newimage);

								imagedestroy($im);



				}

}

Open in new window

0
 

Author Comment

by:cgray1223
ID: 33564322
I'm new to php, so sorry if this is a bit of a dumb question, but I would have to setup php locally on my windows machine and I couldn't leverage the version on my remote host, right?  
0
 
LVL 9

Assisted Solution

by:Snarfles
Snarfles earned 500 total points
ID: 33564332
Yep thats what you would need to do, its a bit outside the scope of this question though. But what you'll probably want is to checkout the lamp install.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now