Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This accepts two parameters, the file path to the image that you want
- * to create a thumbnail of ($file) and the directory path and file name
- * where you want to put the thumbnail ($directory).
- * For the purposes of this module, the directory path to the images are:
- * ../images/mypictures/example.jpg
- * and the place to put the thumbnail is:
- * ../images/mypicturesthums/thumb_example.jpg
- * Returns true if everything has worked out okay, false if there has been
- * a problem when calling the function
- * @param string $file
- * @param string $directory
- **/
- public function createThumb( $file='', $directory = '' ) {
- // Booleans used to set the type of images that we'll deal with later
- // (all false by default):
- $jpeg = false; $png = $jpeg; $gif = $jpeg;
- // Gets the type of image from the array:
- $type = $this->imgData['exts'];
- // Checks for each case to ensure that the relevant filetype is called:
- switch($type) {
- case 'gif':
- $img = imagecreatefromgif($file);
- // Checks for transparency:
- $transparentIndex = imagecolortransparent($img);
- if($transparentIndex!=(-1)) {
- $transparentCol = imagecolorsforindex($img, $transparentIndex);
- }
- $gif = true;
- break;
- case 'jpeg':
- case 'jpg':
- $img = imagecreatefromjpeg($file);
- $jpeg = true;
- break;
- case 'png':
- $img = imagecreatefrompng($file);
- // Checks for alpha channel:
- imagealphablending($img, true);
- imagesavealpha($img, true);
- $png = true;
- break;
- default:
- return false;
- break;
- }
- // This should be the same as saying $newX = imagesx($img)/8, but might
- // be a bit quicker:
- $newX = imagesx($img) >> 3;
- $newY = imagesy($img) >> 3;
- // Creates instance of thumbnail at 1/8 the size, and scales the parent image appropriately:
- $small = imagecreatetruecolor($newX, $newY);
- imagecopyresampled($small, $img, 0, 0, 0, 0, $newX, $newY, imagesx($img),imagesy($img));
- // Saves new image according to type:
- if($jpeg) {
- imagejpeg($small, $directory, 100);
- } else if ($gif) {
- imagegif($small, $directory, 100);
- } else if ($png) {
- imagepng($small, $directory, 100);
- } else {
- // If no boolean is set then something has gone wrong:
- return false;
- }
- // Releases resources:
- imagedestroy($img);
- imagedestroy($small);
- // You success:
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement