Превью картинок

  1. <?php
  2. function resize ($filename, $size)
  3. {
  4. $pref = 'mini_';
  5. $img = strtolower(strrchr(basename($filename), "."));
  6. $imgname = basename($filename);
  7. $formats = array('.jpg', '.gif', '.png', '.bmp');
  8. if (in_array($img, $formats))
  9. {
  10. list($width, $height) = getimagesize($filename);
  11. $new_height = $height * $size;
  12. $new_width = $new_height / $width;
  13. $thumb = imagecreatetruecolor($size, $new_width);
  14. switch ($img)
  15. {
  16. case '.jpg': $source = @imagecreatefromjpeg($filename); break;
  17. //создаем превьюшки
  18. case '.gif': $source = @imagecreatefromgif($filename); break;
  19. case '.png': $source = @imagecreatefrompng($filename); break;
  20. case '.bmp': $source = @imagecreatefromwbmp($filename); break;
  21. }
  22. imagecopyresized($thumb, $source, 0, 0, 0, 0, $size, $new_width, $width, $height);
  23. switch ($img)
  24. {
  25. case '.jpg': imagejpeg($thumb, $pref.$imgname); break;
  26. case '.gif': imagegif($thumb, $pref.$imgname); break;
  27. case '.png': imagepng($thumb, $pref.$imgname); break;
  28. case '.bmp': imagewbmp($thumb, $pref.$imgname); break;
  29. }
  30. }
  31. else return 'Error';
  32. @imagedestroy($thumb);
  33. @imagedestroy($source);
  34. return $imgname;
  35. }
  36. ?>
Вот пример использования
echo '<img src="mini_'.resize('http://www.moysait.rf/test.jpg',200).'" />';

Реклама

Мы в соцсетях

tw tg yt gt