deblan.tv/vendor/trinity/src/Trinity/Bundle/MediaBundle/Model/Media.php

100 lines
2.6 KiB
PHP

<?php
namespace Trinity\Bundle\MediaBundle\Model;
use Trinity\Bundle\MediaBundle\Model\om\BaseMedia;
use Trinity\Component\Utils\Image;
class Media extends BaseMedia
{
public function __toString()
{
return (string) $this->getFile();
}
public function getNewFilename($extension)
{
if ($this->rollback_file) {
return $this->rollback_file;
}
$basename = preg_replace('"\..*$"', '', $this->file->getClientOriginalName());
$filename = $this->fullSlugify($basename).'.'.$extension;
$c = 1;
while (MediaQuery::create()->findOneByFile($filename)) {
$filename = $basename.'-'.$c.'.'.$extension;
$c++;
}
return $filename;
}
public function getThumbnail()
{
$file = $this->getFileAsFile();
if (empty($file)) {
return null;
}
if (0 !== preg_match('/image/', $file->getMimeType())) {
return $this->getUploadDir('file').'/thumb_'.$this->getBasename();
}
if (!$this->getType()) {
return null;
}
$fileThumb = $this->getType()->getThumbnail();
return $fileThumb ? $fileThumb : null;
}
public function getBasename()
{
return basename($this->getFile());
}
public function setTypeId($id)
{
if ($id === '') {
$id = null;
}
parent::setTypeId($id);
}
public function postSave(\PropelPDO $con = null)
{
// if (!$this->getTypeId() && $this->getFileAsFile()) {
// $this->setTypeId(TypePeer::getTypeFor($this->getFileAsFile()));
// }
if ($this->hasUploadedFile()) {
if (@getimagesize($this->getWebPathForFile())) {
Image::create($this->getWebPathForFile())
->crop(2000, 2000, 0, 0)
->save();
$file = $this->getFileAsFile();
Image::create($this->getWebPathForFile())
->thumb(100, 100)
->save($file->getPath().'/thumb_'.$file->getBasename());
}
}
return parent::postSave($con);
}
protected function fullSlugify($text)
{
$text = strtr(utf8_decode($text), utf8_decode("ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ"), utf8_decode("aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn"));
$text = preg_replace('/\W+/', '-', utf8_encode($text));
$text = strtolower(trim($text, '-'));
return $text;
}
}