56 lines
2.0 KiB
PHP
56 lines
2.0 KiB
PHP
<?php
|
|
@require_once('DB.php');
|
|
@require_once('Services.php');
|
|
//ini_set('display_errors', 1);error_reporting(E_ALL);
|
|
|
|
|
|
class Images {
|
|
|
|
public static function getUrlFromUrlThumb($url) {
|
|
return str_replace('_thumb', '', $url);
|
|
}
|
|
|
|
public static function insertStringBeforeExtension($originalImagePath, $string = '_thumb') {
|
|
$extension_pos = strrpos($originalImagePath, '.'); // find position of the last dot, so where the extension starts
|
|
$thumb = substr($originalImagePath, 0, $extension_pos) . $string . substr($originalImagePath, $extension_pos);
|
|
return $thumb;
|
|
}
|
|
|
|
public static function upload($file) {
|
|
$size = $file['size'];
|
|
if (!$size) {
|
|
return;
|
|
}
|
|
$maxSize = 8 * 1024 * 1024;
|
|
$unik = uniqid();
|
|
Services::log("upload unik:$unik size:$size");
|
|
if ($size>$maxSize) {
|
|
return false;
|
|
}
|
|
|
|
$fileParts = pathinfo($file['name']);
|
|
$destinationFilename = sprintf("%s.%s", $unik, $fileParts['extension']);
|
|
$destination = UPLOAD_IMAGES_MESSAGES.$destinationFilename;
|
|
move_uploaded_file($file['tmp_name'], $destination);
|
|
if (filesize ($destination) >$maxSize) {
|
|
unlink($destination);
|
|
return false;
|
|
}
|
|
$cmd = "convert -thumbnail 1200 $destination $destination";
|
|
exec($cmd);
|
|
$urlThumb = self::createThumb(UPLOAD_IMAGES_MESSAGES_WIDTH, UPLOAD_IMAGES_MESSAGES_HEIGHT, $destination);
|
|
return $urlThumb;
|
|
}
|
|
|
|
|
|
public static function createThumb($width, $height, $originalImagePath) {
|
|
$finalImagePath = self::insertStringBeforeExtension($originalImagePath);
|
|
//convert -thumbnail 200 abc.png thumb.abc.png
|
|
$cmd = "convert -thumbnail $width $originalImagePath $finalImagePath";
|
|
exec($cmd);
|
|
$url = UPLOAD_IMAGES_MESSAGES_URL.basename($finalImagePath);
|
|
return $url;
|
|
}
|
|
|
|
}
|