stuppid_bot

Создаем превьюшки для изображений

Apr 26th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.78 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0);
  4. $directory = 'img/mems';
  5. $images = array_filter( scandir($directory), function($name) { return preg_match('/\.jpg/i', $name); } );
  6. $size = 150;
  7.  
  8. foreach ($images as $image) {
  9.     $src = imagecreatefromjpeg("$directory/$image");
  10.     $width = imagesx($src);
  11.     $height = imagesy($src);
  12.     $proportion = $width / $height;
  13.    
  14.     if ($width > $height) {
  15.         $thumb_width = $size;
  16.         $thumb_height = $size / $proportion;
  17.     }
  18.     else {
  19.         $thumb_width = $size * $proportion;
  20.         $thumb_height = $size;
  21.     }
  22.    
  23.     $dst = imagecreatetruecolor($thumb_width, $thumb_height);
  24.     imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
  25.     imagejpeg($dst, "$directory/thumbs/$image");
  26. }
  27.  
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment