| 1 |
<?php
|
| 2 |
// $Id:
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This class provides a very simple cache for the thumbnails, in case
|
| 7 |
* Imagecache is not being used (or can't be used).
|
| 8 |
*
|
| 9 |
* @author Raphael Schär - www.schaerwebdesign.ch
|
| 10 |
* @author Nicholas C. Yang - www.nyanginator.com
|
| 11 |
*/
|
| 12 |
|
| 13 |
class FastGalleryCache {
|
| 14 |
static private $instance = null;
|
| 15 |
|
| 16 |
/**
|
| 17 |
* We are implementing a singleton pattern
|
| 18 |
*/
|
| 19 |
private function __construct() {
|
| 20 |
}
|
| 21 |
|
| 22 |
public function getInstance() {
|
| 23 |
if (is_null(self :: $instance)) {
|
| 24 |
self :: $instance = new self;
|
| 25 |
}
|
| 26 |
return self :: $instance;
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Creates an image resource from a path. Supported filetypes are GIF,
|
| 31 |
* JPEG, and PNG.
|
| 32 |
*
|
| 33 |
* $param path
|
| 34 |
* Path to the image.
|
| 35 |
*/
|
| 36 |
function imagecreatefromfile($path) {
|
| 37 |
$info = getimagesize($path);
|
| 38 |
|
| 39 |
$types = array (
|
| 40 |
IMAGETYPE_GIF => 'imagecreatefromgif',
|
| 41 |
IMAGETYPE_JPEG => 'imagecreatefromjpeg',
|
| 42 |
IMAGETYPE_PNG => 'imagecreatefrompng',
|
| 43 |
|
| 44 |
);
|
| 45 |
|
| 46 |
if (!$info || !$types[$info[2]] || !function_exists($types[$info[2]])) {
|
| 47 |
return false;
|
| 48 |
}
|
| 49 |
|
| 50 |
return $types[$info[2]] ($path);
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Creates a thumb and copies it into the same folder with .thumb extension.
|
| 55 |
*
|
| 56 |
* @param name
|
| 57 |
* Name of the original image.
|
| 58 |
* @param width
|
| 59 |
* Desired width of the thumbnail.
|
| 60 |
* @param height
|
| 61 |
* Desired height of the thumbnail.
|
| 62 |
*/
|
| 63 |
public function createthumb($name, $width, $height) {
|
| 64 |
$name = utf8_decode($name);
|
| 65 |
|
| 66 |
// Return without doing anything if the thumbnail already exists
|
| 67 |
if (file_exists($name . '.thumb'))
|
| 68 |
return true;
|
| 69 |
|
| 70 |
// We're only supporting JPG, PNG, GIF
|
| 71 |
$src_img = $this->imagecreatefromfile($name);
|
| 72 |
if ($src_img == FALSE) {
|
| 73 |
drupal_set_message('Internal cache failed to create thumbnail: ' . $name);
|
| 74 |
return;
|
| 75 |
}
|
| 76 |
|
| 77 |
// New dimensions
|
| 78 |
$width_orig = imagesx($src_img);
|
| 79 |
$height_orig = imagesy($src_img);
|
| 80 |
$ratio_orig = $width_orig / $height_orig;
|
| 81 |
|
| 82 |
if ($width / $height > $ratio_orig) {
|
| 83 |
$width = $height * $ratio_orig;
|
| 84 |
} else {
|
| 85 |
$height = $width / $ratio_orig;
|
| 86 |
}
|
| 87 |
|
| 88 |
// Resample
|
| 89 |
$dst_img = imagecreatetruecolor($width, $height);
|
| 90 |
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
|
| 91 |
|
| 92 |
// Save out the thumbnail file as a JPEG
|
| 93 |
imagejpeg($dst_img, $name . '.thumb');
|
| 94 |
|
| 95 |
// Some cleanup
|
| 96 |
imagedestroy($dst_img);
|
| 97 |
imagedestroy($src_img);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Removes all the thumbs.
|
| 102 |
*
|
| 103 |
* @param path
|
| 104 |
* Path of directory where thumbs are located.
|
| 105 |
* @param recursive
|
| 106 |
* Specify whether to recurse through subdirectories.
|
| 107 |
*/
|
| 108 |
public function flushThumbs($path = '', $recursive = TRUE) {
|
| 109 |
if ($path == '') {
|
| 110 |
$path = variable_get('fast_gallery_path', 'sites/default/files');
|
| 111 |
}
|
| 112 |
// Get all .thumb files from given path
|
| 113 |
$files = glob($path . '*.thumb');
|
| 114 |
|
| 115 |
// Delete the thumbs in this path, skip over directories (arrays)
|
| 116 |
if (is_array($files)) {
|
| 117 |
foreach ($files as $img) {
|
| 118 |
if (!is_array($img))
|
| 119 |
unlink($img);
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
// Recurse through subdirectories if necessary
|
| 124 |
if ($recursive) {
|
| 125 |
$dirs = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR);
|
| 126 |
foreach ($dirs as $dir) {
|
| 127 |
$this->flushThumbs($dir, TRUE);
|
| 128 |
}
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|