Создание превью для изображений
- define('FILESDIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR);
- $path = FILESDIR . 'someimage.png';
- $fname = 'someimage';
- if (extension_loaded('gd')) {
- $imageSize = getimagesize($path);
- $types = array(1 => 'gif', 2=> 'jpg', 3 => 'png');
- $imageType = array_key_exists($imageSize[2], $types) ? $types[$imageSize[2]] : FALSE;
- if ($imageType !== FALSE) {
- $width = $imageSize[0];
- $height = $imageSize[1];
- if ($width > 220 || $height > 176) {
- switch ($imageType) {
- case 'gif': $image = imagecreatefromgif($path); break;
- case 'jpg': $image = imagecreatefromjpeg($path); break;
- case 'png': $image = imagecreatefrompng($path); break;
- default:
- }
- $max = $width > $height ? 'w' : 'h';
- $new_width = 176;
- $new_height = 220;
- if ($max == 'w' && $width > $new_width)
- $new_height = intval(($new_width * $height) / $width);
- if ($max == 'h' && $height > $new_height)
- $new_width = intval(($new_height * $width) / $height);
- $preview = imagecreate($new_width, $new_height);
- imagecopyresized($preview, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- imagepng($preview, FILESDIR . $fname . '_preview.png');
- }
- }
- }