PHP Code to generate thumbnails of all images in current directory

PHP Code to generate thumbnails of all images in current directory


 / Published in: PHP

URL: http://www.randomsequence.com/articles/making-square-thumbnails-with-imagemagick/

Expand | Embed | Plain Text
  1. <?php
  2. $SQUARESIZE = 108; // the size in pixels for your square images
  3. $convertPath = “/usr/local/bin/convert”; // full path to ImageMagick’s convert tool
  4. $path = getcwd().”/”; // path to folder full of jpg files to convert
  5. $n = 0;
  6. if ($handle = opendir($path)) {
  7. while (false !== ($file = readdir($handle))) {
  8. if ($file != “.” && $file != “..” && strpos($file,”jpg”) !== false) {
  9. echo $file.”\n”;
  10. $originalname = $file;
  11. $filenamePart = $n++;
  12. $squarename = $filenamePart.”_sq.jpg”;
  13. $squareCommand = $convertPath.” \””.$path.$originalname.”\” -thumbnail x”.($SQUARESIZE*2).” -resize ‘”.($SQUARESIZE*2).”x&lt;’ -resize 50% -gravity center -crop “.$SQUARESIZE.”x”.$SQUARESIZE.”+0+0 +repage -format jpg -quality 91 \””.$path.”sq/”.$squarename.”\””;
  14. system($squareCommand);
  15. }
  16. }
  17. closedir($handle);
  18. }
  19. ?>

Report this snippet

Advertisements
By Rz Rasel Posted in Php

2 comments on “PHP Code to generate thumbnails of all images in current directory

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s