Menu

[55c200]: / lib / Thumbnailer / CreateThumb.php  Maximize  Restore  History

Download this file

101 lines (78 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
<?php
class Thumbnailer_CreateThumb {
/**
* Create thumbnail of an image
* @param string Path of source image
* @param string Destination path of thumbnail
* @param number Width of thumbnail
* @param number Height of thumbnail
* @param number (optional) thumbnail image format : JPG, GIF, PNG
*/
public static function createThumbnail($sourcePath, $destPath, $width, $height, $q = 'JPG', $crop = false, $options = array()) {
try {
$destDir = dirname($destPath);
if(!is_dir($destDir)) { mkdir($destDir, 0777); }
list($max_width, $max_height) = getimagesize($sourcePath);
$ratio = $max_width/$max_height;
if ($ratio<1) $height = intval($height/$ratio);
else $width = intval($width*$ratio);
$options['correctPermissions'] = true;
$thumb = Thumbnailer_ThumbLib::create($sourcePath, $options);
// if(!empty($crop)) $thumb->crop($crop['x'], $crop['y'], $w, $h);
$thumb->resize($width, $height);
if($crop) {
$thumb->cropFromCenter($width, $height);
}
$thumb->save($destPath, $q);
}
catch(Exception $e) {
$destPath = $e->getMessage();
}
return $destPath;
}
public static function resize($sourcePath, $destPath, $width, $height, $q = 'JPG', $crop = false) {
try {
$destDir = dirname($destPath);
if(!is_dir($destDir)) { mkdir($destDir, 0777); }
list($max_width, $max_height) = getimagesize($sourcePath);
$options = array('correctPermissions' => true, 'resizeUp' => true);
$thumb = Thumbnailer_ThumbLib::create($sourcePath, $options);
$width_ratio = $max_width / $width;
$height_ratio = $max_height / $height;
if($max_width < $width OR $max_width < $height)
{
if($width_ratio > $height_ratio) {
$thumb->resize(0, $height);
} else {
$thumb->resize($width, 0);
}
if($crop) {
$thumb->crop(0, 0, $width, $height);
}
} else {
$thumb->resize($width, $height);
}
$thumb->save($destPath, $q);
}
catch(Exception $e) {
$destPath = $e->getMessage();
}
return $destPath;
}
public static function crop($sourcePath, $destPath, $x, $y, $width, $height, $fromCenter = false) {
try {
if(empty($destPath)) $destPath = $sourcePath;
$destDir = dirname($destPath);
if(!is_dir($destDir)) { mkdir($destDir, 0777); }
// Crop l'image
$thumb = Thumbnailer_ThumbLib::create($sourcePath, array('correctPermissions' => true));
if($fromCenter) $thumb->cropFromCenter($width, $height);
else $thumb->crop($x, $y, $width, $height);
$thumb->save($destPath);
}
catch(Exception $e) {
$destPath = null;
}
return $destPath;
}
}