PHP Code to generate thumbnails of all images in current directory


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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.  
  5. $path = getcwd()."/"; // path to folder full of jpg files to convert
  6.  
  7. $n = 0;
  8.  
  9. if ($handle = opendir($path)) {
  10. while (false !== ($file = readdir($handle))) {
  11. if ($file != "." && $file != ".." && strpos($file,"jpg") !== false) {
  12. echo $file."\n";
  13. $originalname = $file;
  14.  
  15. $filenamePart = $n++;
  16.  
  17. $squarename = $filenamePart."_sq.jpg";
  18. $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."\"";
  19. system($squareCommand);
  20. }
  21. }
  22. closedir($handle);
  23. }
  24. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.