Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function _makeThumbnail($image, $dest, $ext)
- {
- $imageType = exif_imagetype($image);
- switch ($imageType)
- {
- case IMAGETYPE_JPEG:
- $img = imagecreatefromjpeg($image);
- break;
- case IMAGETYPE_PNG:
- $img = imagecreatefrompng($image);
- break;
- case IMAGETYPE_GIF:
- $img = imagecreatefromgif($image);
- break;
- default:
- throw new Exception('Bad extension');
- }
- $width = imagesx($img);
- $height = imagesy($img);
- list($widthX, $heightX) = array('130', '130');
- if ($width > $widthX || $height > $heightX)
- {
- if ($height > $width)
- {
- $ratio = $heightX / $height;
- $newHeight = $heightX;
- $newWidth = $width * $ratio;
- }
- else
- {
- $ratio = $widthX / $width;
- $newWidth = $widthX;
- $newHeight = $height * $ratio;
- }
- $previewImg = imagecreatetruecolor($newWidth, $newHeight);
- $palsize = ImageColorsTotal($img);
- for ($i = 0; $i < $palsize; $i++)
- {
- $colors = ImageColorsForIndex($img, $i);
- ImageColorAllocate($previewImg, $colors['red'], $colors['green'], $colors['blue']);
- }
- imagecopyresized($previewImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
- $name = $dest;
- switch ($imageType)
- {
- case IMAGETYPE_JPEG:
- imagejpeg($previewImg, $name . '.' . $ext, 100);
- break;
- case IMAGETYPE_PNG:
- imagesavealpha($previewImg, true);
- imagepng($previewImg, $name . '.' . $ext, 9);
- case IMAGETYPE_GIF:
- imagegif($previewImg, $name . '.' . $ext);
- break;
- default:
- throw new Exception();
- }
- }
- imagedestroy($previewImg);
- imagedestroy($img);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement