| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
class ImagesTools |
|---|
| 7 |
{ |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
* Creates a thumbnail from an uploaded image file |
|---|
| 11 |
* |
|---|
| 12 |
* @param sfValidatedFile Validated uploaded file |
|---|
| 13 |
* @throws sfException |
|---|
| 14 |
*/ |
|---|
| 15 |
public static function createThumb(sfValidatedFile $file, |
|---|
| 16 |
$destination_dir, |
|---|
| 17 |
$filename, |
|---|
| 18 |
$max_width = 200, |
|---|
| 19 |
$max_height = 140, |
|---|
| 20 |
$export_mime = 'image/jpeg', |
|---|
| 21 |
$quality = 90, |
|---|
| 22 |
$options = array()) |
|---|
| 23 |
{ |
|---|
| 24 |
if (!is_dir($destination_dir) && !@mkdir($destination_dir)) |
|---|
| 25 |
{ |
|---|
| 26 |
sfContext::getInstance()->getLogger()->err('{ImagesTools} Cannot find nor create dir ' . $destination_dir); |
|---|
| 27 |
throw new sfException('{ImagesTools} Impossible to create directory'); |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
$source_path = $file->getTempName(); |
|---|
| 31 |
|
|---|
| 32 |
switch ($export_mime) |
|---|
| 33 |
{ |
|---|
| 34 |
case 'image/png': |
|---|
| 35 |
case 'image/x-png': |
|---|
| 36 |
$extension = 'png'; |
|---|
| 37 |
break; |
|---|
| 38 |
case 'image/pjpeg': |
|---|
| 39 |
case 'image/jpeg': |
|---|
| 40 |
$extension = 'jpg'; |
|---|
| 41 |
break; |
|---|
| 42 |
case 'image/gif': |
|---|
| 43 |
$extension = 'jpg'; |
|---|
| 44 |
break; |
|---|
| 45 |
default: |
|---|
| 46 |
throw new sfException(sprintf('{ImagesTools} "%s" mime type is not supported', $export_mime)); |
|---|
| 47 |
break; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
$destination = $destination_dir.DIRECTORY_SEPARATOR.$filename; |
|---|
| 51 |
|
|---|
| 52 |
try |
|---|
| 53 |
{ |
|---|
| 54 |
$thumbnail = new sfThumbnail($max_width, $max_height, true, true, $quality, null, $options); |
|---|
| 55 |
$thumbnail->loadFile($source_path); |
|---|
| 56 |
$thumbnail->save($destination, $export_mime); |
|---|
| 57 |
return $filename; |
|---|
| 58 |
} |
|---|
| 59 |
catch (Exception $e) |
|---|
| 60 |
{ |
|---|
| 61 |
sfContext::getInstance()->getLogger()->err(sprintf('{ImagesTools} Error generating thumb from source "%s" to destination "%s": %s', $source_path, $destination, $e->getMessage())); |
|---|
| 62 |
} |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* Batch create thumbs from an input field |
|---|
| 67 |
* |
|---|
| 68 |
* @param string $fieldname |
|---|
| 69 |
* @param string $basedir |
|---|
| 70 |
* @param string $filename |
|---|
| 71 |
* @param array $formats |
|---|
| 72 |
* @return string Generic filename |
|---|
| 73 |
*/ |
|---|
| 74 |
public static function createThumbs($fieldname, $basedir, $filename, $formats) |
|---|
| 75 |
{ |
|---|
| 76 |
$file = null; |
|---|
| 77 |
foreach ($formats as $format => $params) |
|---|
| 78 |
{ |
|---|
| 79 |
$width = isset($params['width']) ? $params['width'] : 160; |
|---|
| 80 |
$height = isset($params['height']) ? $params['height'] : 120; |
|---|
| 81 |
$quality = isset($params['quality']) ? $params['quality'] : 90; |
|---|
| 82 |
$mime = isset($params['mime']) ? $params['mime'] : 'image/jpeg'; |
|---|
| 83 |
$squarize = isset($params['squarize']) ? $params['squarize'] : false; |
|---|
| 84 |
$destination_dir = $basedir.DIRECTORY_SEPARATOR.$format; |
|---|
| 85 |
if (is_file($destination_dir)) |
|---|
| 86 |
{ |
|---|
| 87 |
throw new sfException(sprintf('Resource "%s" is a file', $destination_dir)); |
|---|
| 88 |
} |
|---|
| 89 |
if (!is_dir($destination_dir) && !@mkdir($destination_dir)) |
|---|
| 90 |
{ |
|---|
| 91 |
throw new sfException(sprintf('Unable to create unexistent directory "%s"', $destination_dir)); |
|---|
| 92 |
} |
|---|
| 93 |
$file = self::createThumb($fieldname, |
|---|
| 94 |
$destination_dir, |
|---|
| 95 |
$filename, |
|---|
| 96 |
$width, |
|---|
| 97 |
$height, |
|---|
| 98 |
$mime, |
|---|
| 99 |
$quality, |
|---|
| 100 |
array('squarize' => $squarize)); |
|---|
| 101 |
} |
|---|
| 102 |
return $file; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
* Deletes images in a multiple formats context |
|---|
| 107 |
* |
|---|
| 108 |
* @param string $path |
|---|
| 109 |
* @param string $basedir |
|---|
| 110 |
* @param array $formats |
|---|
| 111 |
* @return boolean |
|---|
| 112 |
*/ |
|---|
| 113 |
public static function deleteImagesFromFormatParameters($filename, $basedir, $formats = array()) |
|---|
| 114 |
{ |
|---|
| 115 |
$errors = 0; |
|---|
| 116 |
foreach (array_keys($formats) as $format) |
|---|
| 117 |
{ |
|---|
| 118 |
if (!@unlink($basedir.DIRECTORY_SEPARATOR.$format.DIRECTORY_SEPARATOR.$filename)) |
|---|
| 119 |
{ |
|---|
| 120 |
$errors++; |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
return ($errors > 0); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
} |
|---|
| 127 |
|
|---|